【问题标题】:How can I zoom my webcam in Open CV Python?如何在 Opencv Python 中缩放我的网络摄像头?
【发布时间】:2018-11-24 23:51:13
【问题描述】:

我希望在打开的 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

【问题讨论】:

  • 展示你的努力,否则问题将被关闭。
  • 完成。我现在已经编辑过了。 @Shree

标签: python python-3.x opencv


【解决方案1】:

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

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

【讨论】:

  • 如果你 cv2.waitkey(1) = 82 或 84 你可以使用箭头键,我不确定 0 和 1 在这里应该做什么。
  • 我做了一个改变,对任何想知道的人来说效果更好。 radiusX,radiusY = int(centerX*scale), int(centerY*scale),然后我的 scale 变量从 1 开始(无缩放),您可以将其减小到 0(如 0.01)以进行更多缩放,例如缩放 = 缩放 * 0.95(放大),缩放 = 缩放 / 0.95(缩小)。并确保将其保持在 (0.01, 1) 的范围内。
【解决方案2】:

缩放只是增加图像大小。只需通过使用 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()

【讨论】:

    【解决方案3】:

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

    我为此受了很多苦:cropped = image[minX:maxX, minY:maxY] Crop 不知何故超出了感兴趣的领域。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多