2015-11-07 75 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

謝謝,黑斑將現在做,我總是可以採取負面和再次運行算法 –