How to convert cv::Mat to MFC CBitmap - c++

how can I convert an OpenCV image to an Microsoft Foundation Classes (MFC) CBitmap object?
I tried the following, which failed,
cv::Mat tmp;
(Load opencv image ...)
cv::Size size = tmp.size();
CBitmap bitmap;
// 3 colors (RGB), 24bits (8bits*3channels)
if (!bitmap.CreateBitmap(128, 128, 1, 24, (void *)tmp.data)) {
TRACE0("Failed to create bitmap for image display\n");
return;
}
this results in a black image..

This post shares the method of converting an IplImage to CBitmap:
CBitmap* IplImageToCBitmap(IplImage* img)
{
CDC dc;
CDC memDC;
if (!dc.CreateDC("DISPLAY", NULL, NULL, NULL))
return NULL;
if (!memDC.CreateCompatibleDC(&dc))
return NULL;
CBitmap* bmp = new CBitmap();
CBitmap* pOldBitmap;
bmp->CreateCompatibleBitmap(&dc, img->width, img->height);
pOldBitmap = memDC.SelectObject(bmp);
CvvImage cvImage; // you will need OpenCV_2.2.0- to use CvvImage
cvImage.CopyOf(img);
cvImage.Show(memDC.m_hDC, 0, 0, img->width, img->height, 0, 0);
cvImage.Destroy();
memDC.SelectObject(pOldBitmap);
memDC.DeleteDC();
dc.DeleteDC();
return bmp;
}
Based on this, you can achieve your goal by calling it as follows:
cv::Mat aMat;
CBitmap *aCBitmap = IplImageToCBitmap((IplImage*) &aMat);

Related

How to turn a screenshot bitmap to a cv::Mat

