简体   繁体   English

使用OpenCv检测图像中的矩形明亮区域

[英]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++ 我之前曾问过一个问题,使用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: 原始图片:

原始图片 .But after I used the algorithm, I got something like this(details are visible if you open it in a new tab) 但是,在使用算法后,我得到了这样的信息(如果在新选项卡中打开它的细节可见)

Marked Image: 标记的图像:

在此处输入图片说明 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 谢谢

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: 之后,应用腐蚀squares.cpp技术 (来自OpenCV的示例)输出:

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. find_squares()函数返回一个向量,其中包含图像中找到的所有平方。 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 . 因为它在图像的每个通道上进行迭代,所以在您的示例中,它成功检测到每个图像中的矩形区域,因此打印squares.size()输出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. 由于正方形可以看作是4个(X,Y)坐标的vector<Point> ,因此OpenCV将此概念表示为vector<Point>使您可以访问X和Y部分的坐标。

Now, printing squares revelead that the points were detected in a counterclockwise direction: 现在,打印squares表明已在逆时针方向上检测到这些点:

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: 在此示例之后,很明显,如果您需要增加矩形的高度,则需要更改第一点和第四点的Y:

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

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. 如果这是一致的,我会考虑减去背景阴影。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM