5

I have previously asked a question Marking an interest point in an image using c++. I used the same solution and got the required point using the adaptive threshold and Blob Detection Algorithm(Growing Regions). I have the original source figure where I want to detect the rectangular region at the center

Original Image:

Original Image.But after I used the algorithm, I got something like this(details are visible if you open it in a new tab)

Marked Image:

enter image description here where apart from the rectangular region the bright day light illuminated spots are also visible. I have used bilateral filtering but still I am not able to detect the rectangular region.But this algorithm works for the Night image where the background is more darker as expected.

Can someone suggest me whether the same algorithm with some modifications is sufficient or any other efficient ways are available..

Thanks

Community
  • 1
  • 1
ShivShambo
  • 503
  • 12
  • 29

2 Answers2

4

Using a simple combination of blur & threshold I managed to get this result (resized for viewing purposes):

After that, applying erosion & the squares.cpp technique (which is a sample from OpenCV) outputs:

which is almost the result you are looking for: the bottom part of the rectangle was successfully detected. All you need to do is increase the height of the detected rectangle (red square) to fit your area of interest.

Code:

Mat img = imread(argv[1]);

    // Blur
Mat new_img = img.clone();
medianBlur(new_img, new_img, 5);

// Perform threshold
double thres = 210;
double color = 255;
threshold(new_img, new_img, thres, color, CV_THRESH_BINARY);
imwrite("thres.png", new_img);

// Execute erosion to improve the detection
int erosion_size = 4;   
Mat element = getStructuringElement(MORPH_CROSS,
                                   Size(2 * erosion_size + 1, 2 * erosion_size + 1),
                                   Point(erosion_size, erosion_size) );
erode(new_img, new_img, element);
imwrite("erode.png", new_img);

vector<vector<Point> > squares;
find_squares(new_img, squares);
std::cout << "squares: " << squares.size() << std::endl;

draw_squares(img, squares);

imwrite("area.png", img);

EDIT:

The find_squares() function returns a vector with all the squares found in the image. Because it iterates on every channel of the image, on your example it successfully detects the rectangular region in each of them, so printing squares.size() outputs 3.

As a square can be seen as a vector of 4 (X,Y) coordinates, OpenCV express this concept as vector<Point> allowing you to access the X and Y part the coordinate.

Now, printing squares revelead that the points were detected in a counterclockwise direction:

1st ------ 4th
 |          |
 |          |
 |          |
2nd ------ 3rd

Following this example, its fairly obvious that if you need to increase the height of the rectangle you need to change the Y of the 1st and 4th points:

for (int i = 0; i < squares.size(); i++)
{
    for (int j = 0; j < squares[i].size(); j++)
    {
    //  std::cout << "# " << i << " " << squares[i][j].x << ","<< squares[i][j].y << std::endl;
        if (j == 0 || j == 3)
            squares[i][j].y = 0;
    }
}

Community
  • 1
  • 1
karlphillip
  • 87,606
  • 33
  • 227
  • 395
  • @Karlphilip wow..Thats Great..Thanks a lot for your help.I am getting the desired result for the darker images also but with hardcoded threshold say(around 80).If it goes away from the region.I cannot find the squares. So I used Otsu thresholding value but I have to add 30 for the satisfactory detection. Any other way to detect both the darker and brighter images – ShivShambo May 15 '12 at 03:08
  • That is the only one I can think of. – karlphillip May 15 '12 at 11:26
  • @Karlphilip..Using this algorithm I want to just find the coordinates of the center of the rectangular area in the original figure. any ideas that I can approach. – ShivShambo May 16 '12 at 01:04
  • Well, since you already have the coordinates of the edges of the rectangle, a simple math operation will give you the center coordinates, wouldn't it? Let's keep one question per thread. If you have more questions feel free to ask them in new threads. – karlphillip May 16 '12 at 01:12
  • @lakshmikant You shouldn't add more points to the vector, just change the ones you have. A `cv::Point` stores a X,Y coordinate. You are interested in change the Y coordinate to 0 in 2 points. Remember, a square has 4 points. – karlphillip May 16 '12 at 16:45
  • @karlphilip.. one more doubt..how do you find the maximum Y value for the picture? ie the bottom edge – ShivShambo May 17 '12 at 02:18
  • @lakshmikant Reflect upon the `for` loops I added when the answer was updated. Unfortunately, we have a policy of 1 question per thread, I think I ended up answering a few more than that. I encourage you to make new questions in new threads. Good luck – karlphillip May 17 '12 at 02:41
1

In the image shown above, I would suggest

  1. either a normal thresholding operation which should work pretty well or

  2. a line-wise chain-code "calculation" or

  3. finding gradients in your histogram.

There would be plenty of other solutions. I would consider to subtract the background shading if this is consistent.

Kiquenet
  • 13,271
  • 31
  • 133
  • 232
guitarflow
  • 2,832
  • 21
  • 35