2017-05-25 100 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]' –