如何利用Python和OpenCV实现一个很好的边缘检测来标定一些很小的东西

2024-09-30 16:38:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Python和OpenCV实现一个非神经网络的边缘检测来标定一些非常小的东西,比如显微镜下的精子。不幸的是,我发现精子的尾巴很难校准,而且它们与背景非常相似。在

我使用cv2.pyrMeanShiftFiltering()来实现降噪,并使用cv2.findContours()来查找轮廓。结果是这样的:

结果:

result 这是原图:

这是我的代码:

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)

我试过卢克的方法,代码在这里:

^{pr2}$

结果是: The latest picture 虽然我使用了一个非常大的阈值(190),甚至出现了大量的噪音,代码仍然找不到尾部。我怎样才能解决这个问题? 如果有人能教我如何改进这个简单的边缘检测程序,那就非常感谢了。在


Tags: path代码imageimportimgascv2cv
3条回答

有很多创造性的方法可以对高频边缘进行边缘检测(比如精子尾部)

我建议使用cv2.Canny()来进行一般的边缘检测—您必须在特定应用程序的输入上下功夫。在

或者,您可以对gaussian做一个差分,在这里使用cv2.GaussianBlur()和两个不同的sigma,然后取差分(https://en.wikipedia.org/wiki/Difference_of_Gaussians) i、 e

blur1 = cv2.GaussianBlur(im, (5,5), sigmaX_1, sigmaY_1)
blur2 = cv2.GaussianBlur(im, (5,5), sigmaX_2, sigmaY_2)
DoG_edge = blur1 - blur2

最后一种可能性是你也可以尝试一些直方图操作来增强精子的尾部。在

精子的尾巴总是灰蓝色的吗?在这种情况下,可以使用简单的分段。在

首先将图像转换为HSV,如果H值在蓝色/绿色的范围内,则将其标记为前景。在

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

图像依次为:带掩模的图像、扩张的掩模和腐蚀的掩模。在

在执行任何操作之前,您可能需要增强图像。在查看this Wikipedia page时,我遇到了以下用于取消锐化遮罩的公式:

锐化=原始+(原始-模糊)×数量

假设amount = 1,我编写了以下代码片段:

# - resized the image (too big !!!)  -
im = cv2.resize(im, (0, 0), fx = 0.3, fy = 0.3)

# - smoothen the image  -
blur = cv2.GaussianBlur(im, (23, 23), 0)

# - applied the formula assuming amount = 1 -
cv2.imshow('sharpened', cv2.add(im[:,:,1], (im[:,:,1] - blur[:,:,1])))

我得到的是:

enter image description here

现在尾巴更显眼了。使用此选项可以进一步增强您的检测机制。在

相关问题 更多 >