cv2.findContours()函数学习

2023-07-05  本文已影响0人  大龙10

一、轮廓检测

contours, hierarchy = cv2.findContours(image,mode,method)

注意:cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图),所以读取的图像要先转成灰度的,再转成二值图。

二、轮廓的绘制

cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None)

三、示例

import numpy as np
import cv2

kernel = np.ones((1, 5), np.uint8)
img = cv2.imread("D:\\mytest.jpg")  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)  
binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel, anchor=(2, 0), iterations=5)
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)  
cv2.drawContours(img,contours,-1,(255,0,0),3)  
cv2.imshow("img", img)  
cv2.waitKey(0)  

四、资料

「岁月蹉跎的一杯酒」的博客:
https://blog.csdn.net/weixin_44690935/article/details/109008946

上一篇下一篇

猜你喜欢

热点阅读