在对二值化图像的连通域进行分析时,重心,对称轴等参数是重要特征。现对连通域重心计算的python代码实现,归纳总结如下,欢迎大家批评指正。重心是距离对重量的加权平均。用数学语言描述即为:
离散化后为:
对于二值化图像中的连通域,属于均匀物体,每一点像素值相等,所以重心公式如下:
x_i , y_i分别为连通域中像素点的坐标。当然更方便的方法是利用现有的函数接口,代码如下:
import cv2
import numpy as np
from skimage import data,segmentation,measure,morphology,color
import glob
import matplotlib.pyplot as plt
# images = glob.glob("./*.jpg")
images = glob.glob("./inputdata/highlight/*.bmp")
num_img = len(images)
Coord_highlight = np.zeros((4,2,num_img))
CoordInWorld = np.zeros((4,2,num_img))
num_img = 0
for fname in images:
img = cv2.imread(fname)
img_copy = img.copy()
plt.figure()
plt.subplot(221)
plt.imshow(img[:,:,[2,1,0]]) # plt的图像通道和opencv图像通道相反,前者为rgb,后者则为bgr。
plt.title('ori_img')
plt.subplot(222)
plt.imshow(img_copy[:,:,[2,1,0]])
plt.title('ori_img_copy')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray, (9, 9), 0)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(8,8))#圆结构元素
gray = cv2.erode(gray,kernel,iterations = 1)
gray = cv2.dilate(gray,kernel,iterations = 1)
retval, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)#阈值的选择
# method1 利用函数接口
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for index,c in enumerate(contours):
# calculate moments for each contour
M = cv2.moments(c)
# calculate x,y coordinate of center
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cv2.circle(img, (cX, cY), 10, (0, 0, 255), -1)
print(index,cX,cY)
# method2 利用公式计算
label_image = measure.label(thresh) #连通区域标记
print('##########################')
for index,region in enumerate(measure.regionprops(label_image)):
points = region.coords #得到的是按列 行
cX = int(np.mean(points[:,1])) # 求均值即可
cY = int(np.mean(points[:,0]))
cv2.circle(img_copy, (cX, cY), 10, (255, 0, 255), -1)
print(index,cX,cY)
plt.subplot(223)
plt.imshow(img[:,:,[2,1,0]])
plt.title('post_img')
plt.subplot(224)
plt.imshow(img_copy)
plt.title('post_img_copy')
plt.show()
cv2.namedWindow('img',0)
cv2.imshow('img',img)
cv2.namedWindow('img_copy',0)
cv2.imshow('img_copy',img_copy)
cv2.waitKey(0)