opencv中具有一定像素高度、宽度的视频

And*_*olo 2 python opencv computer-vision

我\xe2\x80\x99m尝试使用opencv在1080p相机上拍照,但是,只希望照片为224x224像素。我怎样才能使用 opencv 来做到这一点。

\n\n

我目前有以下代码:

\n\n
Import cv2\nCap = cv2.VideoCam(0)\nCap.set(3, 224)\nCap.set(4, 224)\n\nRet, frame = cap.read()\n
Run Code Online (Sandbox Code Playgroud)\n\n

然而,当我查看框架的形状时,它不是(224, 224, 3)。有人可以帮我弄清楚如何让它输出我想要的像素尺寸

\n

Mar*_*ell 5

当您说您想要 224x224 图像时,这取决于您的意思。如果我们从这张 1920x1080 的图像开始,您可能需要:

  • (A) - 左上角,以洋红色突出显示
  • (B) - 中央 224x224 像素,以蓝色突出显示
  • (C) - 最大的正方形,大小调整为 224x224,以红色突出显示
  • (D) - 整个图像扭曲以适应 224x224

在此输入图像描述

因此,假设您已将相机中的帧读取到一个名为的变量中,im如下所示:

...
...
ret, im = cap.read()
Run Code Online (Sandbox Code Playgroud)

如果您想要 (A),请使用:

# If you want the top-left corner
good = im[:224, :224]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


如果您想要 (B),请使用:

# If you want the centre
x = h//2 - 112
y = w//2 - 112
good = im[x:x+224, y:y+224]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


如果您想要 (C),请使用:

# If you want the largest square, scaled down to 224x224
y = (w-h)//2
good = im[:, y:y+h]
good = cv2.resize(good,(224,224))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


如果您想要 (D),请使用:

# If you want the entire frame distorted to fit 224x224
good = cv2.resize(im,(224,224))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

关键词:图像处理、视频、1920x1080、1080p、裁剪、扭曲、最大正方形、中心部分。左上角,Python,OpenCV,框架,提取。