1. 程式人生 > 程式設計 >python-opencv獲取二值影象輪廓及中心點座標的程式碼

python-opencv獲取二值影象輪廓及中心點座標的程式碼

python-opencv獲取二值影象輪廓及中心點座標程式碼:

groundtruth = cv2.imread(groundtruth_path)[:,:,0]
h1,w1 = groundtruth.shape
contours,cnt = cv2.findContours(groundtruth.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
if len(contours) != 1:#輪廓總數
 continue
M = cv2.moments(contours[0]) # 計算第一條輪廓的各階矩,字典形式
center_x = int(M["m10"] / M["m00"])
center_y = int(M["m01"] / M["m00"])
image = np.zeros([h1,w1],dtype=groundtruth.dtype)
cv2.drawContours(image,contours,255,-1)#繪製輪廓,填充
cv2.circle(image,(center_x,center_y),7,128,-1)#繪製中心點
cv2.imwrite("1.png",image)

以上這篇python-opencv獲取二值影象輪廓及中心點座標的程式碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。