I currently am trying to take a screenshot of the screen, and then get it into a format editable by OpenCV. The code I'm using is from the microsoft website, https://learn.microsoft.com/en-gb/windows/win32/gdi/capturing-an-image. The code uses the "Windows.h" library. The easiest way of doing it is obviously to just save the bitmap as a .bmp, then open it using opencv. However, I would like it to be more efficient than that, and I don't know how to. When I used the code, it outputted a char pointer, which I don't know how to convert to a cv::Mat. The code is below:
cv::Mat * Capture::GetMat()
{
cv::Mat * mat1;
MemoryHandle = NULL;
BitmapHandle = NULL;
// Find the handle for the device context of the entire screen, and the specific window specified.
ScreenHandle = GetDC(NULL);
WindowHandle = GetDC(hwnd);
//Make the compatible DC (Device Context) for storing the data in memory.
MemoryHandle = CreateCompatibleDC(WindowHandle);
//Make a compatible DC for the bitmap to be stored in.
BitmapHandle = CreateCompatibleBitmap(WindowHandle, width, height);
//Select the correct bitmap, and put it into memory using the memory handle.
SelectObject(MemoryHandle, BitmapHandle);
//Transfer the actual bitmap into the compatible memory DC.
BitBlt(MemoryHandle, 0, 0, 1920, 1080, WindowHandle, 0, 0, SRCCOPY);
//Get the bitmap from the handle, and ready it to be filed.
GetObject(BitmapHandle, sizeof(BITMAP), &Bitmap);
//Cofinguring INFO details.
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfoHeader.biWidth = Bitmap.bmWidth;
bmpInfoHeader.biHeight = Bitmap.bmHeight;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biBitCount = 32;
bmpInfoHeader.biCompression = BI_RGB;
bmpInfoHeader.biSizeImage = 0;
bmpInfoHeader.biXPelsPerMeter = 0;
bmpInfoHeader.biYPelsPerMeter = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biClrImportant = 0;
bmpSize = ((Bitmap.bmWidth * bmpInfoHeader.biBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
memhnd = GlobalAlloc(GHND, bmpSize);
mat1 = (cv::Mat *)GlobalLock(memhnd);
std::cout << GetLastError() << std::endl;
return mat1;
}
int Capture::save_mat(cv::Mat * mat)
{
std::string FileName("P:/Photos/capture");
FileName += std::to_string(image_count_mat);
FileName += (const char*)(".jpg");
cv::Mat mat2 = *mat;
cv::imwrite(FileName.c_str(), mat2);
image_count_mat++;
return 0;
}
The class has these attributes:
private:
HWND hwnd;
HDC hdc;
int image_count_bitmap = 0;
int image_count_mat = 0;
int height;
int width;
HDC ScreenHandle;
HDC WindowHandle;
HDC MemoryHandle = NULL;
HBITMAP BitmapHandle = NULL;
BITMAP Bitmap;
BITMAPFILEHEADER bmpFileHeader;
BITMAPINFOHEADER bmpInfoHeader;
DWORD bmpSize;
HANDLE memhnd;
The GetMat() function works fine and doesn't output an error, although I have no idea how to check if the outputted cv::Mat is correct. When I run the save_mat() function however, the program crashes.
It is not recommended to store device context handles. For example a call to GetDC should be followed by ReleaseDC as soon as you are finished with the handle. You can store bitmap handles and memory dc, but in most cases it is not necessary.
Once you have copied the image in to bitmap, use GetDIBits to copy the bits in to cv::Mat as shown in the example below.
Note that your application needs DPI compatibility, for example with SetProcessDPIAware to find the correct desktop size.
This example uses 32-bit bitmap with CV_8UC4, but these GDI functions are 24-bit. You can also use 24-bit bitmap with CV_8UC3.
void screenshot()
{
auto w = GetSystemMetrics(SM_CXFULLSCREEN);
auto h = GetSystemMetrics(SM_CYFULLSCREEN);
auto hdc = GetDC(HWND_DESKTOP);
auto hbitmap = CreateCompatibleBitmap(hdc, w, h);
auto memdc = CreateCompatibleDC(hdc);
auto oldbmp = SelectObject(memdc, hbitmap);
BitBlt(memdc, 0, 0, w, h, hdc, 0, 0, SRCCOPY);
cv::Mat mat(h, w, CV_8UC4);
BITMAPINFOHEADER bi = { sizeof(bi), w, -h, 1, 32, BI_RGB };
GetDIBits(hdc, hbitmap, 0, h, mat.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
cv::imwrite("screenshot.png", mat);
SelectObject(memdc, oldbmp);
DeleteDC(memdc);
DeleteObject(hbitmap);
ReleaseDC(HWND_DESKTOP, hdc);
}

C++ How to display Imebra image (char buffer) as an image?

I use an Imebra library to read DICOM files. I need to display DICOM image. Imebra library gives me an image in buffer (I follow this documentation):
load file
std::unique_ptr<DataSet> MyDataSet(CodecFactory::load("IM1"));
retrieving an image
Image* image(MyDataSet->getImageApplyModalityTransform(0));
int iWidth = image->getWidth();
int iHeight = image->getHeight();
TransformsChain chain;
if (ColorTransformsFactory::isMonochrome(image->getColorSpace()))
{
VOILUT voilutTransform;
vois_t vois = MyDataSet->getVOIs();
list<LUT*> luts;
for (size_t scanLUTs(0); ; ++scanLUTs)
{
try { luts.push_back(MyDataSet->getLUT(TagId(tagId_t::VOILUTSequence_0028_3010), scanLUTs)); }
catch (const MissingDataElementError&) { break; }
}
if (!vois.empty()) voilutTransform.setCenterWidth(vois[0].center, vois[0].width);
else if (!luts.empty()) voilutTransform.setLUT(*(luts.front()));
else voilutTransform.applyOptimalVOI(*image, 0, 0, iWidth, iHeight);
DrawBitmap draw(chain);
size_t requestedBufSize = draw.getBitmap(*image, drawBitmapType_t::drawBitmapRGBA, 4, 0, 0);
string buffer(requestedBufSize, char(0));
draw.getBitmap(*image, drawBitmapType_t::drawBitmapRGBA, 4, &buffer.at(0), requestedBufSize);
So I got a buffer with bitmap inside it. (following each word of documentation).
Then I get bitmap from this buffer:
hBitmap = CreateBitmap(iWidth, iHeight, 1, 24, buffer);
Now I'm trying to dispay it in MFC PictureControl (on dialog window):
void CImebraDlg::OnPaint()
{
CPaintDC dc(this);
CDC memdc;
BITMAP b;
//m_DicomImage - CStatic variable of PictureControl
m_DicomImage.GetClientRect(&rect);
::GetObject(theApp.hBitmap, sizeof(BITMAP), &b);
memdc.CreateCompatibleDC(&dc);
memdc.SelectObject(&bmp);
dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &memdc,
0, 0, b.bmWidth, b.bmHeight, SRCCOPY);
dc.MoveTo(0, 0);
}
And nothig happens, no image in PictureControl.
Trying like this:
void CImebraDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetImageToRictureControl(theApp.hBitmap);
}
void CImebraDlg::SetImageToRictureControl(HBITMAP hbmp)
{
m_DicomImage.SetBitmap(hbmp);
UpdateData(FALSE);
}
the same result...
BITMAP structure you can see on the screenshot.
bmBits is NULL.. Is this the reason of not displaying dicom image?
How to display a DICOM image right with imebra lib?

OpenCV 2.4 : Displaying a cv::Mat in MFC

I am updating an existing code base from IplImage* to the newer cv::Mat. I was wondering how to display my cv::Mat object to MFC. The current solution we are using is based on the old CvvImage class:
void DrawPicToHDC(IplImage *img, UINT ID, bool bOnPaint)
{
CDC *pDC = GetDlgItem(ID)->GetDC();
HDC hDC= pDC->GetSafeHdc();
CRect rect;
GetDlgItem(ID)->GetClientRect(&rect);
CvvImage cimg;
cimg.CopyOf( img );
cimg.DrawToHDC( hDC, &rect );
ReleaseDC( pDC );
}
I came across this thread but unfortunately the answer provided doesn't meet my needs because the answers still require you to convert the Mat to an IplImage* before displaying.
Is there any way to do this using cv::Mat only? Any help is much appreciated.
UPDATE:
I adapted the above function to using cv::Mat with the help from Kornel's answer. I now do not need to include the CvvImage class:
void DrawPicToHDC(cv::Mat cvImg, UINT ID, bool bOnPaint)
{
// Get the HDC handle information from the ID passed
CDC *pDC = GetDlgItem(ID)->GetDC();
HDC hDCDst = pDC->GetSafeHdc();
CRect rect;
GetDlgItem(ID)->GetClientRect(&rect);
cv::Size winSize(rect.right, rect.bottom);
// Resize the source to the size of the destination image if necessary
cv::Mat cvImgTmp(winSize, CV_8UC3);
if (cvImg.size() != winSize)
{
cv::resize(cvImg, cvImgTmp, winSize);
}
else
{
cvImgTmp = cvImg;
}
// Rotate the image
cv::flip(cvImgTmp,cvImgTmp,0);
// Initialize the BITMAPINFO structure
BITMAPINFO bitInfo;
bitInfo.bmiHeader.biBitCount = 24;
bitInfo.bmiHeader.biWidth = winSize.width;
bitInfo.bmiHeader.biHeight = winSize.height;
bitInfo.bmiHeader.biPlanes = 1;
bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitInfo.bmiHeader.biCompression = BI_RGB;
bitInfo.bmiHeader.biClrImportant = 0;
bitInfo.bmiHeader.biClrUsed = 0;
bitInfo.bmiHeader.biSizeImage = 0;
bitInfo.bmiHeader.biXPelsPerMeter = 0;
bitInfo.bmiHeader.biYPelsPerMeter = 0;
// Add header and OPENCV image's data to the HDC
StretchDIBits(hDCDst, 0, 0,
winSize.width, winSize.height, 0, 0,
winSize.width, winSize.height,
cvImgTmp.data, &bitInfo, DIB_RGB_COLORS, SRCCOPY);
ReleaseDC( pDC );
}
Suppose we have an OpenCV image cv::Mat cvImg which one should be converted to CImage* mfcImg.
Of course the MFC image should be displayed on an MFC window i.e. in CStatic winImg
So the following transformations should be performed in order to display an OpenCV image in an MFC window:
cv::Mat -> CImage -> CStatic
Define MFC window size:
RECT r;
winImg.GetClientRect(&r);
cv::Size winSize(r.right, r.bottom);
The size of cvImg is not always the same as an MFC window’s:
cv::Mat cvImgTmp(winSize, CV_8UC3);
if (cvImg.size() != winSize)
{
cv::resize(cvImg, cvImgTmp, winSize);
}
else
{
cvImgTmp = cvImg.clone();
}
Rotate the image:
cv::flip(cvImgTmp, cvImgTmp, 0);
Create an MFC image:
if (mfcImg)
{
mfcImg->ReleaseDC();
delete mfcImg; mfcImg = nullptr;
}
mfcImg = new CImage();
mfcImg->Create(winSize.width, winSize.height, 24);
For mfcImg you need a header. Create it by using BITMAPINFO structure
BITMAPINFO bitInfo;
bitInfo.bmiHeader.biBitCount = 24;
bitInfo.bmiHeader.biWidth = winSize.width;
bitInfo.bmiHeader.biHeight = winSize.height;
bitInfo.bmiHeader.biPlanes = 1;
bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitInfo.bmiHeader.biCompression = BI_RGB;
bitInfo.bmiHeader.biClrImportant = 0;
bitInfo.bmiHeader.biClrUsed = 0;
bitInfo.bmiHeader.biSizeImage = 0;
bitInfo.bmiHeader.biXPelsPerMeter = 0;
bitInfo.bmiHeader.biYPelsPerMeter = 0;
Add header and OpenCV image’s data to mfcImg
StretchDIBits(mfcImg->GetDC(), 0, 0,
winSize.width, winSize.height, 0, 0,
winSize.width, winSize.height,
cvImgTmp.data, &bitInfo, DIB_RGB_COLORS, SRCCOPY
);
Display mfcImg in MFC window
mfcImg->BitBlt(::GetDC(winImg.m_hWnd), 0, 0);
Release mfcImg, if you will not use it:
if (mfcImg)
{
mfcImg->ReleaseDC();
delete mfcImg; mfcImg = nullptr;
}

Conversion from MFC CImage to Opencv IPlImage

I am having a camera application using opencv where I am using MFC GUI.I need to convert the CImage to IplImage for the Opencv modules for image processing and convert it back to CImage for displaying in the window again.
I researched in this topic but not enough examples and solutions.Anybody have some suggestions.Thanks
This is my code...
void CChildView::OnFileOpenimage(void)
{
// TODO: Add your command handler code here
CString strFilter;
CSimpleArray<GUID> aguidFileTypes;
HRESULT hResult;
hResult = imgOriginal.GetExporterFilterString(strFilter,aguidFileTypes);
if (FAILED(hResult)) {
CString fmt;
fmt.Format("GetExporterFilter failed:\n%x - %s", hResult, _com_error(hResult).ErrorMessage());
::AfxMessageBox(fmt);
return;
}
CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, strFilter);
dlg.m_ofn.nFilterIndex = m_nFilterLoad;
hResult = (int)dlg.DoModal();
if(FAILED(hResult)) {
return;
}
m_nFilterLoad = dlg.m_ofn.nFilterIndex;
imgOriginal.Destroy();
CString pathval = dlg.GetPathName();
hResult = imgOriginal.Load(dlg.GetPathName());
if (FAILED(hResult)) {
CString fmt;
fmt.Format("Load image failed:\n%x - %s", hResult, _com_error(hResult).ErrorMessage());
::AfxMessageBox(fmt);
return;
}
// IplImage *img from imgOriginal; want to convert here for further processing.
m_nImageSize = SIZE_ORIGINAL;
Invalidate();
UpdateWindow();
}
Had this problem for quite a while myself. I found this code here which I ended up using that worked quite well. Instead of researching "CImage to IplImage" you should search "HBITMAP to IplImage"
IplImage* hBitmap2Ipl(HBITMAP hBmp, bool flip)
{
BITMAP bmp;
::GetObject(hBmp,sizeof(BITMAP),&bmp);
int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8;
int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
IplImage *img=cvCreateImageHeader(cvSize(bmp.bmWidth, bmp.bmHeight), depth, nChannels);
img->imageData = (char*)malloc(bmp.bmHeight*bmp.bmWidth*nChannels*sizeof(char));
memcpy(img->imageData,(char*)(bmp.bmBits),bmp.bmHeight*bmp.bmWidth*nChannels);
return img;
}
Usage:
ATL::CImage image;
//whatever code you use to generate the image goes here
IplImage *convertedImage=hBitmap2Ipl(image.Detach());
image.Destroy();
I searched half the night for my copy-and-paste memory leak and finally found it!
So here is an updated code snippet of the original answer. The important thing for me was to use cvCreateImage instead of cvCreateImageHeader because I did use cvReleaseImage after the call to hBitmap2IplImage. Also flipping was neccessary in my case. Cheers!
IplImage* hBitmap2IplImage(HBITMAP hBmp, bool flip)
{
BITMAP bmp;
::GetObject(hBmp,sizeof(BITMAP),&bmp);
int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8;
int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
IplImage *img=cvCreateImage(cvSize(bmp.bmWidth, bmp.bmHeight), depth, nChannels);
size_t imgSize = bmp.bmHeight*bmp.bmWidth*nChannels;
memcpy_s(img->imageData, imgSize, (char*)(bmp.bmBits), imgSize);
if (flip)
cvFlip(img, NULL, 0);
return img;
}
Usage:
ATL::CImage image;
HBITMAP hBitmap = image.Detach(); // some handle to your bitmap
bool flip = true;
IplImage* pSomeIplImage = hBitmap2IplImage(hBitmap, flip);
image.Destroy();
//
// use "pSomeIplImage"
//
cvReleaseImage( &pSomeIplImage ); // don't forget
If a bitmap image is on a view you can use the device context(CDC dcBuffer ) to copy it.
IplImage *pCapture = cvCreateImage(cvSize(U16_IMAGE_WIDTH/2,U16_IMAGE_HEIGHT/2),8, 3);
::GetDIBits(dcBuffer.GetSafeHdc(), bitmapBuffer, 0, U16_IMAGE_HEIGHT/2, pCapture->imageData, &bitmapInfo, DIB_RGB_COLORS);
cvSaveImage("temp.bmp", pCapture);
cvReleaseImage(&pCapture);

how to load image from file using MFC

My browse button code is
void CFileOpenDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE);
int result=dlg.DoModal();
if(result==IDOK)
{
path=dlg.GetPathName();
UpdateData(FALSE);
}
}
and this is the code for loading an image from resource but that does not work for loading an image from file. i know LoadImage(); is used for this but how? how can i edit this code to load image from file. Plzz help.....
void CFileOpenDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
CRect r;
CBitmap* m_bitmap;
CDC dc, *pDC;
BITMAP bmp;
m_bitmap = new CBitmap();
m_bitmap->LoadBitmapW(IDB_BITMAP1);
m_bitmap->GetBitmap(&bmp);
pDC = this->GetDC();
dc.CreateCompatibleDC(pDC);
dc.SelectObject(m_bitmap);
pDC->BitBlt(200, 200, bmp.bmWidth, bmp.bmHeight, &dc,0 , 0, SRCCOPY);
m_bitmap->DeleteObject();
m_bitmap->Detach();
}
MSDN LoadImage
HANDLE hBitMap = ::LoadImage(0, "c:\\mybmp.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
CBitmap bmp;
bmp.Attach((HBITMAP)hBitMap);
If you want to open .JPG, .PNG ... eventually you can use CImage (is a shared class between MFC and ATL)
CImage image;
image.Load ( "picture.jpg" );
image.Draw ( pDC , 200, 200 );