Detect dots in a image using image processing with python

1.8k Views Asked by At

I need to identify the following dots in the given images. But it doesn't give correct detection. Can someone give a methodology to identify these dots in an image like this? enter image description here

I have done some enhancement to this as follows, enter image description here

  • enhanced image, by dilation followed by sharpened

    I Used template matching for detecting these dots in the image. But it didn't work well. Code is as follows. Is there any other way to detect these?

import cv2 import numpy as np

img_rgb = cv2.imread(file)
cv2.imwrite("D:/4/Detect/"+str(i)+".0.jpg",img_rgb)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

template = cv2.imread('a.jpg',0)
w, h = template.shape[::-1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.455
loc = np.where( res >= threshold)

for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 0)

cv2.imshow('Detected',img_rgb)
cv2.imwrite("D:/4/Detect/"+str(i)+".1.jpg",img_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()
1

There are 1 best solutions below

4
On

Here's the main part of the code for better result (shown in the image below), that I developed quickly:

import cv2 as cv
img = cv.imread('med.jpg',0)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,\
            cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv.THRESH_BINARY,11,2)
titles = ['Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [th2, th3]
inv = ~th3
res = cv.bitwise_and(img,inv)
cv.imshow(titles[1],res)
cv.waitKey(0)
cv.imwrite("result.jpg",res)
cv.destroyAllWindows()

enter image description here

Tune the parameters for desired result.