import cv2
import tkinter as tk
import numpy as np
from numpy.ma.core import concatenate
# 读取摄像头
fps = 30
cap = cv2.VideoCapture(0) # 0通常代表默认摄像头
cap.set(cv2.CAP_PROP_FPS, fps)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
codes = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
cap.set(cv2.CAP_PROP_FOURCC, codes)
window_width = 640
window_height = 480
def handle_scroll(event, x, y, flags, param):
global width
global height
scale_factor = 1
if event == cv2.EVENT_MOUSEWHEEL:
if flags > 0:
scale_factor = 2 # 缩放增加10%
elif flags < 0:
scale_factor = 0.5 # 缩放减小10%
new_width = int(width * scale_factor)
new_height = int(height * scale_factor)
if new_width > window_width * 50 or new_height > window_height * 50:
print('Image Too Large!')
elif new_width < window_width or new_height < window_height:
print('Image Too Small!')
elif current_size is not None:
width = new_width
height = new_height
print(width, 'and', height)
if not cap.isOpened():
print("Error opening camera")
exit()
raw_frame = cap.read()[1]
current_size = raw_frame.shape[:2]
raw_width = width = int(current_size[1])
raw_height = height = int(current_size[0])
def image_init():
global current_size
print(raw_width, raw_height)
raw_image = cv2.resize(raw_frame, (raw_width, raw_height), cv2.INTER_CUBIC)
raw_cropped_image = raw_image[
int(raw_height / 2) - int(window_height / 2):int(raw_height / 2) + int(window_height / 2),
int(raw_width / 2) - int(window_width / 2):int(raw_width / 2) + int(window_width / 2)]
return raw_cropped_image
def single_cam():
cv2.namedWindow('Camera View', cv2.WINDOW_AUTOSIZE)
frames = []
while True:
ret = cap.read() # 读取一帧图像
tmp_frame = cap.read()[1]
frames.append(tmp_frame)
if not ret:
print("Error reading from camera")
break
if len(frames) == 6:
frames.pop(0)
frame = np.mean(frames, axis=0).astype(np.uint8)
# 使用鼠标滚轮事件处理函数
cv2.setMouseCallback('Camera View', handle_scroll)
# 对中心区域缩放
resized_img = cv2.resize(frame, (width, height), cv2.INTER_LINEAR)
cropped_img = resized_img[int(height / 2) - int(window_height / 2):int(height / 2) + int(window_height / 2),
int(width / 2) - int(window_width / 2):int(width / 2) + int(window_width / 2)]
cv2.imshow('Camera View', cropped_img)
key = cv2.waitKey(1) & 0xFF
# 如果按下 'q' 键,退出程序
if key == ord('q'):
cv2.destroyWindow('Camera View')
return
def double_cam():
cv2.namedWindow('Camera View', cv2.WINDOW_AUTOSIZE)
raw_cropped_image = image_init()
frames = []
while True:
ret = cap.read() # 读取一帧图像
tmp_frame = cap.read()[1]
frames.append(tmp_frame)
if not ret:
print("Error reading from camera")
break
if len(frames) == 6:
frames.pop(0)
frame = np.mean(frames, axis=0).astype(np.uint8)
# 使用鼠标滚轮事件处理函数
cv2.setMouseCallback('Camera View', handle_scroll)
# 对中心区域缩放
resized_img = cv2.resize(frame, (width, height), cv2.INTER_LINEAR)
cropped_img = resized_img[int(height / 2) - int(window_height / 2):int(height / 2) + int(window_height / 2),
int(width / 2) - int(window_width / 2):int(width / 2) + int(window_width / 2)]
# 合并图像,左边为原始图像,右边为可缩放图像
stack_img = np.hstack((raw_cropped_image, cropped_img))
cv2.imshow('Camera View', stack_img)
key = cv2.waitKey(1) & 0xFF
# 如果按下 'q' 键,退出程序
if key == ord('q'):
cv2.destroyWindow('Camera View')
return
def corner_click(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
if 0 <= x <= window_width / 2 and 0 <= y <= window_height / 2:
while True:
ret = cap.read() # 读取一帧图像
frame = cap.read()[1]
if not ret:
print("Error reading from camera")
break
# 使用鼠标滚轮事件处理函数
cv2.setMouseCallback('Camera View', handle_scroll)
# 对中心区域缩放
resized_img = cv2.resize(frame, (width, height), cv2.INTER_LINEAR)
lu_img = resized_img[0:int(window_height), 0:int(window_width)]
cv2.imshow('Camera View', lu_img)
key = cv2.waitKey(1) & 0xFF
# 如果按下 'q' 键,退出程序
if key == ord('q'):
return
elif 0 <= x <= window_width / 2 and window_height / 2 <= y <= window_height:
while True:
ret = cap.read() # 读取一帧图像
frame = cap.read()[1]
if not ret:
print("Error reading from camera")
break
# 使用鼠标滚轮事件处理函数
cv2.setMouseCallback('Camera View', handle_scroll)
# 对中心区域缩放
resized_img = cv2.resize(frame, (width, height), cv2.INTER_LINEAR)
lu_img = resized_img[int(height - window_height):height, 0:int(window_width)]
cv2.imshow('Camera View', lu_img)
key = cv2.waitKey(1) & 0xFF
# 如果按下 'q' 键,退出程序
if key == ord('q'):
return
elif window_width >= x >= window_width / 2 and window_height / 2 <= y <= window_height:
while True:
ret = cap.read() # 读取一帧图像
frame = cap.read()[1]
if not ret:
print("Error reading from camera")
break
# 使用鼠标滚轮事件处理函数
cv2.setMouseCallback('Camera View', handle_scroll)
# 对中心区域缩放
resized_img = cv2.resize(frame, (width, height), cv2.INTER_LINEAR)
lu_img = resized_img[int(height - window_height):height, int(height - window_width):height]
cv2.imshow('Camera View', lu_img)
key = cv2.waitKey(1) & 0xFF
# 如果按下 'q' 键,退出程序
if key == ord('q'):
return
elif window_width >= x >= window_width / 2 and 0 <= y <= window_height / 2:
while True:
ret = cap.read() # 读取一帧图像
frame = cap.read()[1]
if not ret:
print("Error reading from camera")
break
# 使用鼠标滚轮事件处理函数
cv2.setMouseCallback('Camera View', handle_scroll)
# 对中心区域缩放
resized_img = cv2.resize(frame, (width, height), cv2.INTER_LINEAR)
lu_img = resized_img[0:window_width, int(width - window_width):width]
cv2.imshow('Camera View', lu_img)
key = cv2.waitKey(1) & 0xFF
# 如果按下 'q' 键,退出程序
if key == ord('q'):
return
def corner_cam():
cv2.namedWindow('Camera View', cv2.WINDOW_AUTOSIZE)
global width
global height
frames = []
while True:
ret = cap.read() # 读取一帧图像
tmp_frame = cap.read()[1]
frames.append(tmp_frame)
if not ret:
print("Error reading from camera")
break
if len(frames) == 6:
frames.pop(0)
frame = np.mean(frames, axis=0).astype(np.uint8)
cv2.setMouseCallback('Camera View', corner_click)
tmp_size = frame.shape[:2]
width = tmp_size[1]
height = tmp_size[0]
resized_img = cv2.resize(frame, (width, height), cv2.INTER_LINEAR)
b, g, r = resized_img[0, 0]
lu_img = resized_img[0:int(window_height / 2), 0:int(window_width / 2)]
ru_img = resized_img[0:int(window_height / 2), int(width - window_width / 2):width]
lb_img = resized_img[int(height - window_height / 2):height, 0:int(window_width / 2)]
rb_img = resized_img[int(height - window_height / 2):height, int(width - window_width / 2):width]
stack_img1 = concatenate((lu_img, ru_img), axis=1)
stack_img2 = concatenate((lb_img, rb_img), axis=1)
stack_img = concatenate((stack_img1, stack_img2), axis=0)
cv2.imshow('Camera View', stack_img)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
cv2.destroyWindow('Camera View')
return
# def stability_test():
class GUI:
def __init__(self):
self.window = tk.Tk()
self.window.title('摄像头缩放工具')
self.window.geometry('1000x600')
self.interface()
def interface(self):
self.l = tk.Label(self.window, text='请选择需要的缩放窗口', font=('Arial', 12), width=30, height=2)
self.l.place(x=300, y=20)
self.b1 = tk.Button(self.window, text='单画', font=('Arial', 12), width=10, height=1, command=single_cam)
self.b1.place(x=100, y=500)
self.b2 = tk.Button(self.window, text='双画', font=('Arial', 12), width=10, height=1, command=double_cam)
self.b2.place(x=400, y=500)
self.b3 = tk.Button(self.window, text='四角', font=('Arial', 12), width=10, height=1, command=corner_cam)
self.b3.place(x=700, y=500)
if __name__ == '__main__':
a = GUI()
a.window.mainloop()
# 清理资源
cap.release()
cv2.destroyAllWindows()
USB摄像头小工具
于 2024-09-02 21:39:28 首次发布