Shi*_*mbo 5 c++ algorithm opencv image-processing
我之前曾问过一个问题 ,用c ++标记图像中的兴趣点.我使用相同的解决方案并使用自适应阈值和Blob检测算法(增长区域)获得所需的点.我有原始的源图,我想检测中心的矩形区域
但是在我使用算法之后,我得到了这样的东西(如果你在新标签中打开它,细节是可见的)
除了矩形区域之外,还可以看到明亮的日光照射点.我使用了双边滤波,但我仍然无法检测到矩形区域.但是这个算法适用于夜间图像,其中背景比预期的更暗.
有人可以建议我是否有足够的修改相同的算法或任何其他有效的方法可用..
谢谢
使用模糊和阈值的简单组合,我设法得到了这个结果(调整大小以供查看):
![]()
之后,应用侵蚀和squares.cpp 技术(来自 OpenCV 的示例)输出:
![]()
这几乎就是您正在寻找的结果:成功检测到矩形的底部。您所需要做的就是增加检测到的矩形(红色方块)的高度以适合您感兴趣的区域。
代码:
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);
Run Code Online (Sandbox Code Playgroud)
编辑:
该find_squares()函数返回一个向量,其中包含图像中找到的所有方块。因为它会迭代图像的每个通道,所以在您的示例中,它成功检测到每个通道中的矩形区域,因此打印squares.size()输出3.
由于正方形可以看作是 4 个 (X,Y) 坐标的向量,OpenCV 将这一概念表达为vector<Point>允许您访问坐标的 X 和 Y 部分。
现在,打印squares表明这些点是按逆时针方向检测到的:
1st ------ 4th
| |
| |
| |
2nd ------ 3rd
Run Code Online (Sandbox Code Playgroud)
按照这个例子,很明显,如果您需要增加矩形的高度,您需要更改第 1 个和第 4 个点的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
![]()
| 归档时间: |
|
| 查看次数: |
4828 次 |
| 最近记录: |