python-opencv獲取二值圖像輪廓及中心點(diǎn)坐標(biāo)代碼:
成都創(chuàng)新互聯(lián)2013年至今,先為和靜等服務(wù)建站,和靜等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為和靜企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。groundtruth = cv2.imread(groundtruth_path)[:, :, 0] h2, w1 = groundtruth.shape contours, cnt = cv2.findContours(groundtruth.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if len(contours) != 1:#輪廓總數(shù) continue M = cv2.moments(contours[0]) # 計(jì)算第一條輪廓的各階矩,字典形式 center_x = int(M["m10"] / M["m00"]) center_y = int(M["m01"] / M["m00"]) image = np.zeros([h2, w1], dtype=groundtruth.dtype) cv2.drawContours(image, contours, 0, 255, -1)#繪制輪廓,填充 cv2.circle(image, (center_x, center_y), 7, 128, -1)#繪制中心點(diǎn) cv2.imwrite("1.png", image)