您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:OpenCV-Python实现轮廓的特征值

51自学网 2021-10-30 22:29:33
  python
这篇教程OpenCV-Python实现轮廓的特征值写得很实用,希望能帮到您。

前言

轮廓自身的一些属性特征及轮廓所包围对象的特征对于描述图像具有重要意义。本篇博文将介绍几个轮廓自身的属性特征及轮廓包围对象的特征。

宽高比

在轮廓中,我们可以通过宽高比来描述轮廓,例如矩形的轮廓宽高比为:

宽高比=宽度/高度

下面,我们来计算矩形轮廓的宽高比,代码如下:

import cv2img = cv2.imread("26_1.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)x, y, w, h = cv2.boundingRect(contours[0])cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)cv2.imshow("img1", img)aspectRatio=float(w)/hprint(aspectRatio)cv2.waitKey()cv2.destroyAllWindows()

运行之后,我们可以得到轮廓的宽高比约为3:

宽高比

Extend

我们还可以使用轮廓面积与矩形边界面积之比Extend来描述图像及其轮廓特征,数学计算公式图下:

Extend=轮廓面积/矩形边界面积

下面,我们来计算Extend,代码如下:

import cv2img = cv2.imread("26_1.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)x, y, w, h = cv2.boundingRect(contours[0])rectArea=w*h#矩形边界面积cntArea=cv2.contourArea(contours[0])#轮廓面积extend=float(cntArea)/rectAreaprint(extend)

本例中,轮廓面积与矩形边界面积的比值Extend大约为0.8:

0.7

Solidity

我们还可以使用轮廓面积与凸包面积之比Solidity来衡量图像,轮廓以及凸包的特征。其数学计算公式为:

Slidity=轮廓面积/凸包面积

下面,我们来计算Slidity,代码如下:

import cv2img = cv2.imread("26_1.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)x, y, w, h = cv2.boundingRect(contours[0])cntArea=cv2.contourArea(contours[0])#轮廓面积hull=cv2.convexHull(contours[0])hullArea=cv2.contourArea(hull)#凸包面积solidity=float(cntArea)/hullAreaprint(solidity)

运行之后,本例轮廓面积与凸包面积的比值solidity约为1:

比例

等效直径

在OpenCV中,我们还可以使用等效直径来衡量轮廓的特征值,该值是与轮廓面积相等的圆形的直径。其数学计算公式为:

等效直径

下面,我们来计算与轮廓面积相等的圆形直径,代码如下:

import cv2import numpy as npimg = cv2.imread("26_1.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)x, y, w, h = cv2.boundingRect(contours[0])cntArea=cv2.contourArea(contours[0])#轮廓面积equiDiameter=np.sqrt(4*cntArea/np.pi)print(equiDiameter)cv2.circle(img,(100,100),int(equiDiameter/2),(0,255,0),3)cv2.imshow("img1",img)cv2.waitKey()cv2.destroyAllWindows()

运行之后,我们得到其等效直径约为145:

等效直径

方向

在OpenCV中,函数cv2.fitEllipse()可以用来构建最优拟合椭圆,还可以在返回值内分别返回椭圆的中心点,轴长,旋转角度信息。使用这种形式,能够直观地获取椭圆的方向等信息。

函数cv2.fitEllipse()返回值为:

(x,y):椭圆的中心点

(MA,ma):椭圆水平方向轴与垂直方向轴的长度

angle:椭圆的旋转角度

import cv2img = cv2.imread("26_1.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)ellipsis=cv2.fitEllipse(contours[0])(x, y), (MA, ma), angle = cv2.fitEllipse(contours[0])print((x, y), (MA, ma), angle)cv2.ellipse(img, ellipsis, (0, 255, 0), 2)cv2.imshow("img1", img)cv2.waitKey()cv2.destroyAllWindows()

本来就是椭圆图,下面拟合后正好也是椭圆:

椭圆

掩摸和像素点

有时候,我们还像获取某对象的掩摸图像及其对应的点。在OpenCV中,它还提供了cv2.findNonZero()函数用于获取一个图像内的轮廓点位置,其完整定义如下:

def findNonZero(src, idx=None): 

src:要查找非零元素的图像

idx:返回值,表示非0元素的索引位置。具体格式为(行号,列号)

下面,我们实测该函数,代码如下:

import cv2import numpy as npimg = cv2.imread("26_1.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(gray.shape,np.uint8)cv2.drawContours(mask,[contours[0]],0,255,2)pixelpoints=cv2.findNonZero(mask)print(pixelpoints)cv2.imshow("img1", mask)cv2.waitKey()cv2.destroyAllWindows()

运行之后,我们会得到轮廓点位置:

轮廓点位置

最大值,最小值以及它们的位置

在OpenCV中,它提供cv2.minMaxLoc()函数获取指定对象内最大值,最小值以及位置等信息,其完整定义如下:

def minMaxLoc(src, mask=None): 

src:单通道图像

mask:掩摸,通过使用掩摸图像,得到掩膜指定区域内的最值信息

该函数返回4个值:最小值,最大值,最小值位置,最大值位置。

下面,我们来获取这些值,代码如下:

import cv2import numpy as npimg = cv2.imread("26_1.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(gray.shape, np.uint8)cv2.drawContours(mask, [contours[0]], 0, 255, 2)min, max, min_loc, max_loc = cv2.minMaxLoc(gray, mask)print(min, max, min_loc, max_loc)

运行之后,控制台输出4个值:

最小值

平均颜色及平均灰度

在OpenCV中,它给我们提供cv2.mean()函数计算一个对象的平均颜色与平均灰度。其完整定义如下:

def mean(src, mask=None):

参数与上面两个小节一样,这里不在赘述。下面,我们来使用这个函数,代码如下:

import cv2import numpy as npimg = cv2.imread("26_1.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(gray.shape,np.uint8)cv2.drawContours(mask,[contours[0]],0,255,2)mean=cv2.mean(img,mask)

运行之后,输出4个值:RGB以及A通道的均值:

颜色平均值

极点

有时候,我们希望获取某个对象内的极点,比如最左,最右,最上,最下等。在OpenCV中,它给我们提供了以下方法进行获取:

left=tuple(cnt[cnt[:,:,0].argmin()][0])right=tuple(cnt[cnt[:,:,0].argmax()][0])top=tuple(cnt[cnt[:,:,1].argmin()][0])bottom=tuple(cnt[cnt[:,:,1].argmax()][0])

下面,我们来通过这些方法获取,代码如下:

import cv2import numpy as npimg = cv2.imread("26_1.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)cnt = contours[0]left = tuple(cnt[cnt[:, :, 0].argmin()][0])right = tuple(cnt[cnt[:, :, 0].argmax()][0])top = tuple(cnt[cnt[:, :, 1].argmin()][0])bottom = tuple(cnt[cnt[:, :, 1].argmax()][0])print(left, right, top, bottom)font = cv2.FONT_HERSHEY_SIMPLEXcv2.putText(img, "left", left, font, 1, (0, 255, 0), 2)cv2.putText(img, "right", right, font, 1, (0, 255, 0), 2)cv2.putText(img, "top", top, font, 1, (0, 255, 0), 2)cv2.putText(img, "bottom", bottom, font, 1, (0, 255, 0), 2)cv2.imshow("result",img)cv2.waitKey()cv2.destroyAllWindows()

运行之后,值与效果如下:

上下左右

到此这篇关于OpenCV-Python实现轮廓的特征值的文章就介绍到这了,更多相关OpenCV 轮廓的特征值内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net!


再也不用花钱买漫画!Python爬取某漫画的脚本及源码
OpenCV-Python实现怀旧滤镜与连环画滤镜
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。