当我尝试使用 OpenCV 从相机捕获图像数据并将其读出时,事情并不顺利。我尝试了多种格式,例如 CV_8UC3 和 CV_UC1 灰度图像。
首先,我从相机拍摄了一张图像(640*480)并将数据保存到文件中
VideoCapture cap(0);
namedWindow("test",0);
namedWindow("gray",0);
FILE *f=fopen("data.txt","wt+");
while(1)
{
Mat frame;
cap>>frame;
imshow("test", frame);
//Mat temp(1, 1, CV_8UC3);
Mat gray;
if(waitKey(30) >= 0)
{
cvtColor(frame, gray, CV_BGR2GRAY);
imshow("gray", gray);
waitKey();
fwrite(gray.data, sizeof(unsigned char), 640*480,f);
break;
}
}
fclose(f);
return 0;
然后在另一个程序中,我尝试像这样读出它们:
FILE *f = fopen("data.txt", "rt");
unsigned char* buffer;
size_t result;
buffer = (unsigned char*)malloc(sizeof(unsigned char)*640*480);
result = fread(buffer, sizeof(unsigned char), 640*480,f);
fclose(f);
Mat image(640, 480, CV_8UC1, buffer);
namedWindow("test", 0);
imshow("test", image);
waitKey();
图像出错了。
感谢您提出任何建议。