[OpenCV] Mat ์ด๋ฏธ์ง๋ฅผ Picture Control์ ์ถ๋ ฅํ๊ธฐ
์ด๋ฒ ํฌ์คํ ์์๋ Mat ์ด๋ฏธ์ง๋ฅผ Picture Control์ ๊ทธ๋ฆฌ๋ ๊ฒ์ด ๋ชฉํ์ ๋๋ค.
์์ ํ๊ฒฝ์ Visual Studio 2015 MFC ์ ๋๋ค.
๊ธฐ๋ณธ์ ์ธ ํ๊ฒฝ ์ค์ ์ ์๋ ๊ธ์ ํ์ธํด์ฃผ์ธ์.
์ฐจ๋ก๋๋ก ์งํ์ ํด๋ณด๊ฒ ์ต๋๋ค.
โป ์ํ ์ฝ๋๋ ๋ณธ๋ฌธ ํ๋จ์ ์ฒจ๋ถ๋์ด ์์ต๋๋ค.
1. ๋ฆฌ์์ค ํธ์ง๊ธฐ์์ Picture Control์ ์ถ๊ฐํฉ๋๋ค.
ID๋ IDC_PC_VIEW๋ผ๊ณ ์ค์ ํ์ต๋๋ค.
2. ๋ฆฌ์์ค ํธ์ง๊ธฐ์์ Button์ ์ถ๊ฐํฉ๋๋ค.
ID๋ IDC_BTN_IMAGELOAD๋ผ๊ณ ์ค์ ํ์ต๋๋ค.
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();
}
}
์ด์ ๋น๋ ํ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
<๊ฒฐ๊ณผ ํ๋ฉด>
๋ณธ๋ฌธ ์ธ ์์ธํ ์ฝ๋ ๋ด์ฉ์ ์ฒจ๋ถ๋ ์์ ๋ฅผ ๊ผญ ํ์ธํด์ฃผ์ธ์.
'Programming > OpenCV' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[OpenCV] VTK 9.0.0๊ณผ OpenCV 4.4.0์ผ๋ก viz ๋ชจ๋ ํฌํจํ์ฌ ๋น๋ํ๊ธฐ! (9) | 2023.04.15 |
---|---|
[OpenCV] C#์์ OpenCVSharp4 ํ๊ฒฝ ๊ตฌ์ฑ ๋ฐ ์ฌ์ฉ ๋ฐฉ๋ฒ (์์ ํฌํจ) (2) | 2021.06.14 |
[OpenCV] ์นด๋ฉ๋ผ์ ๋ฒํผ ์ฐ๊ฒฐํ๊ธฐ (0) | 2019.04.21 |
[OpenCV] IplImage vs Mat ๋ฌด์์ ์ฌ์ฉํด์ผ ํ ๊น? (0) | 2019.03.04 |
[OpenCV] Visual Studio 2015์์ OpenCV 4.0.1 ์ฌ์ฉํ๊ธฐ (6) | 2019.03.03 |
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋ค๋ฅธ ๊ธ
-
[OpenCV] C#์์ OpenCVSharp4 ํ๊ฒฝ ๊ตฌ์ฑ ๋ฐ ์ฌ์ฉ ๋ฐฉ๋ฒ (์์ ํฌํจ)
[OpenCV] C#์์ OpenCVSharp4 ํ๊ฒฝ ๊ตฌ์ฑ ๋ฐ ์ฌ์ฉ ๋ฐฉ๋ฒ (์์ ํฌํจ)
2021.06.14 -
[OpenCV] ์นด๋ฉ๋ผ์ ๋ฒํผ ์ฐ๊ฒฐํ๊ธฐ
[OpenCV] ์นด๋ฉ๋ผ์ ๋ฒํผ ์ฐ๊ฒฐํ๊ธฐ
2019.04.21 -
[OpenCV] IplImage vs Mat ๋ฌด์์ ์ฌ์ฉํด์ผ ํ ๊น?
[OpenCV] IplImage vs Mat ๋ฌด์์ ์ฌ์ฉํด์ผ ํ ๊น?
2019.03.04 -
[OpenCV] Visual Studio 2015์์ OpenCV 4.0.1 ์ฌ์ฉํ๊ธฐ
[OpenCV] Visual Studio 2015์์ OpenCV 4.0.1 ์ฌ์ฉํ๊ธฐ
2019.03.03