Ask Your Question
1

dividing image horizontally into equal parts

asked 2016-01-07 09:48:19 -0600

sarmad gravatar image

Hi everyone:

I want to divide this image into two equal horizontal parts and find the difference between the number of white pixel in the bottom part to the number of pixels in the above part .

Thanks for help

input image

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2016-01-07 10:12:46 -0600

thdrksdfthmn gravatar image

updated 2016-01-07 11:06:43 -0600

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, 0, imageInit.cols, imageInit.rows / 2 );
cv::Rect bottomROI( 0, topROI.height, imageInit.cols, imageInit.rows - topROI.height );
int whitePixelsTop = cv::countNonZero( imageInit(topROI) );
int whitePixelsBottom = cv::countNonZero( imageInit(bottomROI) );
edit flag offensive delete link more

Comments

@sturkmen thanks very much.

can you explain the parameters of Rect (topROI and bottomROI)

cv::Rect topROI(0, 0,gray.cols, gray.rows / 2);

cv::Rect bottomROI(0, topROI.height, gray.cols, gray.rows - topROI.height);
sarmad gravatar imagesarmad ( 2016-01-07 17:36:59 -0600 )edit
1

I have written the answer, and the remark of @sturkmen (imageInint shoud be a single-channel Mat) is a good one. The parameters are explained in the docs of Rect (Rect is Rect_<int>).

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-08 02:32:18 -0600 )edit

for parameters of cv::Rect bottomROI(0, topROI.height, gray.cols, gray.rows - topROI.height);

       0: is top x  ,

      y: equals to the height of the first half (topROI)

     width : imageInit.cols    (image cols)

    height : image rows - topROI  height

Is that right , Thanks

sarmad gravatar imagesarmad ( 2016-01-08 09:43:33 -0600 )edit

I tried to display whitePixelsTop and int whitePixelsBottom and calculate the difference

Why I'm getting unhadled exception ? Code

sarmad gravatar imagesarmad ( 2016-01-08 10:00:51 -0600 )edit
  • x = 0 (so col 0 so the leftest col);
  • y = topROI.height (so the second half starts after the first, in the meddle row);
  • width = image.cols (you take ll the width of the image)
  • height = image.rows - topROI.height (the rest of the image; here it should be the same as the topROI.height if the image has an even number of rows)
thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-08 10:07:09 -0600 )edit

As mentioned by sturkmen, src is not binary (it is RGB). Do a threshold on it

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-08 10:09:59 -0600 )edit

the image is a single channel and I checked it by image.channels()

also I threshold it threshold(gray, gray_thresh, 50, 255, THRESH_BINARY);

it still throws exception

sarmad gravatar imagesarmad ( 2016-01-08 10:27:05 -0600 )edit

And do you apply the countNonZero on the gray_thresh image (or a roi of it)?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-11 02:27:33 -0600 )edit

It is solved now , I passed a one channel image to this function , many thanks for help

sarmad gravatar imagesarmad ( 2016-01-12 04:38:57 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-07 09:48:19 -0600

Seen: 4,052 times

Last updated: Jan 07 '16