找到图像中最亮的点

时间:2016-08-08 13:44:50

标签: python windows python-2.7 numpy simplecv

所以我试图找到图像中最亮的点并获得它的坐标。 我已经找到了一个预编写的代码,但是它不能正常工作,我想知道是否有人可以给我一个解决方案(我在编程方面不是太先进)。 此外,我使用的是跟踪对象的视频,而不仅仅是照片。

这是我尝试使用的代码:

# import the necessary packages
import SimpleCV
import numpy as np
import argparse
import cv2


display = SimpleCV.Display()
cam = SimpleCV.Camera()
img = cv2.imread(args["image"]).flipHorizontal().toGray()

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image file")
ap.add_argument("-r", "--radius", type = int,
     help = "radius of Gaussian blur; must be odd")
args = vars(ap.parse_args())

# perform a naive attempt to find the (x, y) coordinates of
# the area of the image with the largest intensity value
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
cv2.circle(image, maxLoc, 5, (255, 0, 0), 2)

# display the results of the naive attempt
cv2.imshow("Naive", image)

# apply a Gaussian blur to the image then find the brightest
# region
gray = cv2.GaussianBlur(gray, (args["radius"], args["radius"]), 0)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
image = orig.copy()
cv2.circle(image, maxLoc, args["radius"], (255, 0, 0), 2)

# display the results of our newly improved method
cv2.imshow("Robust", image)
cv2.waitKey(0)

但是如果我试图运行它,我得到的错误是:

NameError: the name 'args' is not defined

这是仅使用照片的原始代码:

# 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 = "path to the image file")
 ap.add_argument("-r", "--radius", type = int,
    help = "radius of Gaussian blur; must be odd")
 args = vars(ap.parse_args())

 # load the image and convert it to grayscale
 image = cv2.imread(args["image"])
 orig = image.copy()
 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

 # perform a naive attempt to find the (x, y) coordinates of
 # the area of the image with the largest intensity value
 (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
 cv2.circle(image, maxLoc, 5, (255, 0, 0), 2)

 # display the results of the naive attempt
 cv2.imshow("Naive", image)

# apply a Gaussian blur to the image then find the brightest
# region
gray = cv2.GaussianBlur(gray, (args["radius"], args["radius"]), 0)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
image = orig.copy()
cv2.circle(image, maxLoc, args["radius"], (255, 0, 0), 2)

# display the results of our newly improved method
cv2.imshow("Robust", image)
cv2.waitKey(0)

0 个答案:

没有答案