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

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

问题描述

我尝试使用OpenCV从iPhone游戏剖析屏幕截图



对于您的数据,您尝试查找的矩形是唯一的黑色对象。所以你可以尝试做阈值二值化:黑色像素是那些具有小于40的所有三个RGB值(我已经发现它的经验)。这个简单的操作使你的照片看起来像这样:





之后,您可以应用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天全站免登陆