如何保存分辨率大于1080p的图像?

时间:2019-07-11 16:58:14

标签: python opencv logitech

我正在尝试以3840x2160分辨率使用Logitech BRIO,当我执行python代码时,会打开一个带有摄像机图像的窗口(在3840x2160中),但是当我保存帧时,程序在1920x1080中创建图像。如何在4k中保存图像  高分辨率?

我正在使用opencv-python==4.1.0.25

import cv2
import time

def main(args):

    CAMERA_PORT = 0

    IMAGEWIDTH = 3840
    IMAGEHEIGHT = 2160

    #Propriedades de configuracao da camera
    # 3 = width da camera, 4 = height da camera
    CAMERA_PROP_WIDTH = 3
    CAMERA_PROP_HEIGHT = 4

    camera = cv2.VideoCapture(CAMERA_PORT)
    camera.set(CAMERA_PROP_WIDTH, IMAGEWIDTH)
    camera.set(CAMERA_PROP_HEIGHT, IMAGEHEIGHT)

    imagePath = "/home/barbosa/Documents/camera-controller/images/image.png"


    while(True):

        retval, image = camera.read()
        cv2.imshow('Foto',image)

        k = cv2.waitKey(100)

        if k == 27:
            break

        elif k == ord('s'):
            cv2.imwrite(imagePath,image)
            break

    cv2.destroyAllWindows()
    camera.release()
    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

1 个答案:

答案 0 :(得分:1)

您可以制作自己的自定义尺寸调整功能来放大并保持宽高比,然后保存图像。我在IP摄像机而不是网络摄像机上对其进行了测试。

enter image description here

这是调整大小功能

# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
    # Grab the image size and initialize dimensions
    dim = None
    (h, w) = image.shape[:2]

    # Return original image if no need to resize
    if width is None and height is None:
        return image

    # We are resizing height if width is none
    if width is None:
        # Calculate the ratio of the height and construct the dimensions
        r = height / float(h)
        dim = (int(w * r), height)
    # We are resizing width if height is none
    else:
        # Calculate the ratio of the 0idth and construct the dimensions
        r = width / float(w)
        dim = (width, int(h * r))

    # Return the resized image
    return cv2.resize(image, dim, interpolation=inter)

完整代码

import cv2
import time

# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
    # Grab the image size and initialize dimensions
    dim = None
    (h, w) = image.shape[:2]

    # Return original image if no need to resize
    if width is None and height is None:
        return image

    # We are resizing height if width is none
    if width is None:
        # Calculate the ratio of the height and construct the dimensions
        r = height / float(h)
        dim = (int(w * r), height)
    # We are resizing width if height is none
    else:
        # Calculate the ratio of the 0idth and construct the dimensions
        r = width / float(w)
        dim = (width, int(h * r))

    # Return the resized image
    return cv2.resize(image, dim, interpolation=inter)

def main(args):

    CAMERA_PORT = 0

    IMAGEWIDTH = 3840
    IMAGEHEIGHT = 2160

    #Propriedades de configuracao da camera
    # 3 = width da camera, 4 = height da camera
    CAMERA_PROP_WIDTH = 3
    CAMERA_PROP_HEIGHT = 4

    camera = cv2.VideoCapture(CAMERA_PORT)
    camera.set(CAMERA_PROP_WIDTH, IMAGEWIDTH)
    camera.set(CAMERA_PROP_HEIGHT, IMAGEHEIGHT)

    imagePath = "/home/barbosa/Documents/camera-controller/images/image.png"

    while(True):

        retval, image = camera.read()
        cv2.imshow('Foto',image)

        k = cv2.waitKey(100)

        if k == 27:
            break

        elif k == ord('s'):
            image = maintain_aspect_ratio_resize(image, width=IMAGEWIDTH)
            cv2.imwrite(imagePath,image)
            break

    cv2.destroyAllWindows()
    camera.release()
    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))