本文共 3384 字,大约阅读时间需要 11 分钟。
函数原型:
cv2.findContours(image, mode, method, contours=None, hierarchy=None, offset=None)
mode:cv2.RETR_EXTERNAL 只检测外轮廓cv2.RETR_LIST检测的轮廓不建立等级关系cv2.RETR_CCOMP建立两个等级的轮廓cv2.RETR_TREE建立一个等级树结构的轮廓method:cv2.CHAIN_APPROX_NONE存储所有的轮廓点cv2.CHAIN_APPROX_SIMPLE压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息。
import cv2import numpy as npimg = cv2.imread('shape.jpg') #读取图像gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #转为灰度值图ret, binary = cv2.threshold(gray,220,255,cv2.THRESH_BINARY) #转为二值图contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,\ cv2.CHAIN_APPROX_NONE) #寻找轮廓n=len(contours) #轮廓个数print(n)print(len(contours[0])) #轮廓0像素数目print(len(contours[1])) #轮廓1像素数目print(len(contours[2])) #轮廓2像素数目print(len(contours[3])) #轮廓3像素数目
cv2.imshow("img",img) #显示原图像img2 = cv2.drawContours(img,contours,1,(0,165,255),-1) #绘制轮廓,1表示绘制第几个轮廓cv2.imshow("contours",img2) #显示轮廓cv2.waitKey()cv2.destroyAllWindows()
import cv2import numpy as npimg = cv2.imread('pig.jpg')cv2.imshow("img",img) #显示原图像gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #转为灰度图ret, binary = cv2.threshold(gray,245,255,cv2.THRESH_BINARY_INV) #转为二值图cv2.imshow("binary" ,binary) #显示二值化结果contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)#寻找轮廓mask=np.zeros(img.shape,np.uint8) #生成黑背景,即全为0mask=cv2.drawContours(mask,contours,-1,(255,255,255),-1) #绘制轮廓,形成掩膜cv2.imshow("mask" ,mask) #显示掩膜result=cv2.bitwise_and(img,mask) #按位与操作,得到掩膜区域cv2.imshow("result" ,result) #显示图像中提取掩膜区域cv2.waitKey()cv2.destroyAllWindows()
import cv2import numpy as npimg = cv2.imread('shape.jpg') #读取图像gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #转为灰度值图ret, binary = cv2.threshold(gray,220,255,cv2.THRESH_BINARY) #转为二值图contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,\ cv2.CHAIN_APPROX_NONE) #寻找轮廓n=len(contours) #轮廓个数contoursImg=[]for i in range(n): temp=np.zeros(img.shape,np.uint8) #生成黑背景 contoursImg.append(temp) contoursImg[i]=cv2.drawContours(contoursImg[i],contours,i,(255,255,255), 3) #绘制轮廓 cv2.imshow("contours[" + str(i)+"]",contoursImg[i]) #显示轮廓print("计算图像的矩特征:")for i in range(n): moment=cv2.moments(contours[i]) print(f"轮廓{i}的矩:\n{moment}")cv2.waitKey()cv2.destroyAllWindows()
for i in range(n): area=cv2.moments(contours[i])["m00"] print(f"轮廓{i}的面积:\n{area}")
import cv2import numpy as npimg = cv2.imread('shape.jpg') #读取图像gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #转为灰度值图ret, binary = cv2.threshold(gray,220,255,cv2.THRESH_BINARY) #转为二值图contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,\ cv2.CHAIN_APPROX_NONE) #寻找轮廓n=len(contours) #轮廓个数contoursImg=[]for i in range(n): area = cv2.contourArea(contours[i]) print(f"轮廓{i}的面积:\n{area}")
for i in range(n): length = cv2.arcLength(contours[i], True) #获取轮廓长度 print(f"轮廓{i}的长度:\n{length}") if length<600: temp=np.zeros(img.shape,np.uint8) #生成黑背景 contoursImg.append(temp) contoursImg[i]=cv2.drawContours(contoursImg[i],contours,i,(255,255,255), 3) #绘制轮廓 cv2.imshow("contours[" + str(i)+"]",contoursImg[i]) #显示轮廓cv2.waitKey()cv2.destroyAllWindows(
转载地址:http://ilgof.baihongyu.com/