调用OpenCV中的imshow()没有创建任何输出
The call imshow() in OpenCV is not creating any output
我正在尝试使用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之间的差异是什么?
相关文章:
- 模板化接口 - 创建一个泛型模板类以返回任何容器
- 如何从Windows控制台调用.exe(C++)以在不同的目录(或任何目录)中创建文件夹?
- 如何创建一个模板化函数,可以在任何具有字符串键的 std::map 上运行?
- 如果普通默认构造函数不执行任何操作,为什么我们不能使用 malloc 创建平凡可构造的对象?
- C++:创建1000次唯一的随机分布,在任何分布中都没有重复的数字
- 是否可以用类似C/C++(或任何语言)的语言,从作为用户输入的字符串或文件中创建用户定义的数据类型
- 是否有任何统一的方法可以在 c++ 中创建自定义比较器?
- 调用函数来创建 WinAPI 按钮不会执行任何操作
- 如何创建我稍后在代码中定义大小的任何数组?
- 一个创建彩票游戏的程序,该彩票游戏可以有效地创建 10 套而没有任何重复?
- 该程序应该创建一个带有结果的输出文件,但文件中没有任何内容
- 创建一个构造函数,即Capabe获取任何数量的参数
- 是否有任何可能的方法将事件(自定义)附加到在运行时创建的对象?[C 构建器]
- 声明矢量和指针而无需创建任何类型的对象
- 调用虚拟函数而不通过类类型指针创建任何对象
- 如何在不在堆栈上创建任何内容的情况下打印数组的内容
- 调用OpenCV中的imshow()没有创建任何输出
- 我的旧Qt项目文件(.pro文件),它正在工作;不创建任何文件(moc_ 或 .obj 文件)。为什么它退出构建步骤什么都不做?
- 在创建任何窗口之前,如何获取窗口框架的宽度
- 如何创建任何定制的Unix命令