九 OpenCV学习笔记——图像轮廓(下)( 二 )


从上述运行结果可以看出:
7. 轮廓的特征值
轮廓自身的一些属性特征及轮廓所包围对象的特征对于描述图像具有重要意义 。本节介绍几个轮廓自身的属性特征及轮廓所包围对象的特征 。
7.1 宽高比
可以使用宽高比()来描述轮廓,例如矩形轮廓的宽高比为:宽高比 = 宽度(Width) / 高度()
import cv2o = cv2.imread('cc.bmp')cv2.imshow("original",o)gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)image,contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)x,y,w,h = cv2.boundingRect(contours[0])cv2.rectangle(o,(x,y),(x+w,y+h),(255,255,255),3)aspectRatio = float(w)/hprint(aspectRatio)cv2.imshow("result",o)cv2.waitKey()cv2.destroyAllWindows()运行后程序显示如下的运行结果:2.1506849315068495
7.2
可以使用轮廓面积与矩形边界(矩形包围框、矩形轮廓)面积之比来描述图像及其轮廓特征 。计算方法为: = 轮廓面积(对象面积) / 矩形边界面积
import cv2o = cv2.imread('cc.bmp')cv2.imshow("original",o)gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)image,contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)x,y,w,h = cv2.boundingRect(contours[0])cv2.drawContours(o,contours[0],-1,(0,0,255),3)cv2.rectangle(o,(x,y),(x+w,y+h),(255,0,0),3)rectArea=w*hcntArea=cv2.contourArea(contours[0])extend=float(cntArea)/rectAreaprint(extend)cv2.imshow("result",o)cv2.waitKey()cv2.destroyAllWindows()运行后程序显示如下的运行结果:0.6717127650292296
7.3
可以使用轮廓面积与凸包面积之比 来衡量图像、轮廓及凸包的特征 。其计算方法为: = 轮廓面积(对象面积) / 凸包面积
import cv2o = cv2.imread('hand.bmp')cv2.imshow("original",o)gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)image,contours, hierarchy =cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)cv2.drawContours(o,contours[0],-1,(0,0,255),3)cntArea=cv2.contourArea(contours[0])hull = cv2.convexHull(contours[0])hullArea = cv2.contourArea(hull)cv2.polylines(o, [hull], True, (0, 255, 0), 2)solidity=float(cntArea)/hullAreaprint(solidity)cv2.imshow("result",o)cv2.waitKey()cv2.destroyAllWindows()运行后程序显示如下的运行结果:0.6752344564084751
7.4 等效直径( )
可以用等效直径来衡量轮廓的特征值,该值是与轮廓面积相等的圆形的直径 。其计算公式
为:等效直径 = 根号(4*轮廓面积/Π)
import cv2import numpy as npo = cv2.imread('cc.bmp')cv2.imshow("original",o)gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)image,contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)cv2.drawContours(o,contours[0],-1,(0,0,255),3)cntArea=cv2.contourArea(contours[0])equiDiameter = np.sqrt(4*cntArea/np.pi)print(equiDiameter)cv2.circle(o,(100,100),int(equiDiameter/2),(0,0,255),3) #展示等直径大小的圆cv2.imshow("result",o)cv2.waitKey()cv2.destroyAllWindows()运行后程序显示如下的运行结果:99.00522529212108
7.5 方向
【九OpenCV学习笔记——图像轮廓(下)】在中,函数cv2.()可以用来构造最优拟合椭圆,还可以在返回值内分别返回椭圆的中心点、轴长、旋转角度等信息 。使用这种形式,能够更直观地获取椭圆的方向等信息 。
(x,y),(MA,ma),angle = cv2.(cnt)
import cv2o = cv2.imread('cc.bmp')cv2.imshow("original",o)gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)image,contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)ellipse = cv2.fitEllipse(contours[0])retval=cv2.fitEllipse(contours[0])print("单个返回值形式:")print("retval=\n",retval)(x,y),(MA,ma),angle = cv2.fitEllipse(contours[0])print("三个返回值形式:")print("(x,y)=(",x,y,")")print("(MA,ma)=(",MA,ma,")")print("angle=",angle)cv2.ellipse(o,ellipse,(0,0,255),2)cv2.imshow("result",o)cv2.waitKey()cv2.destroyAllWindows()运行后程序显示如下的运行结果:单个返回值形式:retval=((276.2112731933594, 139.6067352294922), (63.01350021362305,166.72308349609375), 82.60102844238281)三个返回值形式:(x,y)=( 276.2112731933594 139.6067352294922 )(MA,ma)=( 63.01350021362305 166.72308349609375 )angle= 82.60102844238281