检测到绘制轮廓后如何绘制直线中心线?

时间:2020-03-01 01:17:34

标签: python opencv

我计划在检测到cv2.drawContour之后绘制中心线并将其计算为mm(毫米)?在Opencv Python中可以吗?

Image

This is the result I want and it will give the millimeter.

img = cv2.imread('leaf4.jpg')
blur1 = cv2.GaussianBlur(img,(3,3),1)

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_blue = np.array([5, 25, 25])
upper_blue = np.array([70, 255, 255])
thresh = cv2.inRange(hsv, lower_blue, upper_blue)

imask = thresh>0
green = np.zeros_like(img, np.uint8)
green[imask] = img[imask]

#Guassian blur
blur = cv2.GaussianBlur(img,(11,11),1)


#Canny-edge detection
canny = cv2.Canny(blur, 160, 290)
canny = cv2.cvtColor(canny,cv2.COLOR_GRAY2BGR)

# Find contours and sort for largest contour
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)


for c in cnts:
    x, y, w, h = cv2.boundingRect(c)

    #Draw rectangle
    cv2.rectangle(green, (x, y), (x + w, y + h), (36, 255, 12), 2)
    cv2.putText(green, "w={},h={}".format(w, h), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36, 255, 12), 2)
    cv2.drawContours(green, [c], -1, (1, 55, 255),3)
    break

0 个答案:

没有答案
相关问题