简体   繁体   English

如何在 Open CV 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.我希望我的网络摄像头在 open cv python 中放大,但我不知道如何。 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

You can use this solution.您可以使用此解决方案。 It makes the job -> croping + zoom + array up and array down.它使工作 -> 裁剪 + 缩放 + 阵列向上和阵列向下。

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

Zooming is simply increasing image size .缩放只是增加图像大小。 Just increase image size by converting frame to image using PIL and then resize function.只需通过使用 PIL 将帧转换为图像然后调整大小功能来增加图像大小。 Sample code示例代码

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

I am not sure if this is useful now to add this point.我不确定现在添加这一点是否有用。 (Hope this helps for all the noobs in opencv at least) (希望这至少对 opencv 中的所有菜鸟有所帮助)

I suffered a lot at this: cropped = image[minX:maxX, minY:maxY] Crop was somehow out of the interested area.我在这方面受了很多cropped = image[minX:maxX, minY:maxY]cropped = image[minX:maxX, minY:maxY] Crop 不知何故超出了感兴趣的区域。

After researching a lot, I found out that the problem was with the syntax.经过大量研究,我发现问题出在语法上。 Actually it should have been : cropped = image[minY:maxY , minX:maxX] because openCV crop syntax should be like this?..实际上应该是: cropped = image[minY:maxY , minX:maxX]因为openCV 裁剪语法应该是这样的?..

Anyways, thanks to Amara BOUDIB & JDS for the sample codes!无论如何,感谢 Amara BOUDIB 和 JDS 提供示例代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM