opencv+python取某区域中心点

import numpy as np

from scipy import ndimage
import cv2
img=cv2.imread(r"c.png")
groundtruth = img[:, :, 0]
# print(groundtruth.shape)(768, 1024)
h1, w1 = groundtruth.shape
contours, cnt = cv2.findContours(groundtruth, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
image = np.zeros([h1, w1])
X_center=[]
Y_center=[]
for i in range(len(contours)):
    M = cv2.moments(contours[i])  # 计算第一条轮廓的各阶矩,字典形式
    center_x = int(M["m10"] / M["m00"])
    center_y = int(M["m01"] / M["m00"])
    X_center.append(center_x)
    Y_center.append(center_y)
    cv2.drawContours(image, contours, i, 255, -1)  # 绘制轮廓,填充
    m_image=cv2.circle(image, (center_x, center_y), 7, 128, -1)  # 绘制中心点
cv2.imwrite("e.png", image)

原图

opencv+python取某区域中心点_第1张图片

 取区域中心点的图 

opencv+python取某区域中心点_第2张图片

 

你可能感兴趣的:(opencv,python,计算机视觉,图像处理)