Advertisement

利用cv2.findContours()进行物体轮廓检测

阅读量:

一、语法:contours,hierarchy = cv2.findContours(img,mode,method)

参数说明:img为寻找轮廓的图像,且为二值图(黑白图)。

mode用于描述轮廓提取的方式,并分为四种类型:其中cv2.RETR_EXTERNAL专门用于识别外部边界;而cv2.RETR_LIST方法则不区分内外层轮廓; cv2.RETR_CCOMP则创建两个层次的边界信息——上一层代表外围边界;下一层则记录内部孔洞的边界的详细信息;如果一个孔内部还存在一个连通区域,则该区域对应的边界将位于同一层次; cv2.RETR_TREE方法则建立了这些层次之间的树状结构

该算法用于轮廓的近似提取。其中使用cv2.CHAIN_APPROX_NONE时会记录下所有构成轮廓的像素点。当计算相邻两点之间的距离时,其定义为曼哈顿距离的最大值不超过1个单位(即max(abs(x1-x2), abs(y1-y2)) == 1)。而使用cv2.CHAIN_APPROX_SIMPLE会对水平、垂直以及对角线方向上的像素点进行压缩,并仅保留这些方向上的终点坐标信息。

返回值contours表示图像中的所有轮廓的list,np.array类型。

返回参数中的hierarchy选项用于标识轮廓之间层次关系的内建索引编号。若无对应项,则返回-1。

复制代码
 import cv2

    
 import numpy as np
    
 img = cv2.imread('contours.bmp')    #读取图像
    
 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #转为灰度值图
    
 ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) #转为二值图
    
 contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) #寻找轮廓
    
 cv2.imshow("img",img) #显示原图像
    
 n=len(contours)       #轮廓个数
    
 contoursImg=[]
    
 for i in range(n):
    
     length = cv2.arcLength(contours[i], True)  #获取轮廓长度
    
     area = cv2.contourArea(contours[i])        #获取轮廓面积
    
     print('length['+str(i)+']长度=',length)
    
     print("contours["+str(i)+"]面积=",area)
    
     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()

检测轮廓效果图:

二、实物轮廓检测实例展示

复制代码
 import cv2

    
 import numpy as np
    
 img = cv2.imread('loc3.jpg')
    
 cv2.imshow("img",img)   #显示原图像
    
 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)      #转为灰度图
    
 ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)  #转为二值图
    
 contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)#寻找轮廓
    
 mask=np.zeros(img.shape,np.uint8)  #生成黑背景,即全为0
    
 mask=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 cv2

    
 import numpy as np
    
 img1 = cv2.imread('cc.bmp')
    
 cv2.imshow("img",img1)  #显示原始图像
    
 img2 = np.copy(img1)
    
 img3 = np.copy(img1)
    
 img4 = np.copy(img1)
    
 img5 = np.copy(img1)
    
 gray = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)  #转为灰度值图
    
 ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)   #转为二值图
    
 contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)  #寻找轮廓
    
 x,y,w,h = cv2.boundingRect(contours[0])  #轮廓点
    
 result1 = cv2.rectangle(img1,(x,y),(x+w,y+h),(255,255,255),2)  #构造矩形方框
    
 rect = cv2.minAreaRect(contours[0])  #最小面积方框
    
 points = cv2.boxPoints(rect)
    
 points = np.int0(points)  #取整
    
 result2=cv2.drawContours(img2,[points],0,(255,255,255),2)
    
 (x,y),radius = cv2.minEnclosingCircle(contours[0]) #寻找中心点,半径
    
 center = (int(x),int(y))
    
 radius = int(radius)
    
 result3 = cv2.circle(img3,center,radius,(255,255,255),2)  #绘制圆
    
 ellipse = cv2.fitEllipse(contours[0])   #寻找椭圆
    
 result4 = cv2.ellipse(img4,ellipse,(0,255,0),3)  #绘制椭圆
    
 area,trgl = cv2.minEnclosingTriangle(contours[0])  #寻找三角形
    
 for i in range(0, 3):                               #绘制三角形
    
     result5 = cv2.line(img5, tuple(trgl[i][0]), tuple(trgl[(i + 1) % 3][0]), (255,255,255), 2)
    
  
    
 cv2.imshow("result1",result1) #显示结果图像
    
 cv2.imshow("result2",result2)
    
 cv2.imshow("result3",result3)
    
 cv2.imshow("result4",result4)
    
 cv2.imshow("result5",result5)
    
 cv2.waitKey()
    
 cv2.destroyAllWindows()

效果图:

四、CT异常物体检测实例

复制代码
 import cv2

    
 import numpy as np
    
  
    
 img = cv2.imread('ct.png')
    
 cv2.imshow("img",img)
    
  
    
 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
 ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)  
    
 contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    
 ct=contours[2]            #coutours[0]、coutours[1]是左侧字母R
    
  
    
 mask = np.zeros(img.shape,np.uint8)
    
 mask = cv2.drawContours(mask,[ct],-1,(255,255,255),-1) #生成掩膜
    
 result = cv2.bitwise_and(img,mask)  #按位与,获取掩膜感兴趣区域
    
 cv2.imshow("result",result)
    
 cv2.waitKey()
    
 cv2.destroyAllWindows()

效果图:

全部评论 (0)

还没有任何评论哟~