See the image below, there are 9 "blocks" and I wish to detect their centers
Asked
Active
Viewed 810 times
-1
-
Detect the edges and then do algebra? What have you tried? – John Coleman Mar 27 '17 at 12:03
-
Do you mean the pixel with the largest value or the "centre of mass" of the bright events? – DavidG Mar 27 '17 at 12:09
-
Hi John, this image is the extracted features by CNN and I wanna to detect the center in order for automatic annotation for original images. – Venn Mar 27 '17 at 12:19
-
Hi David, yeah that's true. – Venn Mar 27 '17 at 12:20
1 Answers
1
You can use openCV:
1) Find objects:
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)
2) Find centers of the object:
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'])

Václav Struhár
- 1,739
- 13
- 21
-
It seems that you forgot this line in the second section: imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) – KBill Nov 01 '19 at 11:36