Even before publishing my previous article about OpenCV and JPEG-images from buffers, I found the cv::imdecode()-function. But it was Peter’s comment which made me really take a look and compare the two methods.
Here’s the code I’m using to create a cv::Mat from a JPEG-image in a buffer using cv::imdecode().
void handleImage_o(const uint8_t *buffer, size_t size)
{
std::vector<uint8_t> v;
v.assign(buffer, buffer+size);
cv::Mat img = cv::imdecode(v, CV_LOAD_IMAGE_COLOR);
}
Yes, it is only 3 lines compared to the 20+ lines using libjpeg… My initial doubt about imdecode() was the speed of decoding. Will it be as fast as my manual approach? Well, almost!:
This quick and dirty benchmark is of course subjective and not usable for reference as it lacks spread, but for my use case it is enough to help me make a decision.
I decode 100 times the same image (4288×2848 pixels) and just measure the time(1) it took. Here are the results with imdecode()
real 0m14.600s user 0m13.652s sys 0m0.956s
versus libjpeg-turbo.
real 0m12.476s user 0m11.736s sys 0m0.760s
My version using libjpeg-turbo is roughly 14% faster than imdecode() from OpenCV 2.4.9.1 .
Is 14% justifying the complexity of the libjpeg-based code? That’s up to you to decide.