python-opencv(13):图像轮廓

使用的函数:

image,contours,hierarchy=cv2.findContours(image,mode,method)
dst=cv2.drawContours(src,contours,contourldx,color[,thickness])
(contourldx为需要绘制的轮廓的边缘索引,若全部绘制则为-1)

 程序示例:

import cv2
import numpy as np
img=cv2.imread("3.png",cv2.IMREAD_UNCHANGED)
img2=cv2.cvtColor(img,cv2.COLOR_RGBA2BGR)
gray=cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret,binary=cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
image,contours,hierarchy=cv2.findContours(gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
middle=img2.copy()
result=cv2.drawContours(middle,contours,-1,(0,0,255),3)

cv2.imshow("original",img)
cv2.imshow("resuly",result)

cv2.waitKey()
cv2.destroyAllWindows()

 结果:

猜你喜欢

转载自blog.csdn.net/Mr_zhuzj/article/details/82223616