IT

OpenCV putText 한글 넣기 (Visual Studio)

라이꼬끼 2019. 6. 1. 16:13

영상이나 이미지에 putText로 문구를 넣을 때 한글이 넣어지지 않는다.

한글이 유니코드고 유니코드를 OpenCV가 지원하지 않기 때문이다. 따라서 중국어, 일본어, 체코어 등등에 있는 다른 문자들도 안넣어진다.

그래서 유니코드 문자를 넣을 수 있는 함수들을 만들어보았다. 대신 현재는 MFC 라이브러리를 사용했기 때문에 Windows Visual Studio에서만 사용할 수 있다.

 

https://github.com/hyeonjoo/openCVputUniText

 

hyeonjoo/openCVputUniText

Putting Unicode characters in OpenCV images in Visual Studio with MFC library. - hyeonjoo/openCVputUniText

github.com

 

당연히 프로젝트에서 OpenCV를 사용하기 위한 세팅이 되어있어야 한다.

 

제작된 함수를 사용하기 위해서는, MFC 라이브러리를 추가하는 작업이 필요하다. 생각보다 간단하다.

 

1. 프로젝트 Configuration 속성 창 -> 일반 탭 -> MFC 사용 부분을 정적 라이브러리 MFC 사용을 선택한다.

    Project Configuration Properties Page -> General -> 'Use of MFC' is set to 'Use MFC in a Static Library'.

2. 같은 탭에서, 'Character Set' 를 '멀티바이트 Character Set 사용'으로 설정한다.

    'Character Set' is set to 'Use Multi-Byte Character Set'.

 

끝이다.

 

이제 제작된 putUnitext.cpp, putUnitext.hpp 파일들을 추가하고, 

putText를 사용할 위치에 다음 함수를 사용하면 된다.

 

IplImage, Mat 둘 다 지원한다.

 

putUniText(IplImage * src, IplImage dst, char text, CvPoint point, CvScalar color);

putUniText(cv::Mat src, cv::Mat& dst, char* text, cv::Point point, cv::Scalar color);

 

다음은 예제 코드이다.

#include "opencv2/opencv.hpp"
#include "putUnitext.hpp"

using namespace cv;

int main(int, char**)
{
	Mat imageMat = imread("source.jpg");
	IplImage * imageIpl = cvLoadImage("source.jpg");

	char* str1 = "한글 된다!!";
	char* str2 = "야호!!";

	putUniText(imageIpl, imageIpl, str1, cvPoint(200, 150), CV_RGB(100, 255, 255));
	cvShowImage("Iplimage", imageIpl);

	putUniText(imageMat, imageMat, str2, cvPoint(200, 150), CV_RGB(255, 100, 255));
	imshow("Mat", imageMat);

	imwrite("result.jpg", imageMat);

	while (waitKey(30) != 27);
}