OpenCV : Canny 및 Shi-Tomasi를 사용하여 카드 게임의 둥근 모서리 감지

Nov 16 2020

왼쪽에서 오른쪽으로 변환하기 위해 평면 정류를 수행하고 싶습니다.

수정을위한 코드가 있지만 4 개의 코너 좌표가 필요합니다.

나는 그들을 찾기 위해 다음 코드를 사용하고 있습니다.

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()

이미지를 읽고이를 그레이 스케일 + 캐니로 변환합니다.

그러나 결과 코너 (cv2.goodFeaturesToTrack에서 발견)는 원하는 코너가 아닙니다.

카드의 외부 모서리가 필요합니다.이를 달성하기위한 단서가 있습니까?

감사

이것은 input.png입니다 :

답변

1 DaemonPainter Nov 16 2020 at 15:48

Canny는 가장자리 감지 도구이며 올바르게 조정되면 주석에 표시된대로 작동합니다.

모서리를 얻으면 모서리가 무엇인지 정의해야합니다. 예를 들어, 가장자리에서 급격한 회전입니까?

당신이 기능을 사용하려면 cv2.goodFeaturesToTrack있어야하는데, 코너 검색 도구 지만, 다시 한 번 코너는 무엇입니까? Shi-Tomasi 알고리즘을 사용하여 이미지에서 N 개의 "가장 좋은"모서리를 찾습니다. 이는 임계 값일 뿐이며 포인트 간의 최소 거리입니다.

결국, 원하는 네 모서리를 거의 견디지 못할 것입니다. 다음 대안을 시도하고 최상의 옵션을 고수해야합니다.

  1. 더 많은 모서리를 가져 와서 네 개의 "가장 바깥 쪽"모서리를 기하학적으로 결정하십시오.

  2. 방법을 다른 변형 또는 객체 일치와 결합하십시오. 예를 들어 직사각형 이미지를 찾는 경우 템플릿과 일치시키고 변환 행렬을 계산하고 변환 후 가장자리를 해결합니다.

  3. 다른 가장자리 감지 방법 또는 방법 조합을 사용합니다.

카드에는 종이처럼 날카로운 모서리가 없으므로 둥근 가장자리에 "모서리"를 사용하거나 실제 "흰색"바깥 쪽의 가장자리를 찾으려고하면 카드가 잘 리거나 비뚤어집니다. "카드의 왜곡을 방지하기 위해 (카드를 모서리가 날카로운 직사각형에 새겨보십시오)-이 경우 Canny는 효과적이지 않습니다.

B200011011 Nov 16 2020 at 17:08

업데이트 : 4 점 투시 변환이 추가되었습니다.

질문은 오른쪽 모서리를 찾는 것에 관한 것이므로 원근 변환을 건너 뛰었습니다.

윤곽을 얻은 maximum area다음 처리 하여 루프를 건너 뛸 수 있습니다 . 약간의 흐리게 처리가 더 도움이 될 수 있습니다. Esc다음 이미지 출력을 얻으려면 버튼을 누르십시오 .

또 다른 유용한 방법, opencv의 이미지에서 모양의 모서리 점을 찾는 방법은 무엇입니까?

Ouput 이미지

암호

"""
Task: Detect card corners and fix perspective
"""


import cv2
import numpy as np


img = cv2.imread('resources/KSuVq.png')


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,0)


cv2.imshow('Thresholded original',thresh)
cv2.waitKey(0)



## Get contours
contours,h = cv2.findContours(thresh,cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)


## only draw contour that have big areas
imx = img.shape[0]
imy = img.shape[1]
lp_area = (imx * imy) / 10



#################################################################
# Four point perspective transform
# https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/
#################################################################

def order_points(pts):
    # initialzie a list of coordinates that will be ordered
    # such that the first entry in the list is the top-left,
    # the second entry is the top-right, the third is the
    # bottom-right, and the fourth is the bottom-left
    rect = np.zeros((4, 2), dtype = "float32")
    # the top-left point will have the smallest sum, whereas
    # the bottom-right point will have the largest sum
    s = pts.sum(axis = 1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]
    # now, compute the difference between the points, the
    # top-right point will have the smallest difference,
    # whereas the bottom-left will have the largest difference
    diff = np.diff(pts, axis = 1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]
    # return the ordered coordinates
    return rect


