如何在Open CV Python中缩放我的网络摄像头?

时间:2018-06-15 06:45:19

标签: python python-3.x opencv

我希望我的网络摄像头在open cv python中放大,我不知道怎么做。任何人都可以帮我解决我的问题吗?

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

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

缩放只是增加图像大小。只需通过使用PIL将帧转换为图像然后再调整大小功能来增加图像尺寸即可。 示例代码

    import tkinter
    import cv2
    from PIL import Image,ImageTk


    root = tkinter.Tk()

    root.geometry("1000x500+200+0")
    w = 1000
    h = 630

    capture = tkinter.Canvas(root, bd=2, bg="blue", height=h, width=w)
    capture.grid(column = 0, row = 0)
    video = None
    frame = None
    img=None
    show=None
    begin = False
    def start_capture(event):
        global begin,frame,img,root,show,capture,video    
        video = cv2.VideoCapture(0)
        begin = True
        while begin:
            check, frame = video.read()

            img = Image.fromarray(frame)
            w,h = img.size
            img = img.resize((w*2,h*2))
            show = ImageTk.PhotoImage(img)
            capture.create_image(0,0,anchor=tkinter.NW,image=show)
            root.update()

    def stop_capture(event):
        global video,begin
        begin = False
        video.release()

    start = tkinter.Button(root, text='Start')
    start.grid(column = 0, row = 2)
    start.bind('<Button-1>', start_capture)  
    stop = tkinter.Button(root, text='Stop')
    stop.grid(column = 1, row = 2)
    stop.bind('<Button-1>', stop_capture)  

    root.mainloop()

答案 2 :(得分:0)

我不确定现在添加这一点是否有用。 (希望这至少对opencv中的所有菜鸟有帮助)

我为此遭受了很多苦难:cropped = image[minX:maxX, minY:maxY]作物以某种方式不在感兴趣的领域。

大量研究之后,我发现问题出在语法上。 实际上应该是:cropped = image[minY:maxY , minX:maxX] 因为openCV裁剪语法应该是这样的?。

无论如何,感谢Amara BOUDIB和JDS提供的示例代码!