Skip to content

FreeType/TTF putText crashes with CV_32FC3 (low priority) #2771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 tasks done
cwreynolds opened this issue Dec 3, 2020 · 1 comment · Fixed by #2776
Closed
4 tasks done

FreeType/TTF putText crashes with CV_32FC3 (low priority) #2771

cwreynolds opened this issue Dec 3, 2020 · 1 comment · Fixed by #2776

Comments

@cwreynolds
Copy link

System information (version)
  • OpenCV => 4.5.0
  • Operating System / Platform => macOS Catalina 10.15.7
  • Compiler => Xcode 12.2
Detailed description

The older putText() function works on both CV_8UC3 and CV_32FC3 (probably all) types of Mat.

The version of putText() that uses FreeType2 and (e.g.) .ttf files appears to only work on CV_8UC3 Mats, according to the assert that fails while trying to put text on a 3xfloat Mat.

This seems easy enough to work around, so should be considered a low priority bug. But since I ran into it I thought I should file this report with code to reproduce.

Note that the documentation (here) does not mention the restriction to CV_8UC3.

Steps to reproduce

Run this function, passing in the pathname of a .ttf file:

void compare_putTexts(const std::string& ttf_pathname)
{
    auto two_putTexts = [&](const std::string& window, int type, int bright)
    {
        // Make white mat of given type.
        cv::Mat mat(300, 300, type, cv::Scalar::all(bright));
        // Draw text the original way.
        cv::putText(mat, "hello world", cv::Point(20, 100), cv::FONT_HERSHEY_SIMPLEX,
                    1, CV_RGB(0, 0, 0), 1, cv::LINE_AA);
        cv::namedWindow(window);
        cv::imshow(window, mat);
        cv::waitKey();
        // Draw text using TTF.
        cv::Ptr<cv::freetype::FreeType2> ft2;
        ft2 = cv::freetype::createFreeType2();
        ft2->loadFontData(ttf_pathname, 0);
        ft2->putText(mat, "hello world", cv::Point(20, 200),
                     30, CV_RGB(0, 0, 0), cv::FILLED, cv::LINE_AA, true);
        cv::imshow(window, mat);
        cv::waitKey();
    };
    two_putTexts("window 1", CV_8UC3, 255); // 3 x 8bit color
    two_putTexts("window 2", CV_32FC3, 1);  // 3 x float color
}

A window appears with text drawn by the old putText() on a CV_8UC3 Mat.
Type any key to proceed.
The window will show a second line of text using FreeType2.
Type any key to proceed.
A second window appears with a CV_32FC3 Mat.
Type any key to proceed.
Crashes with an assert failure:

terminating with uncaught exception of type cv::Exception: OpenCV(4.5.0) /tmp/opencv-20201123-26930-m95s1u/opencv-4.5.0/opencv_contrib/modules/freetype/src/freetype.cpp:197: error: (-215:Assertion failed) ( _img.empty() == false ) && ( _img.isMat() == true ) && ( _img.depth() == CV_8U ) && ( _img.dims() == 2 ) && ( _img.channels() == 3 ) in function 'putText'

Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    answers.opencv.org, Stack Overflow, etc and have not found solution
  • I updated to latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc
@alalek
Copy link
Member

alalek commented Dec 3, 2020

crashes

It is just an error with limitation messages in form of C++ exception.
Application crashes because it doesn't catch them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants