美文网首页
opencv算连通域中心

opencv算连通域中心

作者: 发现一个喜悦的地方 | 来源:发表于2020-02-17 13:24 被阅读0次

import cv2
import numpy as np
from matplotlib import pyplot as plt
from collections import Counter
np.random.seed(5)

path='Is0000_validation_lc_0_p3.npy'

label=np.load(path)
c=Counter(label.reshape(-1))

可视化原图

label_colours = np.random.randint(255, size=(100, 3))
a = np.array([label_colours[c % 100] for c in label])
a=a.astype(np.uint8)
plt.imshow(a)

c=np.unique(label)
point=[]
for i in c:
print(i)
l=np.zeros((256,256))
l[label==i]=1
_,contours,hierarchy = cv2.findContours(l.astype(np.uint8), 3, 1)

img = cv2.drawContours(a, contours, -1, (0,255,0), 3)
for j in range(len(contours)):
    cnt = contours[j]
    M = cv2.moments(cnt) 
    cx = int(M['m10']/M['m00']) 
    cy = int(M['m01']/M['m00'])
    point.append((cx,cy))

for p in point:
cv2.circle(a, p, 1, (0, 0, 255), 4)

cv2.namedWindow("image")
cv2.imshow('image', a)
cv2.waitKey (10000) # 显示 10000 ms 即 10s 后消失
cv2.destroyAllWindows()

相关文章

网友评论

      本文标题:opencv算连通域中心

      本文链接:https://www.haomeiwen.com/subject/tuebfhtx.html