Search code examples
pythonopencvedge-detection

How to use Python and OpenCV to achieve a well edge detection to calibrate some very small things


I want to use Python and OpenCV to achieve a non-Neural Network edge detection to calibrate some very small things, such as sperms under the microscope. Unfortunately, I found that the sperms' tails are very difficult to calibrate and they're really similar with the background.

I used cv2.pyrMeanShiftFiltering() to achieve noise reduction and used cv2.findContours() to find contours. The result is like that:

result:

result This is the original picture:

Here is my code:

import cv2 as cv
import numpy as np
import os
path = "/home/rafael/Desktop/2.jpg"

def detection(img):
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    #ret, dst = cv.threshold(gray, 200, 255, cv.THRESH_OTSU)
    ret, dst = cv.threshold(gray, 188, 255, cv.THRESH_BINARY_INV)
    return dst

image = cv.imread(path)

img = cv.pyrMeanShiftFiltering(src = image, sp = 5, sr = 40)

dst = detection(img)
src, contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
cv.drawContours(image, contours, -1, (0, 0, 255), 2)
cv.namedWindow('img', cv.WINDOW_NORMAL)
cv.imshow('img', image)
cv.waitKey(0)

I tried Luke's method, and the code is here:

import cv2 as cv
import numpy as np
import os

path = "/home/rafael/Desktop/2.jpg"

def enhance(img):
    img = cv.resize(img, (0, 0), fx = 0.3, fy = 0.3)
    blur = cv.GaussianBlur(img, (23, 23), 0)
    img = cv.add(img[:, :, 1], (img[:, :, 1] - blur[:, :, 1]))
    return img

def detection(img):
    ret, dst = cv.threshold(img, 190, 255, cv.THRESH_BINARY_INV)
    return dst

image = cv.imread(path)
img = enhance(image)
dst = detection(img)

src, contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
cv.drawContours(img, contours, -1, (0, 0, 255), 2)
cv.namedWindow('img', cv.WINDOW_NORMAL)
cv.imshow('img', img)
cv.waitKey(0)

This is the result: The latest picture Although I used a very big threshold(190), even appeared plenty of noises,the code still couldn't find the tails. How can I solve the problem? So thanks a lot if anyone could teach me how to improve this simple edge detection program.


Solution

  • Are the sperm tails always green-blue on a gray background? In that case, you can use simple segmentation.

    First convert the image to HSV, if the H value is in a range for blue/green, mark it as foreground.

    import cv2
    import numpy as np
    
    img = cv2.imread('img.jpg')
    
    hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    lower = np.array([50, 10, 10])
    upper = np.array([120, 255, 255])
    mask = cv2.inRange(hsv, lower, upper)
    res = cv2.bitwise_and(img,img, mask= mask)
    cv2.imwrite('test.jpg', res)
    
    kernel = np.ones((5,5), np.uint8)  # note this is a horizontal kernel
    d_im = cv2.dilate(mask, kernel, iterations=1)
    e_im = cv2.erode(d_im, kernel, iterations=1)
    
    cv2.imwrite('d.jpg', d_im)
    cv2.imwrite('e.jpg', e_im)
    

    "test.jpg", just the image mask applied"d.jpg", image mask with dilationimage mask with dilation and erosion

    Images in order are: image with mask applied, image mask with dilation, and image mask with erosion.