๋ฐ˜์‘ํ˜•

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Mat ์ด๋ฏธ์ง€๋ฅผ Picture Control์— ๊ทธ๋ฆฌ๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค.

์ž‘์—… ํ™˜๊ฒฝ์€ Visual Studio 2015 MFC ์ž…๋‹ˆ๋‹ค.

๊ธฐ๋ณธ์ ์ธ ํ™˜๊ฒฝ ์„ค์ •์€ ์•„๋ž˜ ๊ธ€์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.

 

์ฐจ๋ก€๋Œ€๋กœ ์ง„ํ–‰์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

โ€ป ์ƒ˜ํ”Œ ์ฝ”๋“œ๋Š” ๋ณธ๋ฌธ ํ•˜๋‹จ์— ์ฒจ๋ถ€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

1. ๋ฆฌ์†Œ์Šค ํŽธ์ง‘๊ธฐ์—์„œ Picture Control์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

<Picture Control์„ ์ถ”๊ฐ€ํ•œ ๋ชจ์Šต>

ID๋Š” IDC_PC_VIEW๋ผ๊ณ  ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

<Picture Control ID>

 

 

2. ๋ฆฌ์†Œ์Šค ํŽธ์ง‘๊ธฐ์—์„œ Button์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

<Button Control์„ ์ถ”๊ฐ€ํ•œ ๋ชจ์Šต>

ID๋Š” IDC_BTN_IMAGELOAD๋ผ๊ณ  ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

 

 

<Button Control ID>

3. Main Dialog Header์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

class COpenCVTestDlg : public CDialogEx
{
// Construction
public:
	COpenCVTestDlg(CWnd* pParent = NULL);	// standard constructor

...
public :
	Mat m_matImage; // ์ด๋ฏธ์ง€ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด.
	BITMAPINFO *m_pBitmapInfo; // Bitmap ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ตฌ์กฐ์ฒด.

	void CreateBitmapInfo(int w, int h, int bpp); // Bitmap ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜.
	void DrawImage(); // ๊ทธ๋ฆฌ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜.
// Implementation
...
};

 

4. 'CreateBitmapInfo'๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

void COpenCVTestDlg::CreateBitmapInfo(int w, int h, int bpp)
{
	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;
}

 

5. 'DrawImage'๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

void COpenCVTestDlg::DrawImage()
{
	CClientDC dc(GetDlgItem(IDC_PC_VIEW));

	CRect rect;
	GetDlgItem(IDC_PC_VIEW)->GetClientRect(&rect);

	SetStretchBltMode(dc.GetSafeHdc(), COLORONCOLOR);
	StretchDIBits(dc.GetSafeHdc(), 0, 0, rect.Width(), rect.Height(), 0, 0, m_matImage.cols, m_matImage.rows, m_matImage.data, m_pBitmapInfo, DIB_RGB_COLORS, SRCCOPY);
}

 

6. ๋ฆฌ์†Œ์Šค ํŽธ์ง‘๊ธฐ์—์„œ ์ถ”๊ฐ€ํ•œ ๋ฒ„ํŠผ์— ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

์ƒ์„ฑ๋œ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

void COpenCVTestDlg::OnBnClickedBtnImageload()
{
	CFileDialog fileDlg(TRUE, NULL, NULL, OFN_READONLY, _T("image file(*.jpg;*.bmp;*.png;)|*.jpg;*.bmp;*.png;|All Files(*.*)|*.*||"));
	if (fileDlg.DoModal() == IDOK)
	{
		CString path = fileDlg.GetPathName();

		CT2CA pszString(path);
		std::string strPath(pszString);

		m_matImage = imread(strPath, IMREAD_UNCHANGED);

		CreateBitmapInfo(m_matImage.cols, m_matImage.rows, m_matImage.channels()*8);

		DrawImage();
	}
}

 

์ด์ œ ๋นŒ๋“œ ํ›„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<๊ฒฐ๊ณผ ํ™”๋ฉด>

 

๋ณธ๋ฌธ ์™ธ ์ž์„ธํ•œ ์ฝ”๋“œ ๋‚ด์šฉ์€ ์ฒจ๋ถ€๋œ ์˜ˆ์ œ๋ฅผ ๊ผญ ํ™•์ธํ•ด์ฃผ์„ธ์š”.

OpenCVTest.zip
๋‹ค์šด๋กœ๋“œ

 

๋ฐ˜์‘ํ˜•