//========= header 에 추가 =========================
void setPutText( IplImage* src_image, IplImage* dst_image, char* text, CvPoint textPos, CvScalar color);
HBITMAP setTextToBitmap( char* text, CvScalar color);
IplImage* setBitmapToIplImage( HBITMAP hBitmap );
//========== 소스코드 ===============================
void CbrailleDlg::setPutText(IplImage* src_image, IplImage* dst_image, char* text, CvPoint textPos, CvScalar color)
{
// 명암도 영상에 대한 처리는 안함 칼라만 처리 일단..
if( src_image->nChannels != 1 )
cvCopy( src_image, dst_image );
int width = src_image->width;
int height = src_image->height;
if( (textPos.y > src_image->height) || (textPos.x > src_image->width) )
{
cvCopy( src_image, dst_image );
}
CvScalar var = {0,0,0,0};
HBITMAP hBitmap = setTextToBitmap(text, color);
IplImage* text_image = setBitmapToIplImage( hBitmap );
for( int nRow = textPos.y; nRow<(textPos.y + text_image->height); nRow++)
{
for( int nCol = textPos.x; nCol<(textPos.x + text_image->width); nCol++)
{
if( nRow >= height || nCol >= width) continue;
var = cvGet2D(text_image, nRow-textPos.y, nCol-textPos.x);
if( var.val[0] == color.val[0] && var.val[1] == color.val[1] && var.val[2] == color.val[2] )
cvSet2D(dst_image, nRow, nCol, var);
}
}
free(text_image->imageData);
cvReleaseImage( &text_image );
DeleteObject( hBitmap );
}
HBITMAP CbrailleDlg::setTextToBitmap( char* text, CvScalar color)
{
int textLength = (int)strlen(text);
if( textLength <= 0 ) return NULL;
HDC hTextDC = CreateCompatibleDC(NULL);
HFONT hOldFont = (HFONT)SelectObject( hTextDC, (HFONT)GetStockObject(SYSTEM_FONT) );
HBITMAP hDstBMP = NULL;
RECT textArea = {0, 0, 0, 0};
DrawText(hTextDC, (LPCTSTR)text, textLength, &textArea, DT_CALCRECT);
if( (textArea.right > textArea.left) && (textArea.bottom > textArea.top) )
{
BITMAPINFOHEADER bitmapInfoHeader;
memset( &bitmapInfoHeader, 0x0, sizeof(BITMAPINFOHEADER) );
void* lpvBits = NULL;
bitmapInfoHeader.biSize = sizeof(bitmapInfoHeader);
bitmapInfoHeader.biWidth = textArea.right-textArea.left;
bitmapInfoHeader.biHeight = textArea.bottom-textArea.top;
bitmapInfoHeader.biPlanes = 1;
bitmapInfoHeader.biBitCount = 32;
bitmapInfoHeader.biCompression = BI_RGB;
hDstBMP = CreateDIBSection( hTextDC, (LPBITMAPINFO)&bitmapInfoHeader, 0, (LPVOID*)&lpvBits, NULL, 0 );
HBITMAP hOldBMP = (HBITMAP)SelectObject( hTextDC, hDstBMP );
if( hOldBMP != NULL)
{
int TEXT_RED = (int)color.val[2];
int TEXT_GREEN = (int)color.val[1];
int TEXT_BLUE = (int)color.val[0];
SetTextColor( hTextDC, RGB(TEXT_RED, TEXT_GREEN, TEXT_BLUE) ); //text color
SetBkColor( hTextDC, 0x00EFFEFF ); //Background color
SetBkMode( hTextDC, OPAQUE ); // 투명
DrawText( hTextDC, (LPCTSTR)text, textLength, &textArea, DT_NOCLIP);
}
::SelectObject( hTextDC, hOldBMP );
}
::SelectObject( hTextDC, hOldFont );
if( hTextDC) { ::DeleteDC( hTextDC ); }
if( hOldFont) { ::DeleteObject( hOldFont ); }
return hDstBMP;
}
IplImage* CbrailleDlg::setBitmapToIplImage( HBITMAP hBitmap )
{
BITMAP bitmap;
GetObject( hBitmap, sizeof( BITMAP ), (LPSTR)&bitmap );
int nChannels = (bitmap.bmBitsPixel == 1)? 1: bitmap.bmBitsPixel*0.125;
int depth = (bitmap.bmBitsPixel == 1)? IPL_DEPTH_1U : IPL_DEPTH_8U;
IplImage* mImage = cvCreateImageHeader( cvSize(bitmap.bmWidth, bitmap.bmHeight), depth, nChannels );
mImage->imageData = (char*)malloc(bitmap.bmHeight * bitmap.bmWidth * nChannels * sizeof(char) );
memcpy(mImage->imageData, (char*)(bitmap.bmBits), bitmap.bmHeight * bitmap.bmWidth * nChannels );
cvFlip( mImage, mImage, 0 );
return mImage;
}
//================사용 예 ==========================
CString str;
str.Format( _T("한글") );
setPutText( &IplImage(m_Image), &IplImage(m_Image), (LPSTR)(LPCTSTR)str, cvPoint(10,10), CV_RGB(255,255,100) );
'Programming > OpenCV' 카테고리의 다른 글
[Function] HoG Destriptor in OpenCV (0) | 2014.01.27 |
---|---|
[Function] 영상을 회전 시키기 (0) | 2014.01.23 |
DC -> IplImage로 전환 (0) | 2013.08.08 |
cv::Mat Class 사용법 (0) | 2013.05.06 |
OpenCV 주요함수 (0) | 2013.04.29 |