rellenar reconocimiento patrones filtros dibujar detectar deteccion contornos contorno con c++ opencv image-processing computer-vision computer-science

c++ - filtros - reconocimiento de patrones python



OpenCV: Encuentre todas las coordenadas distintas de cero de una imagen binaria de Mat. (3)

Existe el siguiente código fuente que se suministró para OpenCV 2.4.3 , que puede ser útil:

#include <opencv2/core/core.hpp> #include <vector> /*! @brief find non-zero elements in a Matrix * * Given a binary matrix (likely returned from a comparison * operation such as compare(), >, ==, etc, return all of * the non-zero indices as a std::vector<cv::Point> (x,y) * * This function aims to replicate the functionality of * Matlab''s command of the same name * * Example: * /code * // find the edges in an image * Mat edges, thresh; * sobel(image, edges); * // theshold the edges * thresh = edges > 0.1; * // find the non-zero components so we can do something useful with them later * vector<Point> idx; * find(thresh, idx); * /endcode * * @param binary the input image (type CV_8UC1) * @param idx the output vector of Points corresponding to non-zero indices in the input */ void find(const cv::Mat& binary, std::vector<cv::Point> &idx) { assert(binary.cols > 0 && binary.rows > 0 && binary.channels() == 1 && binary.depth() == CV_8U); const int M = binary.rows; const int N = binary.cols; for (int m = 0; m < M; ++m) { const char* bin_ptr = binary.ptr<char>(m); for (int n = 0; n < N; ++n) { if (bin_ptr[n] > 0) idx.push_back(cv::Point(n,m)); } } }

Nota: parece que la firma de la función estaba equivocada, así que he cambiado el vector salida paso por referencia.

Estoy intentando encontrar las coordenadas distintas de cero (x, y) de una imagen binaria.

He encontrado algunas referencias a la función countNonZero() que solo cuenta las coordenadas distintas de cero y findNonZero() que no estoy seguro de cómo acceder o usar, ya que parece que se ha eliminado por completo de la documentación.

This es la referencia más cercana que encontré, pero todavía no me sirve de nada. Agradecería cualquier ayuda específica.

Edición: - Para especificar, esto es usar C ++


Puede encontrarlo sin usar findNonZero () este método opencv. más bien puedes obtenerlo simplemente usando 2 para bucles. Aquí está el fragmento. Espero que te pueda ayudar.

**

for(int i = 0 ;i <image.rows() ; i++){// image : the binary image for(int j = 0; j< image.cols() ; j++){ double[] returned = image.get(i,j); int value = (int) returned[0]; if(value==255){ System.out.println("x: " +i + "/ty: "+j);// returned the (x,y) //co ordinates of all white pixels. } } }

**


Here hay una explicación de cómo findNonZero() guarda elementos que no son cero. Los siguientes códigos deberían ser útiles para acceder a coordenadas que no sean cero de su imagen binaria . El Método 1 usó findNonZero() en OpenCV, y el Método 2 verificó cada píxel para encontrar los que no eran cero (positivos).

Método 1:

#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> using namespace std; using namespace cv; int main(int argc, char** argv) { Mat img = imread("binary image"); Mat nonZeroCoordinates; findNonZero(img, nonZeroCoordinates); for (int i = 0; i < nonZeroCoordinates.total(); i++ ) { cout << "Zero#" << i << ": " << nonZeroCoordinates.at<Point>(i).x << ", " << nonZeroCoordinates.at<Point>(i).y << endl; } return 0; }

Método 2:

#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> using namespace std; using namespace cv; int main(int argc, char** argv) { Mat img = imread("binary image"); for (int i = 0; i < img.cols; i++ ) { for (int j = 0; j < img.rows; j++) { if (img.at<uchar>(j, i) > 0) { cout << i << ", " << j << endl; // Do your operations } } } return 0; }