Hi,
In the following code I ‘pre-process’ an image (“01 Original.pgm”-enclosed) and then run ‘findContours’ in OpenCV; in order to obtain an array holding centroids of all contours:
First six lines of code (Pre-processing)
img = cv2.imread(inputimg,cv2.IMREAD_GRAYSCALE) ##Image-01 Original
kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
close = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel1) ##Image-02 morph
div = np.float32(img)/(close) ##Image-03 div
res = np.uint8(cv2.normalize(div,div,0,255,cv2.NORM_MINMAX)) ##Image-04 norm
(T,thresh1)=cv2.threshold(res,200,255,cv2.THRESH_BINARY) ##Image-05 thresh
Finding Contours
im, contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
drawing = cv2.drawContours(thresh1,contours,-1,(150,150,150),1) ##Image-06 Blocks
cv2.imshow(“06 Blocks”, drawing)
for c in contours:
calculate moments for each contour
M = cv2.moments(c)
calculate x,y coordinate of each center
if M[“m00”] != 0:
cX = int(M[“m10”] / M[“m00”])
cY = int(M[“m01”] / M[“m00”])
else:
cX, cY = 0, 0
XYarr.append([cX, cY])
cv2.circle(drawing, (cX, cY), 1, (255, 255, 255), 1) ##Image-07 BlocksWithCentrePoints
cv2.imshow(“07 BlocksWithCentrePoints”, drawing)
Output images from the 7 stages are compiled into a single file (“SevenCapturesFromOpenCV.PNG”-enclosed).
Efforts put till now:
A.
In OpenMV, I tried ‘find_blobs’ directly on the image (without implementing the first six lines of code of OpenCV; i.e. without ‘pre-processing’):
blobs = img.find_blobs([(80,255)], invert=False, pixels_threshold = 5)
It didn’t work (“find_blobsFromOpenMV.PNG”-enclosed).
B.
In OpenMV, I tried “image.morph”, with and without Threshold. It didn’t work:
kernel_size = 2
kernel = [1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1]
img.morph(kernel_size, kernel, threshold=True) ##(“morph(WithThresholdTrue)FromOpenMV.PNG”-enclosed)
img.morph(kernel_size, kernel) ##(“morphFromOpenMV.PNG”-enclosed")
C.
In OpenMV, I tried “img.close(kernel)”. It throws an exception: “TypeError: can’t convert list to int.”
Can you kindly suggest how to convert the first six lines of above code to work on OpenMV Cam H7 (or M7), please? So that, thereafter, I can run find_blobs on the resultant image.
Or, can you guide with a better approach, please?
Thank you.
Regards.
Raj