我想用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)
我尝试了Luke的方法,代码如下:
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)
结果是:The latest picture虽然我使用了一个非常大的阈值(190),即使出现了大量的噪声,代码仍然找不到尾巴。我该如何解决这个问题?所以非常感谢,如果有人能教我如何改进这个简单的边缘检测程序。
发布于 2018-07-27 08:14:17
精子尾巴总是在灰色的背景上呈蓝绿色吗?在这种情况下,您可以使用简单的分段。
首先将图像转换为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)
图像的顺序是:应用蒙版的图像、膨胀的图像蒙版和侵蚀的图像蒙版。
发布于 2018-07-25 08:34:53
对于高频边缘(例如精子尾巴)的边缘检测,有很多有创意的方法。
对于一般的边缘检测,我推荐使用cv2.Canny() --您必须根据自己的应用程序处理输入。
或者,你可以做一个高斯差分,你用两个不同的sigma取cv2.GaussianBlur(),然后取差(https://en.wikipedia.org/wiki/Difference_of_Gaussians),即,
blur1 = cv2.GaussianBlur(im, (5,5), sigmaX_1, sigmaY_1)
blur2 = cv2.GaussianBlur(im, (5,5), sigmaX_2, sigmaY_2)
DoG_edge = blur1 - blur2
最后一种可能性是,你也可以尝试一些直方图操作来增强精子的尾巴。
https://stackoverflow.com/questions/51491771
复制相似问题