Opencv如何从Mat快速获取图像流
Opencv how to get image stream from Mat quickly?
我可以使用imwrite()将图像(如"face.jpg")写入磁盘,然后使用fstream将此JPG读取到数组中。这个数组就是我想要的。
但是,如何快速得到这个?从内存而不是磁盘。我认为图像数据在Mat.data,长度是Mat.cols*Mat.rows。我不确定这是对还是错。所以,我用fstream把它写入磁盘,然后用图像查看器打开它,什么也没有。一定是出了什么问题。
Mat frame;
VideoCapture cap(0);
if (!cap.isOpened())
{
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, 160);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 120);
cap >> frame;
if(frame.empty()){
return -2;
}
//I just want the pointer and length of image information,the following is just for testing
//whether that the same as I thought,if it's right ,frame.data and len is what I want,but it not work.
FILE *fp = fopen("face.jpg", "wb");
if (NULL==fp)
{
return -1;
}
int len = frame.cols*frame.rows; //or 3*frame.cols*frame.rows
fwrite(frame.data, len, sizeof(char), fp);
fclose(fp);
namedWindow("face", 1);
imshow("face", frame);
waitKey(1000);
我是新的opencv,我只是想获得图像数据。谢谢你的帮助!
在写入磁盘之前检查过尺寸了吗?在这里看到您的代码对其他人很有帮助。在Mat的情况下,除非您的数据是灰度的,否则大小将超过cols *行。您应该验证格式是RGB、RGBA还是YUV等。在JPEG的情况下,它很可能是RGBX;所以你应该检查你的流大小是3 * cols *行还是4 * cols *行
我只是用imencode()做了这个,谢谢@ZdaR。
vector<uchar> buff;
vector<int>param = vector<int>(2);
param[0] = CV_IMWRITE_JPEG_QUALITY;
param[1] = 95;
imencode(".jpg", frame, buff, param);
int len = buff.size();
FILE *fout;
fout = fopen("555.jpg", "wb");
if(NULL==fout){
return -3;
}
fwrite(&buff[0], 1, len*sizeof(uchar), fout);
fclose(fout);
相关文章:
- 如何从amd gpus的显示输出中获取图像数据
- 可视化 如何获取图像中像素的颜色(以C++ (sfml) 为单位)
- WIC - Exif 元数据查询 - 如何获取图像说明
- OpenCV - 获取图像的一部分
- 使用 C++ 和 cURL,如何通过需要用户 ID 和密码的 url 获取图像
- 从 python 发送和获取图像到C++
- 在调整大小窗口时,如何使用鼠标单击来获取图像的坐标
- 使用Open CV和Java单击Android时,获取图像尺寸
- 如何通过 c++ 获取图像的所有像素数据 (RGB)
- OpenCV在尝试从轴摄影机获取图像时出错
- 从OpenFileDialog获取图像并将其转换为::Mat
- 获取图像的像素颜色
- MRPT:可以从视口获取图像
- 在平移和倾斜相机后获取图像的 x 和 y
- 如何在 OpenGLES 中获取图像变量的绑定点
- CImg - 获取图像的反向 FFT
- 如何在没有额外分配的情况下从OpenCV矩阵中获取图像数据
- Qt Java-从库中获取图像
- 从gdiplus (GDI+)中的设备上下文或图形对象中获取图像/位图
- ImageMagick c++ API获取图像通道