45int main(
int argc,
char const* argv[])
49 if (argc != 5)
LFATAL(
"USAGE: jevois_camtest <YUYV|BAYER|RGB565> <width> <height> <fps>");
52 if (strcmp(argv[1],
"BAYER") == 0) m.
cfmt = V4L2_PIX_FMT_SRGGB8;
53 else if (strcmp(argv[1],
"YUYV") == 0) m.
cfmt = V4L2_PIX_FMT_YUYV;
54 else if (strcmp(argv[1],
"RGB565") == 0) m.
cfmt = V4L2_PIX_FMT_RGB565;
55 else LFATAL(
"Invalid format, should be BAYER, YUYV or RGB565");
57 m.
cw = std::atoi(argv[2]);
58 m.
ch = std::atoi(argv[3]);
59 m.
cfps = std::atof(argv[4]);
63 int fd;
if ((fd = open (
"/dev/video0", O_RDWR)) == -1)
LFATAL(
"ERROR opening V4L interface");
66 int camidx = -1;
struct v4l2_input inp = { };
69 try {
XIOCTL(fd, VIDIOC_ENUMINPUT, &inp); }
catch (...) {
break; }
70 if (inp.type == V4L2_INPUT_TYPE_CAMERA)
72 if (camidx == -1) camidx = inp.index;
73 LINFO(
"Input " << inp.index <<
" [" << inp.name <<
"] is a camera sensor");
74 }
else LINFO(
"Input " << inp.index <<
" [" << inp.name <<
"] is a NOT camera sensor");
77 if (camidx == -1)
LFATAL(
"No valid camera input found");
80 XIOCTL(fd, VIDIOC_S_INPUT, &camidx);
83 struct v4l2_capability cap = { };
84 XIOCTL(fd, VIDIOC_QUERYCAP, &cap);
85 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0)
LFATAL(
"Video capture not supported");
86 if (!(cap.capabilities & V4L2_CAP_STREAMING))
LFATAL(
"Cameradoes not support streaming i/o");
89 struct v4l2_format fmt = { };
90 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
91 fmt.fmt.pix.width = m.
cw;
92 fmt.fmt.pix.height = m.
ch;
93 fmt.fmt.pix.pixelformat = m.
cfmt;
94 fmt.fmt.pix.field = V4L2_FIELD_ANY;
95 XIOCTL(fd, VIDIOC_S_FMT, &fmt);
97 if ((fmt.fmt.pix.width != m.
cw) || (fmt.fmt.pix.height != m.
ch))
LFATAL(
"Format asked unavailable");
100 struct v4l2_requestbuffers rb = { };
102 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
103 rb.memory = V4L2_MEMORY_MMAP;
104 XIOCTL(fd, VIDIOC_REQBUFS, &rb);
109 struct v4l2_buffer buf = { };
111 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
112 buf.memory = V4L2_MEMORY_MMAP;
113 XIOCTL(fd, VIDIOC_QUERYBUF, &buf);
115 mem[i] = mmap(0 , buf.length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, buf.m.offset);
116 if (mem[i] == MAP_FAILED)
LFATAL(
"Unable to map buffer");
121 struct v4l2_buffer buf = { };
123 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
124 buf.memory = V4L2_MEMORY_MMAP;
125 XIOCTL(fd, VIDIOC_QBUF, &buf);
129 int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
130 XIOCTL(fd, VIDIOC_STREAMON, &type);
131 LINFO(
"Grab start...");
132 for (
int i = 0; i < 100; ++i)
134 struct v4l2_buffer buf = { };
135 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
136 buf.memory = V4L2_MEMORY_MMAP;
137 XIOCTL(fd, VIDIOC_DQBUF, &buf);
143 cv::Mat imgcv(m.
ch, m.
cw, CV_8UC2, mem[buf.index]);
144 cv::cvtColor(imgcv, imgbgr, cv::COLOR_YUV2BGR_YUYV);
145 cv::imwrite(std::string(
"camtest") + std::to_string(i-30) +
".png", imgbgr);
148 XIOCTL(fd, VIDIOC_QBUF, &buf);
155 std::shared_ptr<jevois::Camera> cam(
new jevois::Camera(
"/dev/video0"));
161 LINFO(
"Trashing a few frames...");
163 for (
int i = 0; i < 30; ++i) { cam->get(img); cam->done(img); }
166 LINFO(
"Grab start...");
167 for (
int i = 0; i < 10; ++i)
174 case V4L2_PIX_FMT_SRGGB8:
177 cv::cvtColor(imgcv, imgbgr, cv::COLOR_BayerBG2BGR);
181 case V4L2_PIX_FMT_YUYV:
184 cv::cvtColor(imgcv, imgbgr, cv::COLOR_YUV2BGR_YUYV);
188 case V4L2_PIX_FMT_RGB565:
191 cv::cvtColor(imgcv, imgbgr, cv::COLOR_BGR5652BGR);
197 cv::imwrite(std::string(
"camtest") + std::to_string(i) +
".png", imgbgr);
#define XIOCTL(dev, req, mem)
Helper macro to execute an ioctl, ignore interruptions, and, if error, issue a fatal message and thro...