2015-11-07 74 views
1

我试图根据维基百科的algortihm实现一个日志BLOB探测器执行日志斑点检测: https://en.wikipedia.org/wiki/Blob_detection#The_Laplacian_of_Gaussian在python和OpenCV

我使用Python和OpenCV和我使用的是我得到了一个代码为了制作滤镜,我的代码创建了n级滤镜,使用图像上的滤镜并将所有级别保存在一个阵列中。 然后我寻找当地的最大值,如果我找到一个,我将它标记为一个斑点的中心并围绕它画一个圆。 我设法让它工作,但我得到奇怪的结果,我不知道我做错了什么。

myImage = cv2.imread('fishes.jpg') 
n = 15 
height, width = myImage.shape[:2] 
empty = np.empty((height+2,width+2)).astype(np.uint8) 
imgArray = np.empty((n+2,height+2,width+2)).astype(np.uint8) 
radArray = [] 
imgArray[0] = empty 
imgArray[n+1] = empty 
gray_image = cv2.cvtColor(myImage, cv2.COLOR_BGR2GRAY) 
grayAllChannels = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR) 
sigma = 2 
k = 2**(0.25) 
std2 = float(sigma**2) 
for i in range(n): 
    filt_size = 2*np.ceil(3*sigma)+1 
    radArray.append(filt_size/2) 
    H = log_filt(filt_size, sigma) 
    H *= sigma**2 
    dst = cv2.filter2D(gray_image,-1,H) 
    dst = cv2.copyMakeBorder(dst,1,1,1,1,cv2.BORDER_CONSTANT, value=BLACK) 
    imgArray[i+1] = dst  
    sigma = sigma * k 
    std2 = float(sigma**2) 

i = 0 
for imgIndex in range(1,n+1): 
    for hIndex in range(1, height+1): 
     for wIndex in range(1, width+1): 
      tSlice = imgArray[imgIndex - 1:imgIndex + 2,hIndex - 1:hIndex + 2,wIndex - 1:wIndex + 2] 
      tNum = imgArray[imgIndex,hIndex,wIndex] 
      if (tNum > imgArray[imgIndex - 1,hIndex - 1,wIndex - 1] and 
       tNum > imgArray[imgIndex - 1,hIndex - 1,wIndex] and 
       tNum > imgArray[imgIndex - 1,hIndex - 1,wIndex + 1] and 
       tNum > imgArray[imgIndex - 1,hIndex,wIndex - 1] and 
       tNum > imgArray[imgIndex - 1,hIndex,wIndex] and 
       tNum > imgArray[imgIndex - 1,hIndex,wIndex + 1] and 
       tNum > imgArray[imgIndex - 1,hIndex + 1,wIndex - 1] and 
       tNum > imgArray[imgIndex - 1,hIndex + 1,wIndex] and 
       tNum > imgArray[imgIndex - 1,hIndex + 1,wIndex + 1] and 

       tNum > imgArray[imgIndex,hIndex - 1,wIndex - 1] and 
       tNum > imgArray[imgIndex,hIndex - 1,wIndex] and 
       tNum > imgArray[imgIndex,hIndex - 1,wIndex + 1] and 
       tNum > imgArray[imgIndex,hIndex,wIndex - 1] and 
       tNum > imgArray[imgIndex,hIndex ,wIndex + 1] and 
       tNum > imgArray[imgIndex,hIndex + 1,wIndex - 1] and 
       tNum > imgArray[imgIndex,hIndex + 1,wIndex] and 
       tNum > imgArray[imgIndex,hIndex + 1,wIndex + 1] and 

       tNum > imgArray[imgIndex + 1,hIndex - 1,wIndex - 1] and 
       tNum > imgArray[imgIndex + 1,hIndex - 1,wIndex] and 
       tNum > imgArray[imgIndex + 1,hIndex - 1,wIndex + 1] and 
       tNum > imgArray[imgIndex + 1,hIndex,wIndex - 1] and 
       tNum > imgArray[imgIndex + 1,hIndex,wIndex] and 
       tNum > imgArray[imgIndex + 1,hIndex,wIndex + 1] and 
       tNum > imgArray[imgIndex + 1,hIndex + 1,wIndex - 1] and 
       tNum > imgArray[imgIndex + 1,hIndex + 1,wIndex] and 
       tNum > imgArray[imgIndex + 1,hIndex + 1,wIndex + 1]): 

       cv2.circle(grayAllChannels,(hIndex - 1, wIndex - 1),np.int16(radArray[imgIndex - 1]),(0,30,230),2) 

我得到这个结果:

butterfly

当正确的结果应该是这样的:

correct butterfly

任何想法,我做错了这里?

+0

正确的结果应该是这样的:https://youtu.be/L77m5xuDSKw?t=23m10s - 据我所知,LoG blob检测器只是SIFT算法的一部分。它应该为您提供各种不同尺度的斑点,无需定向。 –

回答

2

是否有可能在某处将列/行分别与y/x混淆(图像右半部分没有斑点)? 如果我猜测,我会在画圈的时候说。我认为你必须通过格式(x, y)的积分,这意味着你必须交换你的价值观。

+0

是的,这是我已经解决的一个问题,但算法结果仍然看起来很奇怪,我会在回家时发布一张新照片 –

+0

完美。还有一个想法:典型的LoG过滤器会导致白色斑点的负值。它看起来像你只搜索最大值。尝试首先计算绝对值(或反转它,如果你只是想要白色斑点)。 – gfkri

+0

谢谢,黑斑将现在做,我总是可以采取负面和再次运行算法 –