Unicode support in cv::addText via Qt (Bugfix #4128)


Added by Craig Reynolds about 10 years ago. Updated almost 10 years ago.


Status:Done Start date:2015-01-16
Priority:Normal Due date:
Assignee:Craig Reynolds % Done:

0%

Category:highgui-images
Target version:3.0
Affected version:branch 'master' (3.0-dev) Operating System:Any
Difficulty:Easy HW Platform:Any
Pull request:

Description

Passing a Unicode UTF-8 string into cv::addText (or cvAddText) results in extraneous characters being drawn (often â or Â) and other being dropped (if their Unicode is greater than 255). This is a symptom of ignoring the multi-byte UTF-8 escape encoding.

Fortunately the Qt library is fully Unicode compliant so can render all glyphs defined by the standard if provided by the fonts in use. (Which is a lot, tens of thousands, see: http://unicode-table.com/en/) All that is needed to fix this bug was to call Qt's QString::fromUtf8 function to indicate the array of 8-bit values should be interpreted as UTF-8 rather than as a vanilla C char string.

Note that this patch provides support for drawing Unicode with Qt, but does not help with the conceptually related problem of using Unicode in pathnames.

I thought I was supposed to provide the URL for my own Pull Request (https://github.com/cwreynolds/opencv/pull/1) in the box below, but it said that was invalid. Perhaps only PRs on https://github.com/Itseez/opencv/pull/... can be listed there? Please advise, this is my first contribution to OpenCV.

Example before this change:

Example after this change:

C++ program to draw example:

#include <opencv/highgui.h>
int main (int argc, char** argv)
{
    cv::namedWindow ("Test", 1);
    cv::moveWindow ("Test", 10, 50);
    cv::Mat image (cv::Size (1000, 200), CV_8UC3, cv::Scalar(0, 111, 222));
    CvFont fontB = cv::fontQt ("Times", 30, cv::Scalar (255, 255, 255), CV_FONT_BOLD);
    CvFont fontW = cv::fontQt ("Helvetica", 30, cv::Scalar (0, 0, 0), CV_FONT_NORMAL);
    cv::addText (image,
                 "Here is some vanilla 7-bit 'ascii' text.",
                 cv::Point (50, 60),
                 fontB);
    cv::addText (image,
                 "Here is some text with extended “Unicode” glyphs, like circle-R: ®, ",
                 cv::Point (50, 110),
                 fontW);
    cv::addText (image,
                 "superscript TM: ™ and the point-of-interest symbol (OSX cmd): ⌘",
                 cv::Point (50, 145),
                 fontW);
    cv::imshow ("Test", image);
    cv::waitKey(0);
    return 0;
}

Associated revisions

Revision 2bc16e83
Added by Maksim Shabunin over 9 years ago

Merge pull request #4128 from alalek:fix_noipp_build

History

Updated by Craig Reynolds about 10 years ago

Sorry for my noobishness, I seemed to have created a PR on my personal fork, rather than on Itseez/opencv. This may be better: https://github.com/Itseez/opencv/pull/3614

I also neglected to mention that the line of code modified by this commit seems to exists unchanged in the same file in OpenCV 3.0. I have not tested it there but would be happy to if that seems like a good thing. I do not know what the policy is for migrating changes from version n-1 to n.

Updated by Vadim Pisarevsky almost 10 years ago

ok, looks like the PR has been merged in. Thanks for the contribution!

  • Status changed from New to Done

Updated by Steven Puttemans almost 10 years ago

Vadim, could we ask the author to apply the same fix to the master branch? Else it won't get in :)
Changed to master branch and reopened. @Craig are you up to it?

  • Status changed from Done to Open
  • Assignee set to Craig Reynolds
  • Affected version changed from branch '2.4' (2.4-dev) to branch 'master' (3.0-dev)

Updated by Maksim Shabunin almost 10 years ago

  • Category set to highgui-images
  • Target version set to 3.0

Also available in: Atom PDF