I start with OpenCV, and I read several tutorials and tutorials, but I couldnβt understand some things.
I'm currently trying to crop a binary image into two sections. I want to know which row has the most white pixels, and then cut the line and everything above it, and then redraw the image with only the data below the line with the most white pixels.
What I have done so far is to find the coordinates of the white pixels using findNonZero, and then save it in Mat. The next step is embarrassing. I'm not sure how to access the elements in Mat and figure out which row is the most in the array.
I used a test image with my code below. This gave me a pixel layout of [2.0; 1.1; 2.1; 3.1; 0.2; 1,2; 2.2; 3.2; 4.2; 1.3; 2.3; 3.3; 2.4]. Each element has an x ββand y coordinate of a white pixel. First of all, how can I access each element and then only poll the y-coordinate in each element to determine the most significant row? I tried using the at <> () method, but I don't think I used it correctly.
Is this method a good way to do this, or is there a better and / or faster way? I read another method here using the L1 norm, but I could not figure out if this method would be faster than mine?
Any help would be greatly appreciated.
Below is the code that I have.
#include <opencv2\opencv.hpp> #include <opencv2\imgproc\imgproc.hpp> #include <opencv2\highgui\highgui.hpp> #include <iostream> using namespace cv; using namespace std; int main() { int Number_Of_Elements; Mat Grayscale_Image, Binary_Image, NonZero_Locations; Grayscale_Image = imread("Test Image 6 (640x480px).png", 0); if(!Grayscale_Image.data) { cout << "Could not open or find the image" << endl; return -1; } Binary_Image = Grayscale_Image > 128; findNonZero(Binary_Image, NonZero_Locations); cout << "Non-Zero Locations = " << NonZero_Locations << endl << endl; Number_Of_Elements = NonZero_Locations.total(); cout << "Total Number Of Array Elements = " << Number_Of_Elements << endl << endl; namedWindow("Test Image",CV_WINDOW_AUTOSIZE); moveWindow("Test Image", 100, 100); imshow ("Test Image", Binary_Image); waitKey(0); return(0); }