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

标签 python opencv computer-vision

我正在尝试使用 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/

相关文章:

python - 谷歌地图和谷歌应用引擎

visual-studio-2010 - 执行人脸识别时找不到或打开PDB文件

python-2.7 - Python调整图像大小并发送到谷歌视觉功能

computer-vision - 如何计算图像分割中的IU平均得分?

python - 如何访问 Pandas 中的字典列字段

python - Pandas groupby 和选择器顺序

python - 您可以从 Django ORM 的父类(super class)模型中访问子类模型吗?

image - (OpenCV)前景和背景颜色相似的图像分割?

opencv - findFundamentalMat 的掩码输出参数是什么?

visual-c++ - 如何在opencv中合并两个图像?