0

The image below only contains the black and white pixels after thresholding. I draw a rotated rectangle in grey on top of this image. Now I would like to count the number of black pixels within this rotated rectangle, but not including the black pixels outside the white rectangle-ish rectangle (i.e. number of the pixels within the white rectangle).

What is the best approach to do that? Shall I fill the area outside the white rectangle with white pixel? Any suggestions are welcomed.

enter image description here

chesschi
  • 666
  • 1
  • 8
  • 36

1 Answers1

1

If you know the angle and size of the area you want to search, you can cut it out of the image using this technique:

How to straighten a rotated rectangle area of an image using opencv in python?

(I know its Python, but there's a c++ equivalent)

EDIT

You can use this code to return and print the value of each pixel so you can get an idea on the kind of values you are getting back (should just be 0 and 1/255 if binary image)

for(int i=0; i<img.rows; i++)
    for(int j=0; j<img.cols; j++) 
        std::cout<<"Value: "<<static_cast<int>(gray_image.at<uchar>(i,j));

Once you are getting these values maybe make a counter that will increment every time a pixel has a value over a certain threshold

Community
  • 1
  • 1
Aphire
  • 1,621
  • 25
  • 55
  • Did you see there are some black pixels between the rotated rectangle in grey and outside the white rectangle? I want to exclude them when counting the number of black pixels -- that is my key question – chesschi Feb 24 '15 at 10:57
  • Make your rectangle smaller by like 5% either side so that it's tight to the white section of the image – Aphire Feb 24 '15 at 11:03
  • Or to be precise, decrease the side of the rectangle until there are no black pixels around the edge, you can check the edge pixels using the code in my answer and just change it so that it checks from 0-img.width, 0- img.length, img.width - img.length and img.lenth-img.width – Aphire Feb 24 '15 at 11:06
  • The number of black pixels around the white rectangle within the rotated rectangle could vary and could be in different angle (i.e. more black pixels). Is there any better approach aparting from cropping the edges each side around the rotated rectangle? – chesschi Feb 24 '15 at 11:06
  • Try my second comment about incrementally making it smaller until there is no more black pixels around the edge. – Aphire Feb 24 '15 at 11:11
  • Create a second image, crop one and count the other – Aphire Feb 24 '15 at 11:17
  • Create a copy of the rotated rectangle image. Run the crop function (looking for all black pixels along the edge and crop until no more) use this image to count the black pixels within the white image. You can use the original image to get any more information you require about the black pixels around the edge – Aphire Feb 24 '15 at 11:21
  • It sounds complicated. How can I turn this in code, especially trace the black pixels along the edge? – chesschi Feb 24 '15 at 11:25
  • Okay, so once you have the rotated rectangle straightened out and saved as a separate image (see the first link) you can modify the code that loops through the pixels to only loop through the top line of pixels (for img.rows) Then if there is a black pixel found, move down to the next row. continue doing this until you have a full row of white pixels, then do the same for the bottom rows, left and right until you have a rectangle with no black pixels around the edge – Aphire Feb 24 '15 at 11:34
  • Failing that, this might be useful: http://opencvpython.blogspot.in/2012/06/hi-this-article-is-tutorial-which-try.html – Aphire Feb 24 '15 at 15:07