我正在使用OpenCV来跟踪带有网络摄像头的镜头,我结合了在互联网上找到的两个脚本,但是现在我想拍摄一个png图像,并将其调整大小到所跟踪的圆的中心并缩放与边界圆跟踪成比例。
我尝试了崇高的机器人,胡须教程,但是我对感兴趣的区域和调整图像大小有些迷惑,任何帮助将不胜感激。
import numpy as np
import cv2
import time
import imutils
import math
import os
uniImg = cv2.imread('unicorn1.png')
def nothing(*arg):
pass
cap = cv2.VideoCapture(0)
#Initial HSV GUI slider values to load on program start.
icol = (36, 202, 59, 71, 255, 255) # Green
icol = (18, 0, 196, 36, 255, 255) # Yellow
icol = (89, 0, 0, 125, 255, 255) # Blue
icol = (0, 100, 80, 10, 255, 255) # Red
icol = (104, 117, 222, 121, 255, 255) # test
icol = (0, 0, 0, 255, 255, 255) # New start
icol2 = (0, 0, 0, 255, 255, 20) # New start
icol3 = (0, 0, 0, 255, 255, 20) # New start
cv2.namedWindow('colorTest')
# Lower range colour sliders.
cv2.createTrackbar('lowHue', 'colorTest', icol[0], 255, nothing)
cv2.createTrackbar('lowSat', 'colorTest', icol[1], 255, nothing)
cv2.createTrackbar('lowVal', 'colorTest', icol[2], 255, nothing)
# Higher range colour sliders.
cv2.createTrackbar('highHue', 'colorTest', icol[3], 255, nothing)
cv2.createTrackbar('highSat', 'colorTest', icol[4], 255, nothing)
cv2.createTrackbar('highVal', 'colorTest', icol[5], 255, nothing)#
cv2.createTrackbar('erode','colorTest',icol2[5], 20, nothing)
cv2.createTrackbar('dilate','colorTest',icol3[5], 20, nothing)
while(1):
timeCheck = time.time()
lowHue = cv2.getTrackbarPos('lowHue', 'colorTest')
lowSat = cv2.getTrackbarPos('lowSat', 'colorTest')
lowVal = cv2.getTrackbarPos('lowVal', 'colorTest')
highHue = cv2.getTrackbarPos('highHue', 'colorTest')
highSat = cv2.getTrackbarPos('highSat', 'colorTest')
highVal = cv2.getTrackbarPos('highVal', 'colorTest')
erode = cv2.getTrackbarPos('erode', 'colorTest')
dilate = cv2.getTrackbarPos('dilate', 'colorTest')
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Get HSV values from the GUI sliders.
# define range of white color in HSV
# change it according to your need !
colorLow = np.array([lowHue,lowSat,lowVal])
colorHigh = np.array([highHue,highSat,highVal])
# Threshold the HSV image to get only white colors
mask = cv2.inRange(hsv, colorLow, colorHigh)
mask = cv2.erode(mask, None, iterations= erode)
mask = cv2.dilate(mask, None, iterations= dilate)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
# find contours in the mask and initialize the current
# (x, y) center of the ball
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
center = None
# only proceed if at least one contour was found
if len(cnts) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
# only proceed if the radius meets a minimum size
if radius > 10:
# draw the circle and centroid on the frame,
# then update the list of tracked points
cir = cv2.circle(frame, (int(x), int(y)), int(radius),
(0, 255, 255), 2)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break