def four_point_transform(image, pts):
    # obtain a consistent order of the points and unpack them
    # individually
    rect = order_points(pts)
    (tl, tr, br, bl) = rect
    # compute the width of the new image, which will be the
    # maximum distance between bottom-right and bottom-left
    # x-coordiates or the top-right and top-left x-coordinates
    widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    maxWidth = max(int(widthA), int(widthB))
    # compute the height of the new image, which will be the
    # maximum distance between the top-right and bottom-right
    # y-coordinates or the top-left and bottom-left y-coordinates
    heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    maxHeight = max(int(heightA), int(heightB))
    # now that we have the dimensions of the new image, construct
    # the set of destination points to obtain a "birds eye view",
    # (i.e. top-down view) of the image, again specifying points
    # in the top-left, top-right, bottom-right, and bottom-left
    # order
    dst = np.array([
        [0, 0],
        [maxWidth - 1, 0],
        [maxWidth - 1, maxHeight - 1],
        [0, maxHeight - 1]], dtype = "float32")
    # compute the perspective transform matrix and then apply it
    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
    # return the warped image
    return warped


#################################################################


## Get only rectangles given exceeding area
for cnt in contours:
    approx = cv2.approxPolyDP(cnt,0.01 * cv2.arcLength(cnt, True), True)
    ## calculate number of vertices
    #print(len(approx))


    if len(approx) == 4 and cv2.contourArea(cnt) > lp_area:
        print("rectangle")

        tmp_img = img.copy()
        cv2.drawContours(tmp_img, [cnt], 0, (0, 255, 255), 6)
        cv2.imshow('Contour Borders', tmp_img)
        cv2.waitKey(0)


        tmp_img = img.copy()
        cv2.drawContours(tmp_img, [cnt], 0, (255, 0, 255), -1)
        cv2.imshow('Contour Filled', tmp_img)
        cv2.waitKey(0)


        # Make a hull arround the contour and draw it on the original image
        tmp_img = img.copy()
        mask = np.zeros((img.shape[:2]), np.uint8)
        hull = cv2.convexHull(cnt)
        cv2.drawContours(mask, [hull], 0, (255, 255, 255), -1)
        cv2.imshow('Convex Hull Mask', mask)
        cv2.waitKey(0)


        # Draw minimum area rectangle
        tmp_img = img.copy()
        rect = cv2.minAreaRect(cnt)
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(tmp_img, [box], 0, (0, 0, 255), 2)
        cv2.imshow('Minimum Area Rectangle', tmp_img)
        cv2.waitKey(0)


        # Draw bounding rectangle
        tmp_img = img.copy()
        x, y, w, h = cv2.boundingRect(cnt)
        cv2.rectangle(tmp_img, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.imshow('Bounding Rectangle', tmp_img)
        cv2.waitKey(0)


        # Bounding Rectangle and Minimum Area Rectangle
        tmp_img = img.copy()
        rect = cv2.minAreaRect(cnt)
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(tmp_img, [box], 0, (0, 0, 255), 2)
        x, y, w, h = cv2.boundingRect(cnt)
        cv2.rectangle(tmp_img, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.imshow('Bounding Rectangle', tmp_img)
        cv2.waitKey(0)


        # determine the most extreme points along the contour
        # https://www.pyimagesearch.com/2016/04/11/finding-extreme-points-in-contours-with-opencv/
        tmp_img = img.copy()
        extLeft = tuple(cnt[cnt[:, :, 0].argmin()][0])
        extRight = tuple(cnt[cnt[:, :, 0].argmax()][0])
        extTop = tuple(cnt[cnt[:, :, 1].argmin()][0])
        extBot = tuple(cnt[cnt[:, :, 1].argmax()][0])
        cv2.drawContours(tmp_img, [cnt], -1, (0, 255, 255), 2)
        cv2.circle(tmp_img, extLeft, 8, (0, 0, 255), -1)
        cv2.circle(tmp_img, extRight, 8, (0, 255, 0), -1)
        cv2.circle(tmp_img, extTop, 8, (255, 0, 0), -1)
        cv2.circle(tmp_img, extBot, 8, (255, 255, 0), -1)


        print("Corner Points: ", extLeft, extRight, extTop, extBot)


        cv2.imshow('img contour drawn', tmp_img)
        cv2.waitKey(0)
        #cv2.destroyAllWindows()



        ## Perspective Transform
        tmp_img = img.copy()
        pts = np.array([extLeft, extRight, extTop, extBot])
        warped = four_point_transform(tmp_img, pts)
        cv2.imshow("Warped", warped)
        cv2.waitKey(0)


cv2.destroyAllWindows()

참고 문헌

https://docs.opencv.org/4.5.0/dd/d49/tutorial_py_contour_features.html

https://www.pyimagesearch.com/2016/04/11/finding-extreme-points-in-contours-with-opencv/

https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/

fmw42 Nov 16 2020 at 19:35

다음은 Python OpenCV에서 모서리를 찾는 한 가지 방법입니다. 입력의 녹색 점이 문제를 복잡하게하고 입력 이미지에 없을 가능성이 높기 때문에 이것이 더 복잡하다는 점에 유의합니다. 녹색 점을 찾기 위해 cv2.inRange ()를 사용하여 녹색 점에 대한 임계 값을 간단히 지정할 수 있습니다. 그러나 나는 이것이 실제로 당신이 원하는 것이 아니라고 가정 할 것입니다.

 - Read the input
 - Convert to gray
 - Threshold
 - Get the largest contour and draw it on the input
 - Reduce the number of vertices in the contour as a polygon and draw the polygon on the input.
 - The polygon has 5 vertices and two are virtually the same. Normally, one would get 4 verices if the green dots were not there. So draw a white filled polygon on a black background.
 - Get the corners from the white polygon on black background and draw on these vertices
 - Save the results

입력:

import cv2
import numpy as np
import time


# load image
img = cv2.imread("hello.png")

# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# threshold
thresh = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)[1]

# get the largest contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
peri = cv2.arcLength(big_contour, True)

# draw contour on input in red
result = img.copy()
result2 = np.zeros_like(img)
cv2.drawContours(result, [big_contour], 0, (0,0,255), 1)
cv2.drawContours(result2, [big_contour], 0, (0,0,255), 1)

# reduce to fewer vertices on polygon
poly = cv2.approxPolyDP(big_contour, 0.1 * peri, False)

# draw polygon on input in green
cv2.polylines(result, [poly], False, (0,255,0), 1)
cv2.polylines(result2, [poly], False, (0,255,0), 1)

# list polygon points
print("Polygon Points:")
for p in poly:
    px = p[0][0]
    py = p[0][1]
    print(px,py)

print('')

# draw white filled polygon on black background
result3 = np.zeros_like(thresh)
cv2.fillPoly(result3,[poly],255)

# get corners
corners = cv2.goodFeaturesToTrack(result3,4,0.01,50,useHarrisDetector=True,k=0.04)

# print corner coords and draw circles
result3 = cv2.merge([result3,result3,result3])
print("Corners:")
for c in corners:
    x,y = c.ravel()
    print(int(x), int(y))
    cv2.circle(result3,(x,y),3,(0,0,255),-1)

# save result
cv2.imwrite("hello_contours.png", result)
cv2.imwrite("hello_polygon.png", result2)
cv2.imwrite("hello_corners.png", result3)

# display it
cv2.imshow("thresh", thresh)
cv2.imshow("result", result)
cv2.imshow("result2", result2)
cv2.imshow("result3", result3)
cv2.waitKey(0)

입력 이미지의 윤곽 및 다각형 :

검은 색 바탕에 윤곽선과 다각형 :

다각형 정점 :

227 69
41 149
114 284
307 167
228 70

첫 번째와 마지막 꼭짓점은 서로 한 픽셀 내에 있습니다.

검정색 배경에 흰색 다각형의 모서리 :

코너 정점 :

306 167
42 149
114 283
227 69