void V4L2Capture::read(ics::util::SpMatRAII& res)
{
// 8、capture a image
ics::util::Stopwatch sw;
static int index = 0, i = 0;
v4l2_buf.index = index;
++i;
index = i % BUFFER_SIZE;
auto ret = ioctl(cam_fd, VIDIOC_DQBUF, &v4l2_buf);
ICS_ASSERT(ret != -1, "V4L2Capture::camera VIDIOC_DQBUF Failed.");
// 9、Save image.
std::cout << "init " << i << " cost: " << sw.ElapsedMilliSeconds() << " ms " << std::endl;
sw.Restart();
memcpy(out_mat->mat.data, pic_buf[v4l2_buf.index].vddr, pic_buf[v4l2_buf.index].len);
// out_mat->mat = cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC2, pic_buf[v4l2_buf.index].vddr);
std::cout << "Image2Mat " << i << " cost: " << sw.ElapsedMilliSeconds() << " ms " << std::endl;
sw.Restart();
cv::extractChannel(out_mat->mat, res->mat, 0);
std::cout << "extractChannel " << i << " cost: " << sw.ElapsedMilliSeconds() << " ms " << std::endl;
// 10、Queue the buffers.
ret = ioctl(cam_fd, VIDIOC_QBUF, &v4l2_buf);
ICS_ASSERT(ret != -1, "V4L2Capture::camera VIDIOC_QBUF Failed.");
}
I am using V4L2 to obtain images on an Android device, and the camera only supports YUYV format. I need to obtain the images and extract the Y channel.
1.When I use memcpy to copy data from the buffer to out_mat->mat, cv:: extractChannel takes 2-3 ms, while memcpy takes about 40 ms;
2.When I use CV:: Mat for construction, the construction itself is not time-consuming, but CV:: extractChannel does require about 300ms.
What is causing this phenomenon? If I want to obtain images at a rate of 30 frames per second, how should I optimize it?