I'm trying to get the edges of this object from a TEM(microscope) image and the problem is that the contact is low especially in the upper edge, I tried several things thresholding, contrast equalization... but I wasn't able to get the upper edge.
N.B: I'm trying to calculate the angle between the droplet and the tube I'm not sure if this is the best way to approach this problem.
The original image:
The Canny Edge detection I get:
the steps I got to get this result are:
Code:
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=(grid_size, grid_size))
equ = clahe.apply(img)
val = filters.threshold_otsu(equ)
mask = img < val
# denoising part
mask = filters.gaussian(mask,sigma=sigmaG)
# edge detection
edge = feature.canny(mask,sigma=sigmaC)
edge = img_as_ubyte(edge)
We have this image and we want to detect the edges of the microphone:
Basically, I converted the image to grayscale, added a Gaussian blur, and detected the edges using the canny edge detector. One more important part is to fill in the gaps in the detected edges by dilating the edges and then eroding them.
All of the above is implemented in the process
function; the draw_contours
function basically utilizes the process
function, and detects the greatest contour:
import cv2
import numpy as np
def process(img):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray, (11, 11), 7)
img_canny = cv2.Canny(img_blur, 0, 42)
kernel = np.ones((19, 19))
img_dilate = cv2.dilate(img_canny, kernel, iterations=4)
img_erode = cv2.erode(img_dilate, kernel, iterations=4)
return img_erode
def draw_contours(img):
contours, hierarchies = cv2.findContours(process(img), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cnt = max(contours, key=cv2.contourArea)
peri = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.004 * peri, True)
cv2.drawContours(img, [approx], -1, (255, 255, 0), 2)
img = cv2.imread("image.jpg")
h, w, c = img.shape
img = cv2.resize(img, (w // 2, h // 2))
draw_contours(img)
cv2.imshow("Image", img)
cv2.waitKey(0)
Output:
You can omit the drop by tweaking some values int the process
function. For example, the values
def process(img):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray, (11, 11), 10)
img_canny = cv2.Canny(img_blur, 0, 38)
kernel = np.ones((13, 13))
img_dilate = cv2.dilate(img_canny, kernel, iterations=3)
img_erode = cv2.erode(img_dilate, kernel, iterations=4)
return img_erode
Output: