我想用Python和OpenCV实现一个非神经网络的边缘检测来标定一些非常小的东西,比如显微镜下的精子。不幸的是,我发现精子的尾巴很难校准,而且它们与背景非常相似。在
我使用cv2.pyrMeanShiftFiltering()
来实现降噪,并使用cv2.findContours()
来查找轮廓。结果是这样的:
结果:
这是原图:
这是我的代码:
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),甚至出现了大量的噪音,代码仍然找不到尾部。我怎样才能解决这个问题? 如果有人能教我如何改进这个简单的边缘检测程序,那就非常感谢了。在
有很多创造性的方法可以对高频边缘进行边缘检测(比如精子尾部)
我建议使用cv2.Canny()来进行一般的边缘检测—您必须在特定应用程序的输入上下功夫。在
或者,您可以对gaussian做一个差分,在这里使用cv2.GaussianBlur()和两个不同的sigma,然后取差分(https://en.wikipedia.org/wiki/Difference_of_Gaussians) i、 e
最后一种可能性是你也可以尝试一些直方图操作来增强精子的尾部。在
精子的尾巴总是灰蓝色的吗?在这种情况下,可以使用简单的分段。在
首先将图像转换为HSV,如果H值在蓝色/绿色的范围内,则将其标记为前景。在
图像依次为:带掩模的图像、扩张的掩模和腐蚀的掩模。在
在执行任何操作之前,您可能需要增强图像。在查看this Wikipedia page时,我遇到了以下用于取消锐化遮罩的公式:
锐化=原始+(原始-模糊)×数量
假设
amount = 1
,我编写了以下代码片段:我得到的是:
现在尾巴更显眼了。使用此选项可以进一步增强您的检测机制。在
相关问题 更多 >
编程相关推荐