OpenCV:使用 Canny 和 Shi-Tomasi 检测扑克牌的圆角 [英] OpenCV: using Canny and Shi-Tomasi to detect round corners of a playing card

查看:89
本文介绍了OpenCV:使用 Canny 和 Shi-Tomasi 检测扑克牌的圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些平面矫正,从左到右转换:

I want to do some planar rectification, to convert from left to right:

我有修正代码,但我需要 4 个角坐标.

I have the code to do the correction, but I need the 4 corner coords.

我正在使用以下代码来查找它们:

I'm using the following code to find them:

import cv2

image = cv2.imread('input.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray, 120, 255, 1)

corners = cv2.goodFeaturesToTrack(canny,4,0.5,50)

for corner in corners:
    x,y = corner.ravel()
    cv2.circle(image,(x,y),5,(36,255,12),-1)

cv2.imshow("result", image)
cv2.waitKey()

它读取图像,并将其转换为灰度 + canny

It reads the image, and transforms it to grayscale + canny

但由此产生的角(由 cv2.goodFeaturesToTrack 发现)不是想要的角:

But the resultant corners (found by cv2.goodFeaturesToTrack) aren't the desired ones:

我需要卡片的外角,有什么线索可以实现吗?

I need the external corners of the card, any clue to achieve it?

谢谢

这是 input.png:

This is the input.png:

推荐答案

Canny 是一种边缘检测工具,如果正确调整,它会按照它在锡上所说的做.

Canny is a tool for edge detection, and if correctly tuned it does what it says on the tin.

获得边缘后,您必须定义角是什么.例如,它是边缘的急转弯吗?

Once you get the edges, you must define what a corner is. For instance, is it a sharp turn in a edge?

您想使用 cv2.goodFeaturesToTrack 函数,它应该是一个 角点检测工具,但再次强调,角点是什么?它使用 Shi-Tomasi 算法来找到N最佳"图像中的角点,这只是一个阈值,以及点之间的一些最小距离.

You'd like to use the function cv2.goodFeaturesToTrack, which is supposed to be a corner detection tool, but once again, what is a corner? It uses the Shi-Tomasi algorithm to find the N "best" corners in an image, which is just a threshold, and some minimum distance between points.

最后保证几乎永远不会承受你想要的四个角.您应该尝试这些替代方案,并坚持最佳选择:

In the end, it is guaranteed to almost never bear the four corners you want. You should try these alternatives, and stick with the best option:

  1. 尝试获得更多的角并从几何上确定四个最外"

  1. try to get more corners and geometrically determine the four "outmost" ones.

将您的方法与其他一些转换或对象匹配相结合.例如,如果您正在寻找矩形图像,请尝试将其与模板进行匹配,计算变换矩阵并在变换后解析边缘.

combine your method with some other transformation, or object-matching. For instance, if you are looking for a rectangular-ish image, try to match it against a template, compute the transform matrix and resolve edges after transformation.

使用不同的边缘检测方法,或方法的组合.

use a different edge detection method, or a combination of methods.

请注意,卡片不像一张纸那样有尖角,因此如果使用任何角",您最终会剪裁或歪斜卡片.在圆形边缘上,或试图找到实际白色"之外的边缘卡,以避免歪斜(尝试将卡刻入一个锋利的矩形) - 请注意,在这种情况下,Canny 无效.

Note that a card doesn't have sharp corners like a piece of paper, so you'll end up cropping the card or skewing it if using any "corner" on the rounded edges, or trying to locate an edge outside the actual "white" of the card, to avoid the skew (try to inscribe the card into a sharp-edge rectangle) - note that Canny is not effective in this case.

这篇关于OpenCV:使用 Canny 和 Shi-Tomasi 检测扑克牌的圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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