首页 > 解决方案 > 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

标签: pythonpython-3.xopencv

解决方案


您可以使用此解决方案。它使工作 -> 裁剪 + 缩放 + 阵列向上和阵列向下。

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()

推荐阅读