1. Code
'''
python One_Spot_Detection.py picture -i 6YuRF.png
python One_Spot_Detection.py camera -c
'''
import cv2
import argparse
import numpy as np
def CVShow(img, title = None, max_h = 950, max_w = 1800):
img = np.array(img)
cv2.namedWindow(title, cv2.WINDOW_KEEPRATIO)
H,W = img.shape[0],img.shape[1]
if H > max_h:
ratio = max_h / H
H, W = int(ratio * H), int(ratio * W)
if W > max_w :
ratio = max_w / W
H, W = int(ratio * H), int(ratio * W)
cv2.resizeWindow(title, W, H)
cv2.imshow(title, img)
key = cv2.waitKey(0)
if key == ord('s'): # wait for key to write or exit
if title == None:title = 'unNamed'
cv2.imwrite(title+'.jpg', img)
cv2.destroyAllWindows()
def SingleImageProcessing(args):
frame = cv2.imread(args.image)
circleRadius, colorRed = 41, (255, 0, 0)
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurFrame = cv2.GaussianBlur(grayFrame, (circleRadius, circleRadius), 0) # Image blurring.
minValue, maxValue, minLocation, maxLocation = cv2.minMaxLoc(blurFrame) # get the brightest pixel in the frame.
cv2.circle(frame, maxLocation, circleRadius, colorRed, 2) # draw circle around the pixel.
CVShow(frame, "frame")
def RealTimeVideoProcessing(args):
# 调取摄像头实时监测
cap = cv2.VideoCapture(0)
circleRadius = 41
colorRed = (255, 0, 0)
while True:
_, frame = cap.read()
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Image blurring.
blurFrame = cv2.GaussianBlur(grayFrame, (circleRadius, circleRadius), 0) # get the brightest pixel in the frame.
(minValue, maxValue, minLocation, maxLocation) = cv2.minMaxLoc(blurFrame) # draw circle around the pixel.
cv2.circle(frame, maxLocation, circleRadius, colorRed, 2)
cv2.imshow("frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
def initiate():
parser = argparse.ArgumentParser(description="Brightest spot detection")
subparser = parser.add_subparsers()
picture = subparser.add_parser('picture', help='Single picture processing')
picture.add_argument('-i', '--image', type=str, required=True, help='image path')
picture.set_defaults(func=SingleImageProcessing)
camera = subparser.add_parser('camera', help='Real time monitoring video processing')
camera.add_argument('-c', '--camera', action='store_true', help='Activate real time monitoring video processing')
camera.set_defaults(func=RealTimeVideoProcessing)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
initiate()
2. Reference
GitHub Link