Prathik
Prathik

Reputation: 15

Finding a rectangular space in an image without clear borders/edges

I'm trying to find a region with similar pixel values without any borders/edges.

I have uploaded an album with desired regions drawn in a rectangular box https://i.sstatic.net/jXnM6.jpg

Even though few of the images have well defined edges, i'm trying to come up with an algorithm which will work for ones without clear edges.

I'm thinking of iterating through the image pixel by pixel and trying to find region with similar pixel values and drawing a rectangle around them.

EDIT:

I was able to remove the noise and come up with a binary image with important edges, I'm not clear on how to fit rectangles of about 4:3 aspect ratio in these images https://i.sstatic.net/4H30C.jpg

Upvotes: 1

Views: 205

Answers (1)

KjMag
KjMag

Reputation: 2770

Your approach isn't really the way to do it. It is not efficient and very hard to automate - if you compare pixels by value, you will end up trying to find a proper pixel value range for each picture separately (because depending on the picutre, a similar pixel value will mean something different), which is troublesome and extremely time-consuming.

Your task contains two parts: the first one is extracting the area of uniform colour, and the second is to try to fit the biggest rectangle possible inside this area.

In order to identify the area, where you can draw your rectangle, the first step would be identifying edges of these areas. Get familiar with high-pass image filters and gradient filters - they will allow you to detect edges and thus distinguish areas that are different in terms of pixel values.

You can try also Hough transform - it is used to identify straight lines on an image and might be especially helpful in case of your third image (the whiteboard with some text written on it), i.e. when the borders are not defined that clearly.

In general, try googling for 'edge detection', 'edge extraction' etc. - this topic is well covered in many papers, image processing libraries etc. I will save you much time, and if you are going to do some serious image processing, you will have to learn it anyway.

You won't have to implement most of these algorithms yourself if you want to test them since they are already implemented in openCV.


After you have the edges extracted, you can for example do this: for each pixel contained within the detected area (i.e. the area in which you want to fit the rectangle):

  1. Try to draw a biggest rectangle possible, whose upper left corner is anchored in that pixel (if you start iterating from upper left corner of the area).

  2. Compare the dimensions of this rectangle with the biggest rectangle you were able to fit so far.

  3. Save the dimensions and position of the rectangle, if it is currently the biggest rectangle.

After iterating through all the pixels of the selected area, you will get the biggest rectangle you can fit together with the position, at which it should be drawn.

Upvotes: 1

Related Questions