【发布时间】:2019-07-24 14:22:43
【问题描述】:
我需要在python上找到一个围棋棋盘并用opencv2检测照片上的筹码,但是现在我的棋盘检测有问题,同一轮廓上有奇怪的点,我不明白,我该怎么做删除它们。这就是我现在所拥有的:
from skimage import exposure
import numpy as np
import argparse
import imutils
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-r", required = True,
help = "ratio", type=int, default = 800)
args = vars(ap.parse_args())
img = cv2.imread('3.jpg') #upload image and change resolution
ratio = img.shape[0] / args["r"]
orig = img.copy()
img = imutils.resize(img, height = args["r"])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)
cnts= cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) #search contours and sorting them
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None
for cnt in cnts:
rect = cv2.minAreaRect(cnt) # try to fit each contour in rectangle
box = cv2.boxPoints(rect)
box = np.int0(box)
area = int(rect[1][0]*rect[1][1]) # calculating contour area
if (area > 300000):
print(area)
cv2.drawContours(img, cnt, -1, (255, 0, 0), 4) #dots in contour
hull = cv2.convexHull(cnt) # calculating convex hull
cv2.drawContours(img, [hull], -1, (0, 0, 255), 3)
cv2.imshow("death", img)
cv2.waitKey(0)
来源
结果
【问题讨论】:
-
尝试根据那里的区域对轮廓进行排序,然后绘制第一个轮廓(这将是最大的)。 this might help
-
@Rick 他已经在
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]这样做了 -
@zteffi 没错,我的错。感谢您指出。那我看不出问题。 OP 可以只绘制第一个轮廓并检查
-
@Georgy 你是在问蓝色点代表什么,为什么 Canny 检测板外边缘,或者建议如何过滤板外边缘?
-
@Georgy 你能添加你的原始输入图像吗?
标签: python image opencv image-processing computer-vision