调用OpenCV中的imshow()没有创建任何输出

The call imshow() in OpenCV is not creating any output

本文关键字:创建 任何 输出 OpenCV 中的 imshow 调用      更新时间:2023-10-16

我正在尝试使用V4l linux驱动程序和c++从工业相机读取数据。我想使用OpenCV显示结果。我读取缓冲区,创建一个Mat对象,它实际上包含范围为0…255的值。

问题似乎是imshow()调用。当注释掉这一行时,将显示一个没有图像的实际窗口。一旦取消注释,就不会显示任何窗口,并且在显示这一行之后的终端中也没有输出。我不能自己找到一个解决方案,我发现的所有例子看起来都和我的代码一样。

代码如下:

#include <fcntl.h>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <libv4l2.h>
#include <libv4l1.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h> 
#define BUFFERSIZE 357120 // 744 * 480
using namespace cv;
using namespace std;
int main(int argc, char **argv) {
int cameraHandle, i;
unsigned char pictureBuffer[BUFFERSIZE];
char cameraDevice[] = "/dev/video0";
struct v4l2_control V4L2_control;
/* open camera device */
if (( cameraHandle = v4l1_open(cameraDevice, O_RDONLY)) == -1 ){
    printf("Unable to open the camera");
    return -1;
}
// disable auto exposure
V4L2_control.id = V4L2_CID_EXPOSURE_AUTO;
V4L2_control.value = V4L2_EXPOSURE_SHUTTER_PRIORITY;
ioctl(cameraHandle, VIDIOC_S_CTRL, &V4L2_control);
// set exposure time
V4L2_control.id = V4L2_CID_EXPOSURE_ABSOLUTE;
V4L2_control.value = 2;
ioctl(cameraHandle, VIDIOC_S_CTRL, &V4L2_control);
// get 5 pictures to warm up the camera
for (i = 0; i <= 5; i++){
    v4l1_read(cameraHandle, pictureBuffer, BUFFERSIZE);
}
// show pictures
Mat mat = Mat(744, 480, CV_8UC3, (void*)pictureBuffer);
cout << "M = " << endl << " " << mat << endl << endl; // display the image data
namedWindow("imagetest", CV_WINDOW_AUTOSIZE );
imshow("imagetest", mat);
waitKey(30);
cout << "test output" << endl;
//clenup
v4l1_close(cameraHandle);
destroyWindow("imagetest");
return 0;
}
编辑:

嗯,在终端而不是eclipse中运行代码后,我看到了一个分段错误甚至在

后面注释了所有内容
cout << "M = " << endl << " " << mat << endl << endl;

行给了我这个错误

解决。问题在于错误的文件格式。CV_8UC1或CV_8U代替CV_8UC3带来和输出。这些格式之间的差异在这里描述:在OpenCV中,CV_8U和CV_8UC1之间的差异是什么?

相关文章: