2017-05-25 101 views
1

這是我的代碼,並通過使用此代碼亮點完美檢測如圖所示。但問題是即使現場不存在它會檢測圖像中的虛假斑點可以幫助我如何擺脫這個?如何避免虛假亮點檢測?

# import the necessary packages 
 
import numpy as np 
 
import argparse 
 
import cv2 
 
    
 
# construct the argument parse and parse the arguments 
 
ap = argparse.ArgumentParser() 
 
ap.add_argument("-i", "--image", help = "Desktop") 
 
ap.add_argument("-r", "--radius", type = int, 
 
\t help = "radius of Gaussian blur; must be odd") 
 
args = vars(ap.parse_args()) 
 
    
 
# load the image and convert it to grayscale 
 
image1 = cv2.imread("h.png") 
 
orig = image1.copy() 
 
gray = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) 
 
gray = cv2.GaussianBlur(gray, (args["radius"], args["radius"]), 0) 
 
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray) 
 
image1 = orig.copy() 
 
cv2.circle(image1, maxLoc, args["radius"], (255, 0, 0), 2) 
 
    
 
# display the results of our newly improved method 
 
cv2.imwrite("myImage.png", image1) 
 

 

 
    [1]: https://i.stack.imgur.com/6CDYP.png

回答

0

這是因爲,當你調用cv2.minMaxLoc(gray),它將返回的最大值和最小值在給定矩陣,以及它們的位置,現在你必須照顧並根據您的需要設置最大值。上述方法總是返回一個maxValue,它可以是1,10或255並不重要,所以對於給定的Mat,它將成功找到最亮像素的位置和強度,而不管給定像素是否真亮根據您的期望。對於所期望的行爲,你需要設定一個閾值:

BRIGHT_PIX_THRESH = 220 
if (maxVal > BRIGHT_PIX_THRESH): 
    cv2.circle(image1, maxLoc, 10, (255, 0, 0), 2) 
+0

非常感謝你爲ZdaR您寶貴的建議,我會嘗試,讓你知道 –

+0

即使我使用的檢測閾值假斑點@ ZdaR..what我可不可以做?? –

+0

你可以給你的郵件編號,我會詳細告訴你我是'[email protected]' –