如何更准确地检测圆角矩形

How to detect rectangles with rounded corners more accurately

我正在尝试创建一个需要很长时间才能在这里解释的程序,所以我会告诉你们我需要帮助的部分。

这里我需要检测一个矩形(在我们的例子中是车牌)。它几乎可以完美地识别,但我希望它更精确。这是我使用的示例图片。

如您所见,它在找到它方面做得相当好,但我也想考虑圆角。

这是源代码

import numpy as np
import cv2
import imutils

def find_edges(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (3, 3), 0)
    edged = cv2.Canny(image=gray, threshold1=100, threshold2=200)
    cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        if len(approx) == 4:
            screenCnt = approx
            break
    return screenCnt


image = cv2.imread('img/plate.jpeg')
cnt = find_edges(image)
cv2.drawContours(image, [cnt], -1, (0, 255, 0), 2)
cv2.imshow('Outline', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

我有什么方法可以实现这个目标吗?如何实现的,或者我是否在这方面努力过头了?

编辑: 添加示例图像。很抱歉没有包括之前,我的错。

首先,在您的 find_edges 函数中,我将 screenCnt = approx 行替换为 screenCnt = c,以便将所有坐标保留在检测到的结果轮廓中:

def find_edges(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (3, 3), 0)
    edged = cv2.Canny(image=gray, threshold1=100, threshold2=200)
    cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        if len(approx) == 4:
            screenCnt = c
            break
    return screenCnt

然后,我定义了一个函数,get_opposites,它将接受一个轮廓,return 两个距离轮廓最远的坐标:

def get_opposites(cnt):
    current_max = 0
    c = None
    for a, b in combinations(cnt, 2):
        current_distance = np.linalg.norm(a - b)
        if current_distance > current_max:
           current_max = current_distance
           c = a, b
    return c

接下来,我将从图像中检测到的轮廓(使用您定义的find_edges函数+我的更改)分成两部分;第一部分包含轮廓的左上 + 右下四分之一,第二部分包含轮廓的右上 + 左下四分之一:

image = cv2.imread('img/plate.jpeg')
cnt = find_edges(image)
xs = cnt[..., 0]
ys = cnt[..., 1]
x_mid = (np.amin(xs) + np.amax(xs)) // 2
y_mid = (np.amin(ys) + np.amax(ys)) // 2
tl_br = cnt[((ys < y_mid) & (xs < x_mid)) | ((ys > y_mid) & (xs > x_mid))]
tr_bl = cnt[((ys > y_mid) & (xs < x_mid)) | ((ys < y_mid) & (xs > x_mid))]

最后,我使用 `` 函数从每个部分获取两个坐标,并将它们放入要绘制到图像上的 numpy 数组中:

p1, p3 = get_opposites(tl_br)
p2, p4 = get_opposites(tr_bl)
cv2.polylines(image, np.array([[p1, p2, p3, p4]], np.int32), True, (0, 255, 0), 2)
cv2.imshow('Outline', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

总计:

import numpy as np
import cv2
import imutils
from itertools import combinations
       
def find_edges(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (3, 3), 0)
    edged = cv2.Canny(image=gray, threshold1=100, threshold2=200)
    cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        if len(approx) == 4:
            screenCnt = c
            break
    return screenCnt

def get_opposites(cnt):
    current_max = 0
    c = None
    for a, b in combinations(cnt, 2):
        current_distance = np.linalg.norm(a - b)
        if current_distance > current_max:
           current_max = current_distance
           c = a, b
    return c

image = cv2.imread('img/plate.jpeg')
cnt = find_edges(image)
xs = cnt[..., 0]
ys = cnt[..., 1]
x_mid = (np.amin(xs) + np.amax(xs)) // 2
y_mid = (np.amin(ys) + np.amax(ys)) // 2
tl_br = cnt[((ys < y_mid) & (xs < x_mid)) | ((ys > y_mid) & (xs > x_mid))]
tr_bl = cnt[((ys > y_mid) & (xs < x_mid)) | ((ys < y_mid) & (xs > x_mid))]

p1, p3 = get_opposites(tl_br)
p2, p4 = get_opposites(tr_bl)
cv2.polylines(image, np.array([[p1, p2, p3, p4]], np.int32), True, (0, 255, 0), 2)
cv2.imshow('Outline', image)
cv2.waitKey(0)
cv2.destroyAllWindows()