01
问题一:寻找靶心
图一
02
问题二:寻找其中的缺失点
图二
解决方法
01
寻找靶心
仔细观察图一,可以看到两个最直接的是靶心有十字交叉线,而在OpenCV形态学处理中,支持十字交叉结构元素,所以我们可以先检测两条线,然后获取十字交叉结构,最后对结构进行轮廓分析,获取中心点,即可获得最终的靶心位置,最终寻找到的靶心位置图示如下:
获取水平与垂直线如下:
获取十字交叉线如下:
代码实现如下:
1image = cv.imread("D:/images/zsxq/cross.jpg") 2cv.imshow("input", image) 3gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) 4ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY_INV) 5se1 = cv.getStructuringElement(cv.MORPH_CROSS, (50, 1)) 6se2 = cv.getStructuringElement(cv.MORPH_CROSS, (1, 50)) 7hline = cv.morphologyEx(binary, cv.MORPH_OPEN, se1) 8vline = cv.morphologyEx(binary, cv.MORPH_OPEN, se2) 9contours, hireachy = cv.findContours(hline, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 10mask = np.zeros_like(hline) 11max = -1 12index = 0 13for cnt in range(len(contours)): 14 x, y, w, h = cv.boundingRect(contours[cnt]) 15 if max < w: 16 max = w 17 index = cnt 18cv.drawContours(mask, contours, index, (255), -1, 8) 19 20cv.imshow("vline", vline) 21contours, hireachy = cv.findContours(vline, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 22max = -1 23index = 0 24for cnt in range(len(contours)): 25 x, y, w, h = cv.boundingRect(contours[cnt]) 26 if max < h and x < int(gray.shape[1]*0.75): 27 max = h 28 index = cnt 29 30cv.drawContours(mask, contours, index, (255), -1, 8) 31cv.imshow("mask", mask) 32 33se3 = cv.getStructuringElement(cv.MORPH_CROSS, (13, 13)) 34mask = cv.morphologyEx(mask, cv.MORPH_OPEN, se3) 35cv.imshow("corss", mask) 36contours, hireachy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 37for cnt in range(len(contours)): 38 x, y, w, h = cv.boundingRect(contours[cnt]) 39 print(x, y, w, h) 40 cx = (x + w//2) 41 cy = (y + h//2) 42 cv.circle(image, (cx, cy), 4, (0, 0, 255), 4, 8, 0) 43cv.imshow("result", image) 44cv.imwrite("D:/find_cross.png", image) 45cv.waitKey(0) 46cv.destroyAllWindows()
02
寻找缺失
仔细观察图二,缺失是偶发情况,针对这种情况下,要完成计数与缺失位置标定!我感觉我的密集恐惧症已经开始犯了!首先需要获取这些位置,通过二值话与轮廓发现搞定,然后根据这些轮廓位置,重新绘制统一的圆形标记,轮廓发现对每个圆形标记进行上下左右位置最近领搜索,返回间隔距离,-1表示边界,根据间隔距离设置阈值查找缺失,最终运行结果如下:
从原图得到的标记图如下:
代码实现如下:
1image = cv.imread("D:/images/zsxq/zsxq_40.png") 2gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) 3ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY_INV) 4cv.imshow("binary", binary) 5contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 6mask = np.zeros_like(binary) 7for cnt in range(len(contours)): 8 area = cv.contourArea(contours[cnt]) 9 if area < 50: 10 continue 11 x, y, w, h = cv.boundingRect(contours[cnt]) 12 if (y + h) > (binary.shape[0] - 10): 13 continue 14 cx = (x + w//2) 15 cy = (y + h//2) 16 cv.circle(mask, (cx, cy), 4, (255), 4, 8, 0) 17cv.imshow("mask", mask) 18contours, hireachy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 19for cnt in range(len(contours)): 20 x, y, w, h = cv.boundingRect(contours[cnt]) 21 cx = (x + w//2) 22 cy = (y + h//2) 23 left = find_neighborhood(mask, cx, cy, 1) 24 right = find_neighborhood(mask, cx, cy, 2) 25 # top = find_neighborhood(mask, cx, cy, 3) 26 # bottom = find_neighborhood(mask, cx, cy, 4) 27 if left == -1 or right == -1: # or top == -1 or bottom == -1: 28 continue 29 dx = right - left 30 # dy = top - bottom 31 # print(dx, dy) 32 if dx > 15: 33 cv.circle(image, (cx + left + 10, cy), 4, (0, 0, 255), 4, 8, 0) 34 35cv.imshow("test", image) 36cv.imwrite("D:/find_miss.png", image) 37cv.waitKey(0) 38cv.destroyAllWindows()
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !