Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

For dividing the image horizontally into 2 equal parts and counting the white pixels in both parts, you shall do

cv::Rect topROI(0, 0, imageInit.cols, imageInit.rows / 2); // here you should treat the case of odd number of rows
cv::Rect bottomROI(0, imageInit.cols / 2 + 1, imageInit.cols, imageInit.rows);
int whitePixelsTop = cv::countNonZero(imageInit(topROI));
int whitePixelsBottom = cv::countNonZero(imageInit(bottomROI));

For dividing the image horizontally into 2 equal parts and counting the white pixels in both parts, you shall do

// imageInit should be a single-channel Mat
cv::Rect topROI(0, topROI( 0, 0, imageInit.cols, imageInit.rows / 2); // here you should treat the case of odd number of rows
2 );
cv::Rect bottomROI(0, imageInit.cols / 2 + 1, bottomROI( 0, topROI.height, imageInit.cols, imageInit.rows);
imageInit.rows - topROI.height );
int whitePixelsTop = cv::countNonZero(imageInit(topROI));
cv::countNonZero( imageInit(topROI) );
int whitePixelsBottom = cv::countNonZero(imageInit(bottomROI));
cv::countNonZero( imageInit(bottomROI) );