如何用数字opencv python标记“ y轴”之后的轮廓?

时间:2019-11-14 04:29:29

标签: python opencv label opencv-contour

嗨,我已经为此工作了一段时间,但是我还没有找到任何好的解决方案。 我的愿望是像这张图片一样标记种子表的每个轮廓(我使用MS油漆放置数字):

enter image description here

例如,如果不小心删除了我标记为1的种子,则2的种子仍然是2而不是1:

我用来检测轮廓的代码:

import cv2
import imutils
import numpy as np

img_color = cv2.imread('130634_F0.jpg')
mask = np.zeros(shape = img_color.shape, dtype = "uint8")

cv2.rectangle(img = mask, pt1=(124,56) , pt2= (557,418), color=(255, 255, 255), thickness= -1)
maskedImg = cv2.bitwise_and(src1= img_color, src2= mask)

img_gray = cv2.cvtColor(maskedImg, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(img_gray, (5, 5), 0)

thresh = cv2.threshold(blurred, 75, 255, cv2.THRESH_BINARY)[1]
thresh_copy = thresh.copy()
_contours = cv2.findContours(thresh_copy, cv2.RETR_EXTERNAL,
                                                       cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(_contours)
for c in cnts:
   M = cv2.moments(c)
   cX = int(M["m10"] / M["m00"])
   cY = int(M["m01"] / M["m00"])

   cv2.drawContours(maskedImg, [c], 0, (255, 0, 0), 1)  # blue
   cv2.circle(maskedImg, (cX, cY), 7, (255, 255, 255), -1)
print "# of Spur:", len(cnts)

cv2.imshow("contour", maskedImg)
cv2.waitKey()

非常感谢大家!!
任何想法将不胜感激。 这是原始图片:

enter image description here

0 个答案:

没有答案