python - 解释 cv2.imencode 结果

标签 python opencv jpeg

cv2.imencode 返回的缓冲区代表什么?

这是 1x1 像素图像的示例。

import cv2
impor numpy as np

img= np.zeros((1,1,3),np.uint8)
en= cv2.imencode('.jpg',img)
print type(en)
<type 'tuple'>
print en[1].shape
(631, 1)

由于某些原因,当图像大小改变时缓冲区大小没有改变:

img= np.zeros((10,10,3),np.uint8)
en= cv2.imencode('.jpg',img)
en[1].shape
(631, 1)

更新:对于更大的图像,它有不同的尺寸。

img= np.zeros((1000,1000,3),np.uint8)
en= cv2.imencode('.jpg',img)
en[1].shape
(16503, 1)

使用随机数据:

img= (np.random.rand(1,1,3)*255).astype(np.uint8)
en= cv2.imencode('.jpg',img)
en[1].shape
(634, 1)

img= (np.random.rand(10,10,3)*255).astype(np.uint8)
en= cv2.imencode('.jpg',img)
en[1].shape
(899, 1)

img= (np.random.rand(1000,1000,3)*255).astype(np.uint8)
en= cv2.imencode('.jpg',img)
en[1].shape
(1175962, 1)

最佳答案

根据 cv2.imencode文档

Note: cvEncodeImage returns single-row matrix of type CV_8UC1 that contains encoded image as array of bytes.

所以基本上输出取决于您定义的图像格式 .png.jpg 等,每种格式都有自己的序列化约定和 cv2.imencode 正是这样做的。它还包含一些与该图像格式相关的元数据,例如:压缩级别等以及像素数据。

关于python - 解释 cv2.imencode 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44328645/

相关文章:

python - 通过 Heroku 部署 Python 应用程序时出现 NoSectionError

python - 以时间间隔对数据库进行调度过滤

c++ - 如何使用opencv框架将点[4]添加到轮廓

Python - 在 Mac 上安装 opencv 时遇到问题(几个月前 opencv 运行良好之后)

c - do-while 循环是无限的并且不断 append 文件

python - 打开输入和输出文件时顺序重要吗?

python - 序列化错误 "c:\path\path"[Errno 13] 权限被拒绝 : C:\\path\\path

opencv - 模板匹配和pyrDown问题

node.js - ffmpeg将流转换为jpg缓冲区而不保存磁盘

latex - 在 Latex 中包含图形文件