Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

c++ - Detection of rectangular bright area in a Image using OpenCv

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

image

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

image

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;
    }
}

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...