gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:54:02 30 4
gpt4 key购买 nike

我正在尝试使用 opencv 在 1080p 相机上拍照,但是,只希望照片为 224x224 像素。我如何使用 opencv 来做到这一点。

我目前有以下代码:

Import cv2
Cap = cv2.VideoCam(0)
Cap.set(3, 224)
Cap.set(4, 224)

Ret, frame = cap.read()

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

最佳答案

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

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

enter image description here

因此,假设您已使用以下内容将相机中的帧读取到名为 im 的变量中:

...
...
ret, im = cap.read()
<小时/>

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

# If you want the top-left corner
good = im[:224, :224]

enter image description here

<小时/>

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

# If you want the centre
x = h//2 - 112
y = w//2 - 112
good = im[x:x+224, y:y+224]

enter image description here

<小时/>

如果您想要 (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))

enter image description here

<小时/>

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

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

enter image description here

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

关于python - opencv中具有一定像素高度、宽度的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59419664/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com