Finding location of rectangles in an image with OpenCV

前端 未结 6 1852
自闭症患者
自闭症患者 2020-12-08 05:52

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

6条回答
  •  旧巷少年郎
    2020-12-08 06:20

    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!

提交回复
热议问题