我需要检测翅膀蜜蜂的静脉连接(图像只是一个例子)。我使用opencv - python。
ps:也许图像质量下降了一点,但图像都是用一个像素宽连接的。
发布于 2019-07-21 07:29:19
这是一个有趣的问题。我得到的结果并不完美,但这可能是一个很好的开始。我用一个只看内核边缘的内核对图像进行了过滤。这个想法是,一个交叉点至少有3条穿过内核边缘的线,而普通的线只有2条。这意味着当内核超过交叉点时,结果值将更高,因此阈值将显示它们。
由于这些行的性质,存在一些正值和一些假阴性。一个关节很可能会被多次找到,所以你必须考虑到这一点。您可以通过绘制小点并检测这些点来使它们独一无二。
结果:
代码:
import cv2
import numpy as np
# load the image as grayscale
img = cv2.imread('xqXid.png',0)
# make a copy to display result
im_or = img.copy()
# convert image to larger datatyoe
img.astype(np.int32)
# create kernel
kernel = np.ones((7,7))
kernel[2:5,2:5] = 0
print(kernel)
#apply kernel
res = cv2.filter2D(img,3,kernel)
# filter results
loc = np.where(res > 2800)
print(len(loc[0]))
#draw circles on found locations
for x in range(len(loc[0])):
cv2.circle(im_or,(loc[1][x],loc[0][x]),10,(127),5)
#display result
cv2.imshow('Result',im_or)
cv2.waitKey(0)
cv2.destroyAllWindows()
注意:您可以尝试调整内核和阈值。例如,使用上面的代码,我得到了126个匹配。但是当我使用
kernel = np.ones((5,5))
kernel[1:4,1:4] = 0
使用阈值
loc = np.where(res > 1550)
我在这些地方找到了33个匹配的:
发布于 2019-07-21 14:47:12
在上图中,你可以使用Harris corner detector
算法来检测静脉交界处。与以前的技术相比,Harris corner detector
直接考虑了角点分数与方向的差异,而不是每45度角使用移位补丁,并且已被证明在区分边和角方面更准确(来源:wikipedia)。
代码:
img = cv2.imread('wings-bee.png')
# convert image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
'''
args:
img - Input image, it should be grayscale and float32 type.
blockSize - It is the size of neighbourhood considered for corner detection
ksize - Aperture parameter of Sobel derivative used.
k - Harris detector free parameter in the equation.
'''
dst = cv2.cornerHarris(gray, 9, 5, 0.04)
# result is dilated for marking the corners
dst = cv2.dilate(dst,None)
# Threshold for an optimal value, it may vary depending on the image.
img_thresh = cv2.threshold(dst, 0.32*dst.max(), 255, 0)[1]
img_thresh = np.uint8(img_thresh)
# get the matrix with the x and y locations of each centroid
centroids = cv2.connectedComponentsWithStats(img_thresh)[3]
stop_criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# refine corner coordinates to subpixel accuracy
corners = cv2.cornerSubPix(gray, np.float32(centroids), (5,5), (-1,-1), stop_criteria)
for i in range(1, len(corners)):
#print(corners[i])
cv2.circle(img, (int(corners[i,0]), int(corners[i,1])), 5, (0,255,0), 2)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
您可以从here查看Harris Corner detector
算法背后的理论。
https://stackoverflow.com/questions/57128882
复制相似问题