How to compile OpenCV with libjpeg-turbo?

c++, libjpeg-turbo, opencv

Solution

To build OpenCV 2.4.0 with `libjpeg-turbo` you need:

- build `libjpeg-turbo` as a static library

- configure OpenCV with the following command:

`cmake -DWITH_JPEG=ON -DBUILD_JPEG=OFF -DJPEG_INCLUDE_DIR=/path/to/libjepeg-turbo/include/ -DJPEG_LIBRARY=/path/to/libjpeg-turbo/lib/libjpeg.a /path/to/OpenCV`

Problem

I am using OpenCV 2.3.1 on OS X Lion in one of my C++ projects. While my project works great as-is, it depends heavily on JPEG decoding and encoding. I would like to gain a speedup by using libjpeg-turbo, but I cannot find the correct way to link with libjpeg-turbo. Depending on my hackery attempts and which files I edit, I either end up with compiling errors or with a useless OpenCV library (everything compiles correctly, but I cannot open or write JPEG files, though no errors appear in the C++ project). Can anyone explain how you can compile OpenCV with libjpeg-turbo for faster JPEG decoding/encoding?

Original source