Installing OpenCV 4.8 with FreeType

Hi Forum, hi developers,

so far I have used OpenCV with great pleasure. The documentation was superior and I really liked that almost everything worked out of the box. However I got to a point where I am struggeling right now.

I am using OpenCV for a computerVision Project on the rapsberry pi.
Therefore I like to include some Text Data onto images. As this includes technical data the ‘µ’ symbol shal be used, which only delivers ?? with the standard putText method. So I used google and found out about freeType.

The problem is, whereever I look, I cannot find a good tutorial how to compile openCV with Freetype. I installed Freetypewith apt-get, after that I compiled harfbuzz from source and then I tried to compile the new openCV 4.8 and set the Compiler Flag WITH_FREETYPE=ON. OpenCV did compile, however obviously I am missing one (or more) steps as still there is not freeType header available in my C++ project. Also in the /usr/local/include/opencv4/opencv2 path is no such File.

Is there a good documentation on how to integrate freeType into OpenCV for C++? A step-by-step guide would be nice, however I would also appreciate a list of compilerflags that cmake needs to compile openCV with Freetype or a hint saying, that Freetype needs to be compiled from source, … Basically anything that makes the process a bit more clear is highly appreciated.

Cheers Daniel

I have never done that on a linux.

you might have to deal with “harfbuzz” in addition to freetype.

if you’re lucky, you just have to install those (including dev package), and then cmake will pick them up for opencv.

if not, you might have to forage through opencv’s cmake files and figure out which cmake variables need setting so you tell the cmake process explicitly where those libraries can be found.

Hi crackwitz,

thanks for your reply. It sounds a bit like, what I have tried so far, but then I will maybe retry the process with an edited cmake File. Are there any other compiler flags needed apart from the WITH_FREETYPE=ON flag?

Ok, i narrowed it down. The CMakeLists.txt of the gapi module (where FreeType is located in) aborts after the command (I inserted a few message commands into the cMake File):
ocv_add_module(gapi
REQUIRED
opencv_imgproc
OPTIONAL
opencv_video opencv_calib3d
WRAP
python
)

This results in the following final command line output:
– OpenCV modules:
– To be built: core features2d flann highgui imgcodecs imgproc photo python3 video videoio
– Disabled: calib3d dnn gapi ml objdetect stitching ts world

I suspect, that FreeType is not included in the build, because gapi is on the Disbaled list. Am I right?

Beforehand it stated:
– ADE: Downloading v0.1.2a.zip from https://github.com/opencv/ade/archive/v0.1.2a.zip
– Checking for module ‘freetype2’
– Found freetype2, version 23.4.17

So in general I suppose, it should be there ready to be built with openCV. I hope this helps someone to help me solve this issue.

wait, wait, that’s wrong. no connection at all. wrong suspect.
you’ll have to look at this cmakelists.txt instead

i’ve never done this personally, but please, try to install (dev version of) harfbuzz & freetype2 , re-run cmake, and show (complete !) output here(!)

1 Like

Thank you, that led me on the right course. I was able to build openCV with freetype now. Sometimes the error is located before the real problem happens. I was not aware, that the contrib modules where needed and excluded them from my build.
I will test my programm with it tomorrow and will write a mini guide on how to install on linux, if everything works out

1 Like

SO now, the late answer (wanted to make it right) to how to install openCV on the Raspberry Pi with FreeType font support.

The easiest way is to install freetype from the official repositories and compile harfbuzz from source. Compiling Freetype from source is also possible however, if a newer version is required. I used harfbuzz version 8.0.1 (but also a newer version can be used probably).

First install harfbuzz dependencies (and compilers) by:

sudo apt-get install cmake unzip meson pkg-config ragel gtk-doc-tools gcc g++ libfreetype6-dev libglib2.0-dev libcairo2-dev -y

Then get an official harfbuzz release from github (currently possible) by:

wget -O harfbuzz_**x_y_z**.zip https://github.com/harfbuzz/harfbuzz/archive/refs/tags/**x.y.z**.zip

Exchange x, y, and z with your desired version.

unzip harfbuzz_**x_y_z**.zip

cd ./harfbuzz-**x.y.z**/

meson setup build && meson test -Cbuild

sudo meson install -C build

cd ~

rm -rf ./harfbuzz-**x.y.z**/

rm harfbuzz_**x_y_z**.zip

Then install openCV with your desired modules AND the compile option -D WITH_FREETYPE=ON

A good guide can be found here Install OpenCV on Raspberry Pi - Q-engineering (qengineering.eu)

2 Likes