我如何使用python opencv相互分离重叠的卡片? [英] How do i separate overlapping cards from each other using python opencv?

查看:155
本文介绍了我如何使用python opencv相互分离重叠的卡片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测纸牌,并使用python opencv将其转换为一张纸牌的鸟瞰图。我的代码在简单情况下工作正常,但我并没有停止在简单情况下,而是想尝试更复杂的情况。我在寻找正确的卡片轮廓时遇到问题。这是我尝试检测卡片并绘制轮廓的附件图像:

I am trying to detect playing cards and transform them to get a bird's eye view of the card using python opencv. My code works fine for simple cases but I didn't stop at the simple cases and want to try out more complex ones. I'm having problems finding correct contours for cards.Here's an attached image where I am trying to detect cards and draw contours:

我的代码:

path1 = "F:\\ComputerVisionPrograms\\images\\cards4.jpeg"
g = cv2.imread(path1,0)
img = cv2.imread(path1)

edge = cv2.Canny(g,50,200)

p,c,h = cv2.findContours(edge, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rect = []
for i in c:
    p = cv2.arcLength(i, True)
    ap = cv2.approxPolyDP(i, 0.02 * p, True)
    if len(ap)==4:
        rect.append(i)
cv2.drawContours(img,rect, -1, (0, 255, 0), 3)

plt.imshow(img)
plt.show()

结果:

这不是我想要的,我只希望选择矩形卡,但是由于它们互相遮挡,所以我没有达到我的期望。我相信我需要应用形态学技巧或其他操作来分离它们,或者使边缘更加突出或其他。如果您能分享解决此问题的方法,将不胜感激。

This is not what I wanted, I wanted only the rectangular cards to be selected but since they are occluding one another, I am not getting what I expected. I believe I need to apply morphological tricks or other operations to maybe separate them or make the edges more prominent or may be something else. It would be really appreciated if you could share your approach to tackle this problem.

其他人要求提供的其他示例:

A few more examples requested by other fellows:

推荐答案

有很多方法可以找到图像中的重叠对象。您可以肯定的信息是,您的卡都是矩形的,大部分是白色的,并且大小相同。您的变量是亮度,角度,可能有些透视失真。如果您想要一个可靠的解决方案,则需要解决所有这些问题。

There are lots of approaches to find overlapping objects in the image. The information you have for sure is that your cards are all rectangles, mostly white and have the same size. Your variables are brightness, angle, may be some perspective distortion. If you want a robust solution, you need to address all that issues.

我建议使用霍夫变换来查找卡片边缘。首先,运行常规边缘检测。不需要清理结果,因为许多短边将属于面对象。牌。我建议结合使用dilate(11)-> erode(15)-> dilate(5)。这种组合将填补脸图中所有的空白。卡,然后缩小沿着斑点向下移去原始边缘,最后长回来并与原始人脸图像重叠一点。然后将其从原始图像中删除。

I suggest using Hough transform to find card edges. First, run a regular edge detection. Than you need to clean up the results, as many short edges will belong to "face" cards. I suggest using a combination of dilate(11)->erode(15)->dilate(5). This combination will fill all the gaps in the "face" card, then it "shrinks" down the blobs, on the way removing the original edges and finally grow back and overlap a little the original face picture. Then you remove it from the original image.

现在,您的图像几乎具有所有相关的边缘。使用霍夫变换找到它们。它会给你一组线。稍微过滤一下之后,您可以将这些边缘调整为卡片的矩形形状。

Now you have an image that have almost all the relevant edges. Find them using Hough transform. It will give you a set of lines. After filtering them a little you can fit those edges to rectangular shape of the cards.

dst = cv2.Canny(img, 250, 50, None, 3)

cn = cv2.dilate(dst, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)))
cn = cv2.erode(cn, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15)))
cn = cv2.dilate(cn, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)))
dst -= cn
dst[dst < 127] = 0

cv2.imshow("erode-dilated", dst)

# Copy edges to the images that will display the results in BGR
cdstP = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)

linesP = cv2.HoughLinesP(dst, 0.7, np.pi / 720, 30, None, 20, 15)

if linesP is not None:
    for i in range(0, len(linesP)):
        l = linesP[i][0]
        cv2.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0, 255, 0), 2, cv2.LINE_AA)

cv2.imshow("Detected edges", cdstP)

这将为您提供以下内容:

This will give you following:

这篇关于我如何使用python opencv相互分离重叠的卡片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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