opencv approxPolyDP使用

代码:

import cv2
import numpy as np

# img = cv2.imread('/home/sensetime/edgeBoxes-Cpp-version/output/img/000021_10.png', -1) 
# contours,hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img = cv2.imread('/home/sensetime/edgeBoxes-Cpp-version/output/img/000021_10.png')  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)  
contours, hierarchy = cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) 


length = len(contours)
print length
for i in range(length):
    cnt = contours[i]
    epsilon = 0.00001 * cv2.arcLength(cnt,True)
    approx = cv2.approxPolyDP(cnt, epsilon, True)
    # cv2.drawContours(img, approx, -1, (0, 0, 255), 3)
    cv2.polylines(img, [approx], True, (0, 0, 255), 2)
cv2.imshow("approx",img)
cv2.
cv2.waitKey(0)

https://www.jianshu.com/p/d53bdfb1051f

代码里使用drawContours只能画出那些点,不能连成线

import numpy下面注释的两行是直接读取灰度图,然后用findContours函数,这样会报错误,实际上,findContours函数传入的是一个二值图,但没经过处理的灰度图里面的值很多

epsilon这个参数表示的是精度,越小精度越高,因为表示的意思是是原始曲线与近似曲线之间的最大距离

你可能感兴趣的:(python,人工智能,java)