图像的矩
Opencv轮廓矩【判断形态方向、匹配度】_zhuyong006的博客-CSDN博客
从图像中计算出来的矩通常描述了图像不同种类的几何特征如:大小、灰度、方向、形状等,图像矩广泛应用于模式识别、目标分类、目标识别与防伪估计、图像编码与重构等领域。
计算图像的矩
import cv2from pprint import pprintim = cv2.imread('./img2.png')imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(imgray, 244, 255, 0)"""图像的矩可以帮助我们计算图像的质心,面积等。"""M = cv2.moments(thresh)# print(M)pprint(M)# 根据这些矩的值我们可以计算出图像的重心cx = int(M['m10'] / M['m00'])cy = int(M['m01'] / M['m00'])print('图像重心:', cx, cy)
计算轮廓矩
import cv2from pprint import pprintim = cv2.imread('./img2.png')imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(imgray, 244, 255, 0)contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)print('轮廓1的有{}个点组成'.format(len(contours[0])))print('len contours', len(contours))contours2=[cnt for cnt in contours if cv2.contourArea(cnt)>200]#过滤太小的contourprint('过滤太小的contour', len(contours2))"""图像的矩可以帮助我们计算图像的质心, 面积等。"""cnt = contours[0] # 第一个print('第一个轮廓',cnt)M = cv2.moments(cnt)# print(M)pprint(M)# 根据这些矩的值我们可以计算出轮廓的重心cx = int(M['m10'] / M['m00'])cy = int(M['m01'] / M['m00'])print('图像重心:', cx, cy)#轮廓面积area = cv2.contourArea(cnt)print('轮廓面积:', area,M['m00'])# 轮廓周长perimeter = cv2.arcLength(cnt, True)print('轮廓周长:', perimeter)
轮廓近似
import cv2import numpy as npim = cv2.imread('./img2.png')imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(imgray, 244, 255, 0)contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)#寻找最大轮廓area=[]for k in range(len(contours)):area.append(cv2.contourArea(contours[k]))max_dix=np.argmax(np.array(area))#近似轮廓.注意这里只能计算一个轮廓的cnt=contours[max_dix]epsilon = 0.1*cv2.arcLength(cnt,True)print('epsilon:',epsilon)approx = cv2.approxPolyDP(cnt,epsilon,True)print('原始的最大轮廓',len(cnt))print('近似的最大轮廓',len(approx))img1=im.copy()cv2.drawContours(img1,cnt,-1,(255,0,0),-3)cv2.imshow('最大轮廓',img1)img2=im.copy()cv2.drawContours(img2,approx,-1,(255,0,0),-3)cv2.imshow('近似轮廓',img2)img3=im.copy()cv2.drawContours(img3,contours,-1,(255,0,0),-3)cv2.imshow('img3',img3)cv2.waitKey()
边界矩形
import cv2import numpy as npim = cv2.imread('./2.jpg')imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(imgray, 244, 255, 0)contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)#寻找最大轮廓area=[]for k in range(len(contours)):area.append(cv2.contourArea(contours[k]))max_dix=np.argmax(np.array(area))cnt=contours[max_dix]print('原始的最大轮廓',len(cnt))img1=im.copy()#边界矩形x,y,w,h=cv2.boundingRect(cnt)img=cv2.rectangle(im,(x,y),(x+w,y+h),(255,0,0),2)cv2.drawContours(img1,contours,-1,(255,0,0),-3)cv2.imshow('所有轮廓',img1)cv2.imshow('边界矩形',img)cv2.waitKey()
轮廓的性质
# -*- coding: utf-8 -*-import cv2import numpy as npim = cv2.imread('./2.jpg')imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(imgray, 244, 255, 0)contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)#寻找最大轮廓area=[]for k in range(len(contours)):area.append(cv2.contourArea(contours[k]))max_dix=np.argmax(np.array(area))cnt=contours[max_dix]print('原始的最大轮廓',len(cnt))"""轮廓的性质.py:"""# 边界矩形的宽高比x, y, w, h = cv2.boundingRect(cnt)aspect_ratio = float(w) / hprint('边界矩形的长宽比',aspect_ratio)# Extent轮廓面积与边界矩形面积的比。area = cv2.contourArea(cnt)x, y, w, h = cv2.boundingRect(cnt)rect_area = w * hextent = float(area) / rect_areaprint('轮廓面积与边界矩形面积比',area)# Solidity轮廓面积与凸包面积的比。area = cv2.contourArea(cnt)hull = cv2.convexHull(cnt)hull_area = cv2.contourArea(hull)solidity = float(area) / hull_areaprint('轮廓面积与凸包面积比',solidity)# 对象的方向# (x, y), (MA, ma), angle = cv2.fitEllipse(cnt)# Mask and Pixel Points掩模和像素点mask = np.zeros(imgray.shape, np.uint8)# 最大值和最小值及它们的位置min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray, mask=mask)# 平均灰度mean_val = cv2.mean(im, mask=mask)print('平均灰度',mean_val)# 极点 一个对象最上面 最下面 最左 最右 的点。leftmost = tuple(cnt[cnt[:, :, 0].argmin()][0])rightmost = tuple(cnt[cnt[:, :, 0].argmax()][0])topmost = tuple(cnt[cnt[:, :, 1].argmin()][0])bottommost = tuple(cnt[cnt[:, :, 1].argmax()][0])
如果觉得《opencv图像的轮廓特征》对你有帮助,请点赞、收藏,并留下你的观点哦!