博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenCV+python:轮廓发现与对象测量
阅读量:3948 次
发布时间:2019-05-24

本文共 3836 字,大约阅读时间需要 12 分钟。

1,轮廓发现

当通过阈值分割提取到图像中的目标物体后,就需要通过边缘检测来提取目标物体的轮廓,使用这两种方法基本能够确定物体的边缘或者前景。接下来,通常需要做的是拟合这些边缘的前景,如拟合出包含前景或者边缘像素点的最小外包矩形、圆、凸包等几何形状,为计算它们的面积或者模板匹配等操作打下坚实的基础。

一个轮廓代表一系列的点(像素),这一系列的点构成一个有序的点集,所以可以把一个轮廓理解为一个有序的点集。

轮廓发现是基于图像边缘提取的基础,寻找对象轮廓的方法,所以边缘提取的阈值选定会影响最终轮廓的发现。

2,对象测量
opencv 中轮廓特征包括:如面积,周长,质心,边界框等。
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述3,轮廓发现源代码:

import cv2 as cvimport numpy as npdef edge_demo(image):    blurred = cv.GaussianBlur(image, (9, 9), 5)    gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)    # X Gradient    xgrad = cv.Sobel(gray, cv.CV_16SC1, 1, 0)    # Y Gradient    ygrad = cv.Sobel(gray, cv.CV_16SC1, 0, 1)    #edge    #edge_output = cv.Canny(xgrad, ygrad, 50, 150)    edge_output = cv.Canny(gray, 30, 100)    cv.imshow("Canny Edge", edge_output)    return edge_outputdef contours_demo(image):    dst = cv.GaussianBlur(image, (3, 3), 0)    gray = cv.cvtColor(dst, cv.COLOR_BGR2GRAY)    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)    cv.imshow("binary image", binary)#直接二值化的图像    #binary = edge_demo(image)   #过边缘处理后的图像    #可以是直接二值化的图像也可以是经过边缘处理后的图像(两种方法视情况而定)    #cloneImage, contours, heriachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)    contours, hierarchy = cv.findContours(binary,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)      cv.drawContours(image,contours,-1,(0,0,255),3)     cv.imshow("detect contours", image)src = cv.imread("F:/images/coin.png")cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)cv.imshow("input image", src)contours_demo(src)cv.waitKey(0)cv.destroyAllWindows()

运行结果:

在这里插入图片描述在这里插入图片描述在这里插入图片描述注意:对于cv.findContours:opencv2返回两个值:contours:hierarchy。注:opencv3会返回三个值,分别是img, countours, hierarchy
4,对象测量源代码:

import cv2 as cvimport numpy as npdef measure_object(image):    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)    print("threshold value : %s"%ret)    cv.imshow("binary image", binary)    dst = cv.cvtColor(binary, cv.COLOR_GRAY2BGR)    contours, hierarchy = cv.findContours(binary,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)      for i, contour in enumerate(contours):        area = cv.contourArea(contour)  #轮廓的面积        x, y, w, h = cv.boundingRect(contour) #轮廓的外接矩形        rate = min(w, h)/max(w, h)#轮廓外接矩形的宽高比        print("rectangle rate : %s"%rate)        mm = cv.moments(contour)#求轮廓的几何矩       #print(type(mm))#字典型数据        cx = mm['m10']/mm['m00']#原点的零阶矩        cy = mm['m01']/mm['m00']        cv.circle(dst, (np.int(cx), np.int(cy)), 3, (0, 0, 255), -1)##画出中心点,-1表示填充        #cv.rectangle(dst, (x, y), (x+w, y+h), (0, 0, 255), 2)#绘制轮廓的外接矩形        print("contour area %s"%area)        approxCurve = cv.approxPolyDP(contour,4,True) #多边形逼近 4是与阈值的间隔大小,越小越易找出,True是是否找闭合图像        """        cv.contourArea(contour)      #获取每个轮廓面积        cv.boundingRect(contour)     #获取轮廓的外接矩形        cv.moments(contour)          #求取轮廓的几何距        cv.arcLength(contour,True)  #求取轮廓的周长,指定闭合        approxPolyDP(curve, epsilon, closed, approxCurve=None)       第一个参数curve:输入的点集,直接使用轮廓点集contour       第二个参数epsilon:指定的精度,也即是原始曲线与近似曲线之间的最大距离。       第三个参数closed:若为true,则说明近似曲线是闭合的,反之,若为false,则断开。       第四个参数approxCurve:输出的点集,当前点集是能最小包容指定点集的。画出来即是一个多边形;              print(approxCurve)  #打印每个轮廓的特征点       print(approxCurve.shape)  #打印该点集的shape,第一个数是代表了点的个数,也就是边长连接逼近数        """        print(approxCurve.shape)        if approxCurve.shape[0] > 6:            cv.drawContours(dst, contours, i, (0, 255, 0), 2)        if approxCurve.shape[0] == 4:            cv.drawContours(dst, contours, i, (0, 0, 255), 2)        if approxCurve.shape[0] == 3:            cv.drawContours(dst, contours, i, (255, 0, 0), 2)    cv.imshow("measure-contours", dst)    cv.imshow("measure-contours", dst)src = cv.imread("F:/images/contours.png")cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)cv.imshow("input image", src)measure_object(src)cv.waitKey(0)cv.destroyAllWindows()

运行结果:

在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述

转载地址:http://yahwi.baihongyu.com/

你可能感兴趣的文章
Ubuntu Mysql 安装与配置
查看>>
QT5.12 Mysql驱动未能加载问题
查看>>
现场直击|SequoiaDB@SIGMOD 2021:关注数据库的根科技存储技术
查看>>
赋能政企智慧办公,巨杉数据库与致远互联完成产品互认证
查看>>
SequoiaDB湖仓一体架构亮相 ACM SIGMOD 2021
查看>>
信通院发布第十二批大数据产品能力评测结果,巨杉数据库两款产品通过
查看>>
巨杉数据库荣获2020年度河南省科学技术进步奖
查看>>
湖仓一体提升管理效率 培育数据沃土
查看>>
报名启动!巨杉数据库 2021 湖仓一体技术大赛带你进入分布式技术的星辰大海
查看>>
python的collections
查看>>
J2ME程序开发新手入门九大要点
查看>>
双向搜索算法
查看>>
日本GAME製作方式
查看>>
移动行业术语资料
查看>>
3G到来将全面颠覆SP、CP游戏规则
查看>>
射击游戏中跟踪弹及小角度移动的开发
查看>>
播放声音文件的完整源代码
查看>>
J2ME编程最佳实践之灵活的RMS应用
查看>>
MOBILE FIRST: HOW TO APPROACH MOBILE WEBSITE TESTING? 移动优先:如何处理移动网站测试?
查看>>
开始使用Retrofit 2 HTTP 客户端
查看>>