Search code examples
c#imagewinformsbitmaprectangles

Determine the white rectangles in the image c#


I want to determine the white rectangles in the images. I will use the rectangles to create panels.

The purpose of that to create a similar view inside the TableLayout control as you see in picture below. I need algorithm that can detect white areas in pictures. I know I should use the color values but I couldn't figure it out quite right.

Note: By white area I mean white and tones close to it.

enter image description here


Solution

  • If you are sure the white rectangles are exactly rectangular, and the image is exactly black and white this should not be that difficult of a problem. You could for example use an algorithm like this:

    1. For all pixels:
      1. If a pixel is white, but has a black pixel above it and to the left:
        1. loop over pixels to the right until you find a black pixel to get the width
        2. Loop over pixels below to get the height
        3. You should now have a x/y position and a width height, so create and output the rectangle

    If your input data is less clean you might be forced to use more complicated image processing techniques, and this can significantly increase the complexity.