使用 OpenCV 查找图像中矩形的位置 [英] Finding location of rectangles in an image with OpenCV

查看:27
本文介绍了使用 OpenCV 查找图像中矩形的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OpenCV 从 iPhone 游戏中解析"屏幕截图

至于您的数据,您要查找的矩形是唯一的黑色对象.所以你可以尝试做一个阈值二值化:黑色像素是那些三个 RGB 值都小于 40 的像素(我凭经验找到了它).这个简单的操作让你的图片看起来像这样:

之后,您可以应用 Hough 变换来查找线条(在我提到的主题中讨论过),或者您可以更轻松地完成.计算黑色像素到 X 和 Y 轴的积分投影.(到 X 的投影是 x_i 的向量 - 黑色像素的数量,使其具有等于 x_i 的第一个坐标).因此,您可以获得可能的 x 和 y 值作为投影的峰值.然后查看所有被找到的 x 和 y 限制的可能段(如果 (x_i, y_j) 和 (x_i, y_k) 之间有很多黑色像素,可能有一条线).最后,将线段组合成矩形!

I'm trying to use OpenCV to "parse" screenshots from the iPhone game Blocked. The screenshots are cropped to look like this:

I suppose for right now I'm just trying to find the coordinates of each of the 4 points that make up each rectangle. I did see the sample file squares.c that comes with OpenCV, but when I run that algorithm on this picture, it comes up with 72 rectangles, including the rectangular areas of whitespace that I obviously don't want to count as one of my rectangles. What is a better way to approach this? I tried doing some Google research, but for all of the search results, there is very little relevant usable information.

解决方案

The similar issue has already been discussed: How to recognize rectangles in this image?

As for your data, rectangles you are trying to find are the only black objects. So you can try to do a threshold binarization: black pixels are those ones which have ALL three RGB values less than 40 (I've found it empirically). This simple operation makes your picture look like this:

After that you could apply Hough transform to find lines (discussed in the topic I referred to), or you can do it easier. Compute integral projections of the black pixels to X and Y axes. (The projection to X is a vector of x_i - numbers of black pixels such that it has the first coordinate equal to x_i). So, you get possible x and y values as the peaks of the projections. Then look through all the possible segments restricted by the found x and y (if there are a lot of black pixels between (x_i, y_j) and (x_i, y_k), there probably is a line probably). Finally, compose line segments to rectangles!

这篇关于使用 OpenCV 查找图像中矩形的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