C++ – OpenCV – could not find encoder for the specified extension


Here is my code I am using to convert *IplImage to jpg:

IplImage* fIplImageHeader;
fIplImageHeader = cvCreateImageHeader(cvSize(160, 120), 8, 3);
fIplImageHeader->imageData = (char*) memblock;

vector<int> p;
vector<unsigned char> buf;
cv::imencode("JPEG", fIplImageHeader, buf, p);


But I am getting this error:

OpenCV Error: Unspecified error (could not find encoder for the specified extension) in imencode, file /build/buildd/opencv-2.1.0/src/highgui/loadsave.cpp, line 409

terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.1.0/src/highgui/loadsave.cpp:409: error: (-2) could not find encoder for the specified extension in function imencode

Why is that? I have OpenCV 2.1 installed. And this works so obviously jpg encoder must be there:

cvSaveImage("/home/richard/im.jpg", fIplImageHeader);

Best Solution

Ok. I've figured this out. This works:

cv::imencode(".jpg", fIplImageHeader, buf, p);

They should mention it somewhere in the documentation that there should be a full stop before extension.