【发布时间】:2020-06-11 12:21:49
【问题描述】:
我试图找到一张扑克牌的 4 个角的坐标,这样我就可以用它来扭曲卡片的透视图,然后用于识别。我曾尝试使用 cv.boundingRect,但由于我使用的是实时视频源,我只需要卡片,而不需要卡片最近的矩形。现在它使用boundingRect工作,但如果我倾斜图像的角度,坐标是boundingRect而不是扑克牌。看图片: straight angle ... crooked angle
然后我需要扑克牌 4 个角的轮廓坐标,而不是使用 boundingRect。 到目前为止,这是我的方法:
def getContours(img, imgContour, standardimg):
global counter
contours, hierachy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in contours:
area = cv2.contourArea(contour)
areaMin = cv2.getTrackbarPos("area", "parameters")
if area > areaMin:
cv2.drawContours(imgContour, contour, -1, (255, 0, 255), 5)
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.04 * peri, True)
if len(approx) == 4:
x, y, w, h = cv2.boundingRect(approx)
cv2.rectangle(imgContour, (x, y), (x + w, y + h), (255, 255, 0), 3)
cv2.putText(imgContour, "Area: " + str(int(area)), (x + w + 20, y + 45), cv2.FONT_HERSHEY_COMPLEX, 0.7,
(0, 255, 0), 2)
if cv2.waitKey(1) & 0xFF == ord('c'):
counter = counter + 1
cv2.imwrite('warpedPicture' + str(counter) + '.jpg', imgContour)
coordinates.insert(0, x)
coordinates.insert(1, y)
coordinates.insert(2, h)
coordinates.insert(3, w)
warpPicture(coordinates[0], coordinates[1], coordinates[2], coordinates[3], standardimg)
【问题讨论】:
-
您需要捕获卡片图像,以便整个卡片可见。否则透视执行过程将不起作用。还有图片中的白色边框是什么?
标签: python opencv opencv-contour