python - How can I zoom my webcam in Open CV Python?
问题描述
I want my webcam to be zoomed in open cv python and I don't know how. Can anyone help me with my problem?
import cv2
video = cv2.VideoCapture(0)
while True:
check, frame = video.read()
cv2.imshow('Video', frame)
key = cv2.waitKey(1)
if key == 27:
break
video.release()
cv2.destroyAllWindows
解决方案
您可以使用此解决方案。它使工作 -> 裁剪 + 缩放 + 阵列向上和阵列向下。
import cv2
def show_webcam(mirror=False):
scale=10
cam = cv2.VideoCapture(0)
while True:
ret_val, image = cam.read()
if mirror:
image = cv2.flip(image, 1)
#get the webcam size
height, width, channels = image.shape
#prepare the crop
centerX,centerY=int(height/2),int(width/2)
radiusX,radiusY= int(scale*height/100),int(scale*width/100)
minX,maxX=centerX-radiusX,centerX+radiusX
minY,maxY=centerY-radiusY,centerY+radiusY
cropped = image[minX:maxX, minY:maxY]
resized_cropped = cv2.resize(cropped, (width, height))
cv2.imshow('my webcam', resized_cropped)
if cv2.waitKey(1) == 27:
break # esc to quit
#add + or - 5 % to zoom
if cv2.waitKey(1) == 0:
scale += 5 # +5
if cv2.waitKey(1) == 1:
scale = 5 # +5
cv2.destroyAllWindows()
def main():
show_webcam(mirror=True)
if __name__ == '__main__':
main()
推荐阅读
- sql - 有没有办法在 DuckDB 中按 15 分钟的间隔进行分组?
- twilio - 在短信正文中粘贴出站电话号码
- javascript - Particle.js 在 ReactJS 网站上不起作用
- apache-spark - 从 spark 2.4.8 集群连接到 Confluent 5.5.2 代理
- reactjs - 我如何 npm 链接 React 项目中的模块?
- css - 更改下拉语义 UI 中的元素文本
- javascript - 如何将使用 javascript 的动画限制为特定的屏幕尺寸?
- html - 如何移动此下拉菜单箭头 - HTML/CSS
- php - 如何在php中将“key”和“value”添加到json/array Rest Api
- sql - 减去两个不同的时间戳并在 Oracle 中转换为分钟?