本文介绍了有没有办法增加cv2.findContours返回的积分数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个我试图匹配拼图游戏的项目,我写了一些代码来确定拼图两边的凹痕(向内和向外).我添加了两个示例图像来显示结果.

For a project where I try to match jigsaw puzzles, I wrote some code that determines the indentations (inward and outward) of each side of a puzzle. I added two example images showing the result.

Inward indentation

Outward indentation

All indents are detected quite nicely but the accuracy of it is not always as expected. I assume this is due to cv2.findContours() that only supplies a limited amount of points to work with. I supplied the code of my cv2.findContours().

imshape = l.getImageShape(image)
ret, thresh = cv2.threshold(imshape, 0, 254, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

I chose to only use the external lines with RETR_EXTERNAL, since those are the only needed contours. I also decided to save every point with CHAIN_APPROX_NONE, even though I do not really need a lot of info around the straight parts of the side. However, CHAIN_APPROX_SIMPLE also removed some points of the crucial (indentation) part, reducing the accuracy even more.

The first function, getImageShape, simply gets the shape on a similar way to thresh, but I found that I got better results using this in advance instead of just taking the source image as input parameter for the cv2.threshold function in previous code.

def getImageShape(img):
    gray   = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray   = cv2.medianBlur(gray, ksize=5)
    thresh = cv2.threshold(gray, 0, 254, cv2.THRESH_BINARY)[1]
    thresh = cv2.blur(thresh, ksize=(3, 3))
    return thresh

Because of the bad accuracy (around 4% inaccuracy of the width/height of a side of the puzzle), matching two sides with eachother only based on their indentation positions results in multiple 'possible' solutions. I was hoping that there was a way to increase the amount of points returned by cv2.getContours but have not found any info about it on the openCV documentation.

If this is impossible, I will have to add some image continuity detection algorithm (but this would make it unable to match pieces only by their shape).

Thanks in advance!

解决方案

If i may suggest an alternative:You can try to make the outlines visible by calculating the image gradient with either a Sobel or Prewitt filter and use Canny edge detection after you did some simple postprocessing.

Here is what i get using that, note that i used the picture you provided with the marker on the outline, so ignore that little inconsistency.

这篇关于有没有办法增加cv2.findContours返回的积分数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:38