opencv图像的轮廓特征

图像的矩

Opencv轮廓矩【判断形态方向、匹配度】_zhuyong006的博客-CSDN博客

从图像中计算出来的矩通常描述了图像不同种类的几何特征如:大小、灰度、方向、形状等,图像矩广泛应用于模式识别、目标分类、目标识别与防伪估计、图像编码与重构等领域。

计算图像的矩

import cv2
from pprint import pprint
im = 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 cv2
from pprint import pprint
im = 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]#过滤太小的contour
print('过滤太小的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 cv2
import numpy as np
im = 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 cv2
import numpy as np
im = 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 cv2
import numpy as np
im = 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) / h
print('边界矩形的长宽比',aspect_ratio)
# Extent轮廓面积与边界矩形面积的比。
area = cv2.contourArea(cnt)
x, y, w, h = cv2.boundingRect(cnt)
rect_area = w * h
extent = float(area) / rect_area
print('轮廓面积与边界矩形面积比',area)

# Solidity轮廓面积与凸包面积的比。
area = cv2.contourArea(cnt)
hull = cv2.convexHull(cnt)
hull_area = cv2.contourArea(hull)
solidity = float(area) / hull_area
print('轮廓面积与凸包面积比',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])

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一壶浊酒..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值