拍摄图像进行裁剪时,停止工作并停止
while True:
_, frame = cap.read()
clone = frame.copy()
cv2.namedWindow("image")
cv2.setMouseCallback("image", shape_selection)
# display the image and wait for a keypress
cv2.imshow("image", frame)
cv2.imshow("clone",clone)
key = cv2.waitKey(1) & 0xFF
if key == ord("r"):
image = clone.copy()
elif key == ord("c"):
break
if len(ref_point) == 2:
crop_img = clone[ref_point[0][1]:ref_point[1][1], ref_point[0][0]:ref_point[1][0]]
cv2.imshow("crop_img", crop_img)
cv2.imwrite(filename='saved_img.jpg', img=crop_img)
cv2.waitKey(0)
# close all open windows
cv2.destroyAllWindows()
这是我选择形状的功能 def shape_selection(事件,x,y,标志,参数): #获取对全局变量的引用 全局ref_point,裁剪
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
ref_point = [(x, y)]
cropping = True
#cv2.imshow("image", clone)
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
ref_point.append((x, y))
cropping = False
# draw a rectangle around the region of interest
cv2.rectangle(clone, ref_point[0], ref_point[1], (0, 255, 0), 2)
#cv2.imshow("image", clone)
答案 0 :(得分:0)
import argparse
import cv2
from threading import Thread
# initialize the list of reference points and boolean indicating
# whether cropping is being performed or not
ref_point = []
cropping = False
def shape_selection(event, x, y, flags, param):
# grab references to the global variables
global ref_point, cropping
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
ref_point = [(x, y)]
cropping = True
#cv2.imshow("image", clone)
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
ref_point.append((x, y))
cropping = False
crop_img = clone[ref_point[0][1]:ref_point[1][1], ref_point[0][0]:ref_point[1][0]]
cv2.imshow("crop_img", crop_img)
cv2.imwrite(filename='saved_img.jpg', img=crop_img)
# draw a rectangle around the region of interest
cv2.rectangle(clone, ref_point[0], ref_point[1], (0, 255, 0), 2)
#cv2.imshow("image", clone)
# load the image, clone it, and setup the mouse callback function
cap = cv2.VideoCapture(0)
# keep looping until the 'q' key is pressed
while True:
_, frame = cap.read()
clone = frame.copy()
cv2.namedWindow("image")
#cv2.setMouseCallback("image", shape_selection)
# display the image and wait for a keypress
cv2.imshow("image", frame)
cv2.imshow("clone",clone)
key = cv2.waitKey(1) & 0xFF
cv2.destroyAllWindows()