2016-08-22 28 views
2

首先,現在我正在用Python2.7和Opencv進行斑點檢測。我想要做的是在顏色檢測後完成斑點檢測。我想檢測紅色圓圈(標記),併爲了避免其他斑點干擾,我想先做顏色檢測,然後做斑點檢測。爲什麼不能在這個二進制圖像上做斑點檢測

和顏色檢測後的圖像是binary mask

現在我想這樣做,圖像斑點檢測,但它不工作。 這是我的代碼。

import cv2 
import numpy as np; 

# Read image 
im = cv2.imread("myblob.jpg", cv2.IMREAD_GRAYSCALE) 

# Set up the detector with default parameters. 

params = cv2.SimpleBlobDetector_Params() 
# Change thresholds 
params.minThreshold = 10; # the graylevel of images 
params.maxThreshold = 200; 

params.filterByColor = True 
params.blobColor = 255 

# Filter by Area 
params.filterByArea = False 
params.minArea = 10000 

detector = cv2.SimpleBlobDetector(params) 


# Detect blobs. 
keypoints = detector.detect(im) 

# Draw detected blobs as red circles. 
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob 
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) 

# Show keypoints 
cv2.imshow("Keypoints", im_with_keypoints) 
cv2.waitKey(0)` 

我真的被這個代碼迷惑,因爲它這個圖像white dots 我覺得白點的圖像是用二進制掩碼安靜相似,但爲什麼不能我的二值圖像上做污點檢測?可以在工作任何人告訴我的區別或正確的代碼?

謝謝!

問候, 楠

+1

我需要一些額外的信息來幫助我,我想。爲什麼它可能不起作用的線索可能是斑點的結構。在第一幅圖像中,白色像素並非全部連接到一個大斑點(意味着有幾個單像素「浮動」),而在第二幅圖像中,這些圓形是完美的斑點。 – meetaig

回答

3

它看起來是blob檢測器默認已啓用filterByInertiafilterByConvexity參數。 您可以在您的系統檢查:

import cv2 
params = cv2.SimpleBlobDetector_Params() 
print params.filterByColor 
print params.filterByArea 
print params.filterByCircularity 
print params.filterByInertia 
print params.filterByConvexity 

所以,當你調用detector = cv2.SimpleBlobDetector(params)你實際上也是由慣性和凸度與預設最低和最高值過濾。

如果您明確禁用這些過濾條件:

# Disable unwanted filter criteria params 
params.filterByInertia = False 
params.filterByConvexity = False 

...,然後調用detector = cv2.SimpleBlobDetector(params)你獲得下面的圖片: blobing result

該圖像中的第三個斑點是由白框造成在圖像的右下角。 您可以裁剪圖像,如果幀總是在同一個地方,或者你可以使用參數由圓過濾和刪除不需要的斑點:

params.filterByCircularity = True 
params.minCircularity = 0.1 

而且你最終得到:

enter image description here

2

及其在通過顏色過濾器的OpenCV的錯誤。所有你需要做的是反轉圖像的顏色 - >檢測斑點 - >再次反轉,回到原來的顏色

代碼

import cv2 
import numpy as np; 

# Read image 
im = cv2.imread("myblob.jpg", cv2.IMREAD_GRAYSCALE) 

# Set up the detector with default parameters. 
im=cv2.bitwise_not(im) 

params = cv2.SimpleBlobDetector_Params() 
detector = cv2.SimpleBlobDetector_create(params) 


# Detect blobs. 
keypoints = detector.detect(im) 
im=cv2.bitwise_not(im) 
# Draw detected blobs as red circles. 
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob 
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) 

# Show keypoints 
cv2.imshow("Keypoints", im_with_keypoints) 
cv2.waitKey(0) 

輸出

enter image description here

+0

@ArjitMukherjee upvoting你的,令人印象深刻的技術用於找到斑點。 –

0

@ArjitMukherjee說道最簡單的方法。

但我也附和什麼@meetaig開始評論了關於在兩個圖像

一種原因可能無法正常工作可能是斑點的結構線索斑點的結構差異。 在第一個圖像的白色像素並不是所有連接到大 BLOB(意味着有幾個單像素「漂浮」),而 第二圖像中的圓圈是完美的斑點

您需要微調你的算法,使其適合/與斑點

的不同結構對齊

我有種並快速微調,這可能部分地滿足您的要求:

import cv2 
import numpy as np; 

# Read image 
im = cv2.imread("eRCe1.png", cv2.IMREAD_GRAYSCALE) 
# Set up the detector with default parameters. 

params = cv2.SimpleBlobDetector_Params() 
# Change thresholds 
params.minThreshold = 10; # the graylevel of images 
params.maxThreshold = 200; 

params.filterByColor = True 
params.blobColor = 255 

# Filter by Area 
params.filterByArea = True 
params.minArea = 300 

detector = cv2.SimpleBlobDetector(params) 

# Detect blobs. 
keypoints = detector.detect(im) 

print keypoints 
# Draw detected blobs as red circles. 
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob 
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) 


cv2.imshow("Keypoints", im_with_keypoints) 
cv2.waitKey(0) 

執行上述共同德上,你給兩個相同的圖像,下面是輸出

示例1:

sample #1

示例2:

sample #2

相關問題