Using (building?) freetype with openCV c++

Hello,

I’m trying to use the Freetype2 module of openCV to use custom fonts, but I’m failing to make it work.

I have tried using the pre-compiled libraries and building openCV with the git-bash method of OpenCV: Installation in Windows (latest current release, 4.5.2) with no success.

I want to use it in Visual Studio: I have tried adding “PreviousPath\OpenCV_4.5.2\opencv_contrib\modules\freetype\include” to the Additional Include Directories in the project properties; while this makes Visual Studio and Intellisense “accept” freetype.hpp, when I try to build the solution it tells me there are unresolved externals (which obviously means I have failed to properly add the library, but I don’t know where my mistake is).

I’m unsure if I’m not configuring my project correctly or if I’m failing to build the source correctly. I have tried looking at tutorials on how to build OpenCV with Freetype, but the documentation is scarce and I haven’t been able to follow the methods proposed. I have seen mentioned that I need the freetype and hardbuzz libraries, but I don’t know how to add them so that CMake recognises them when building the extra modules of OpenCV.

Has anyone been able to use it and could you please tell me how to do it?

1 Like

you need (to get and build) harfbuzz as well as freetype. both support cmake.

extracts from my cmakecache.txt files, which may very well contain rubbish:

  • harfbuzz needs to be told where freetype is.
FREETYPE_INCLUDE_DIR_freetype2:PATH=C:/Program Files (x86)/freetype/include/freetype2
FREETYPE_INCLUDE_DIR_ft2build:PATH=C:/Program Files (x86)/freetype/include/freetype2
FREETYPE_LIBRARY_DEBUG:FILEPATH=C:/Program Files (x86)/freetype/lib/freetyped.lib
FREETYPE_LIBRARY_RELEASE:FILEPATH=C:/Program Files (x86)/freetype/lib/freetype.lib
  • freetype needs to be told where harfbuzz is.
HARFBUZZ_INCLUDE_DIRS:PATH=C:/Program Files (x86)/harfbuzz/include/harfbuzz
HARFBUZZ_LIBRARIES:FILEPATH=C:/Program Files (x86)/harfbuzz/lib
  • opencv needs to be told about both.
//The directory containing a CMake configuration file for harfbuzz.
harfbuzz_DIR:PATH=C:/Program Files (x86)/harfbuzz/lib/cmake/harfbuzz
HARFBUZZ_DIR:PATH=C:/Program Files (x86)/harfbuzz
HARFBUZZ_FOUND:BOOL=ON
HARFBUZZ_INCLUDE_DIRS:PATH=C:/Program Files (x86)/harfbuzz/include/harfbuzz
HARFBUZZ_LIBRARIES:PATH=C:/Program Files (x86)/harfbuzz/lib/harfbuzz.lib
HARFBUZZ_LIBRARY:FILEPATH=C:/Program Files (x86)/harfbuzz/lib/harfbuzz.lib

FREETYPE_DIR:PATH=C:/Program Files (x86)/freetype
FREETYPE_INCLUDE_DIR_freetype2:PATH=C:/Program Files (x86)/freetype/include/freetype2
FREETYPE_INCLUDE_DIR_ft2build:PATH=C:/Program Files (x86)/freetype/include/freetype2
FREETYPE_LIBRARY_DEBUG:FILEPATH=C:/Program Files (x86)/freetype/lib/freetyped.lib
FREETYPE_LIBRARY_RELEASE:FILEPATH=C:/Program Files (x86)/freetype/lib/freetype.lib

you’ll notice that I “installed” both. I’m sure you can install them anywhere, or not at all. I don’t know if cmake has some intelligence about finding stuff in those default directories or not.

it’s doable but it’s always been a battle for me.

2 Likes

Hi crackwitz,

Many thanks for the help. I think I’m starting to understand a bit better now, but I’m still stuck at the very beginning (note that I’m very new to using CMake and compiling from source).

I have tried to follow the instructions of this website The FreeType Project - Browse /freetype2/2.5.3 at SourceForge.net and have managed to compile the freetype source without harfbuzz, then use visual studio to obtain the lib files.

However, I’m at a complete lost on what to do from here. I don’t know how to specify for harfbuzz where to find the include folders and lib files (I assume this is somewhere in the CMakeLists file but don’t know where) and probably will have the same problem once I move onto compiling freetype again and opencv. Namely, I’m getting te following error:

" HB_HAVE_FREETYPE was set, but we failed to find it. Maybe add a
CMAKE_PREFIX_PATH= to your Freetype2 install prefix" I just don’t know how to do what it says.

Could you please help me with it?

use cmake-gui

if you can wait a few months or a year maybe, built-in freetype support is coming

I’m actually using cmake-gui, but I must be making some mistake along the way. Sadly, I can’t wait for a few months. In any case, thank you for the help you have given me.