本文共 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/