我有以下简单代码。在运行时,它会从凸轮中获取图像(CAP_V4L
凸轮偏移量适用于Ubuntu,在其他OS(例如OSX)上则不需要),将其编码为png
,以计算其大小并对其进行解码。问题在于,每次运行时,编码图像的缓冲区大小每次都不同。这是为什么?为什么不相等?
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifndef __OPENCV__
#include "opencv2/opencv.hpp"
#define __OPENCV__
#endif
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
int bak, temp;
fflush(stdout);
bak = dup(1);
temp = open("/dev/null", O_WRONLY);
dup2(temp, 1);
close(temp );
Mat frame;
std::vector<uchar> buf;
namedWindow( "Camera", WINDOW_AUTOSIZE );
VideoCapture cam(0 + CAP_V4L);
sleep(1);
if (!cam.isOpened())
{
cout << "\nCould not open reference " << 0 << endl;
return -1;
}
for (int i=0; i<30; i++)
{
cam>>frame;
}
//cout<<"\nCamera initialized\n";
fflush(stdout);
dup2(bak, 1);
close(bak);
imencode(".png",frame, buf);
cout<<buf.size()<<endl;
Mat res = imdecode(buf, 1);
imshow("Camera",res);
waitKey(0);
cout<<'\0';
return 0;
}