MFC에 OpenCV 사용을 위한 기본 설정을 한 후 ( 다른 사이트들에 쉽게 설명되어 있음)
Dialog를 아래 그림 처럼 생성

Picture Control (IDC_IMG) 로 생성/ Button
CClientDC dc(GetDlgItem(IDC_IMG));
CRect rect;
GetDlgItem(IDC_IMG)->GetClientRect(&rect);
// 선언한 IDC_IMG에서 정보를 받아옴
SetStretchBltMode(dc.GetSafeHdc(), COLORONCOLOR);
// SetStretchBltMode : 이미지를 확대하거나 축소할 경우 생기는 손실을 보정해 주는 함수
// COLORONCOLOR : 픽셀을 삭제합니다. 제거된 픽셀 줄의 정보를 보존하지 않고 삭제합니다
CreateBitmapInfo(MatGray.cols, MatGray.rows, MatGray.channels() * 8);
// 비트맵이라는 것이 필요함 세부 함수는 아래에 따로...
StretchDIBits(dc.GetSafeHdc(), 0, 0, rect.Width(), rect.Height(), 0, 0, MatGray.cols, MatGray.rows, MatGray.data, m_pBitmapInfo, DIB_RGB_COLORS, SRCCOPY);
참고
- StretchDIBits은 Picture control에 이미지 띄우는 함수
- 맨 처음 0, 0 은 Picture control의 시작 위치 (Width, Height) 순서
- 다음 rect.Width(), rect.Height()은 Picture control 의 W, H 크기
- 다음 0, 0 은 이미지의 시작 위치 (cols, rows) 순서
- 다음 MatGray.cols, MatGray.rows 이미지의 cols, rows 크기
- MatGray2.data 이미지 데이터
- m_pBitmapInfo 위에서 만들어준 비트맵
- DIB_RGB_COLORS 색상 테이블 사용방식 지정
- SRCCOPY 원본 이미지 출력
if (m_pBitmapInfo != NULL)
{
delete m_pBitmapInfo;
m_pBitmapInfo = NULL;
}
if (bpp == 8)
m_pBitmapInfo = (BITMAPINFO *) new BYTE[sizeof(BITMAPINFO) + 255 * sizeof(RGBQUAD)];
else // 24 or 32bit
m_pBitmapInfo = (BITMAPINFO *) new BYTE[sizeof(BITMAPINFO)];
m_pBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_pBitmapInfo->bmiHeader.biPlanes = 1;
m_pBitmapInfo->bmiHeader.biBitCount = bpp;
m_pBitmapInfo->bmiHeader.biCompression = BI_RGB;
m_pBitmapInfo->bmiHeader.biSizeImage = 0;
m_pBitmapInfo->bmiHeader.biXPelsPerMeter = 0;
m_pBitmapInfo->bmiHeader.biYPelsPerMeter = 0;
m_pBitmapInfo->bmiHeader.biClrUsed = 0;
m_pBitmapInfo->bmiHeader.biClrImportant = 0;
if (bpp == 8)
{
for (int i = 0; i < 256; i++)
{
m_pBitmapInfo->bmiColors[i].rgbBlue = (BYTE)i;
m_pBitmapInfo->bmiColors[i].rgbGreen = (BYTE)i;
m_pBitmapInfo->bmiColors[i].rgbRed = (BYTE)i;
m_pBitmapInfo->bmiColors[i].rgbReserved = 0;
}
}
m_pBitmapInfo->bmiHeader.biWidth = w;
m_pBitmapInfo->bmiHeader.biHeight = -h;
여기서 w는 이미지의 cols, h는 이미지의 rows, bpp는 이미지의 채널 수.

'MFC' 카테고리의 다른 글
MFC / OpenCV / memory leak (0) | 2024.06.18 |
---|