Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Finally I was able to build OpenCV 3.3.0 with freetype and HarfBuzz enabled! Thanks to "hon_no_mush" and "Tsukasa Sugiura".

Steps to build succesfully opencv with freetype and hafbuzz:

1- Build freetype and harfbuzz dlls using the method has described in: http://www.gregwessels.com/dev/2017/0...

2- Download and extract OpenCV 3.3.0 and OpenVC-Contrib 3.3.0

3- Replace CMakeList.txt on freetype module (opencv-contrib) with that has described by "Tsukasa Sugiura" : https://gist.github.com/UnaNancyOwen/

4- Using CMake to create a Visual Studio 2017 project for OpenCV.

5- Build the project.

BUT there is another problem! When I use freetype with persian(or Arabic) strings, letters are rendered separately! (See the attached image) : "سلام" => "م ا ل س"

image description C:\fakepath\opencv_freetype.jpg

My Code:

#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <opencv2\freetype.hpp>

const char* InitImage = "Initial Image";

int main()
{

    cv::namedWindow(InitImage);

    cv::Mat mat = cv::imread("C:\\Users\\ATabibi\\Pictures\\texture00.png");
    cv::imshow(InitImage, mat);

    //----------------
    // Create FreeType2
    cv::Ptr<cv::freetype::FreeType2> ft2 = cv::freetype::createFreeType2();

    // Load Font Data
    ft2->loadFontData("BHoma.otf", 0);

    // Put Text 
    ft2->putText(mat, u8"سلام", cv::Point(50, 50), 44, cv::Scalar(255, 255, 255), -1, cv::LINE_AA, false);

    cv::imshow(InitImage, mat);

    cv::waitKey();

    return 0;
}

Finally I was able to build OpenCV 3.3.0 with freetype and HarfBuzz enabled! Thanks to "hon_no_mush" and "Tsukasa Sugiura".

Steps to build succesfully opencv with freetype and hafbuzz:

1- Build freetype and harfbuzz dlls using the method has described in: http://www.gregwessels.com/dev/2017/0...

2- Download and extract OpenCV 3.3.0 and OpenVC-Contrib 3.3.0

3- Replace CMakeList.txt on freetype module (opencv-contrib) with that has described by "Tsukasa Sugiura" : https://gist.github.com/UnaNancyOwen/

4- Using CMake to create a Visual Studio 2017 project for OpenCV.

5- Build the project.

BUT there is another problem! When I use freetype with persian(or Arabic) strings, letters are rendered separately! (See the attached image) : "سلام" => "م ا ل س"

image description C:\fakepath\opencv_freetype.jpg

My Code:

#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <opencv2\freetype.hpp>

const char* InitImage = "Initial Image";

int main()
{

    cv::namedWindow(InitImage);

    cv::Mat mat = cv::imread("C:\\Users\\ATabibi\\Pictures\\texture00.png");
    cv::imshow(InitImage, mat);

    //----------------
    // Create FreeType2
    cv::Ptr<cv::freetype::FreeType2> ft2 = cv::freetype::createFreeType2();

    // Load Font Data
    ft2->loadFontData("BHoma.otf", 0);

    // Put Text 
    ft2->putText(mat, u8"سلام", cv::Point(50, 50), 44, cv::Scalar(255, 255, 255), -1, cv::LINE_AA, false);

    cv::imshow(InitImage, mat);

    cv::waitKey();

    return 0;
}

//----Update-----:

Today I learned another method to insert UTF-8 strings to an image in opencv: If opencv libs have built with Qt support, we can use cv::addText function (instead puttext) and all problems are solved!!

#include <opencv2\opencv.hpp>

const char* InitImage = "Initial Image";

int main()
{

    cv::namedWindow(InitImage);

    cv::Mat mat = cv::imread("C:\\Users\\ATabibi\\Pictures\\texture00.png");

    cv::addText(mat, u8"سلام", cv::Point(150, 150), "B Nazanin", 24,cv::Scalar(255,0,100));
    cv::imshow(InitImage, mat);

    cv::waitKey();

    return 0;
}

and result:

image description C:\fakepath\opencv_addText.jpg