如何检测每个"块的中心"在Python中的图像(矩阵)?

时间:2017-03-27 11:57:32

标签: python image-processing edge-detection

见下图,有9个"块"我希望发现他们的中心

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用openCV:

1)查找对象:

import numpy as np
import cv2
im = cv2.imread('test.jpg')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

reference

2)查找对象的中心:

import cv2
import numpy as np
img = cv2.imread('star.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv2.moments(cnt)

cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00']) 

reference