Pre-processing before Bolb Detection - MorphClose, Div, Norm & Thresh

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
01 Original.png


morphFromOpenMV.PNG
morph(WithThresholdTrue)FromOpenMV.PNG
find_blobsFromOpenMV.PNG

Please don’t post forum request that are of a format… here’s a bunch of code. Please fix it for me. We don’t have that type of time anymore for customers.

First, what are you trying to do? Detect sharp edges? If so, see the Examples->Image Filtering->Mean_Adaptive_Threshold example. This does basically the first 6 lines of code in one command. After you do that you can find_blobs() on the image.

Mean adaptive thresholding is where the kernel is slid around the image and then we do adaptive thresholding on the center pixel based on it’s difference from the neighbor pixels. The output is a black and white image where sharp lines are white/black and smooth surfaces are black/white (color dependent on the inversion settings).

Noted.
And, thanks a lot for the quick guidance.

Play with the threshold and the constant added to get the best results. The method was made to do exactly what you want to do above however.