OPENCV+MFC单文档显示图像的两种方法

OPENCV与MFC混合开发图像显示的两种方法:

工程建好后,右击文档类选择建立类向导,选择虚函数中的OnOpenDocument,编辑

在文档类里面添加Mat img;和string path;

同时需要添加头文件#include<opencv2/opencv.hpp>

BOOL CsplitDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	// TODO:  Add your specialized creation code here
	path = CT2A(lpszPathName);
	// TODO:  Add your specialized creation code here
	img = cv::imread(path);
	if (img.empty())
	{
		MessageBox(NULL, lpszPathName, _T("did't load the pic"), MB_OK);
	}
	return TRUE;
}

方法一,进入视图类,编辑OnDraw函数

同时要包含头文件#include<opencv2/opencv.hpp>

void CsplitView::OnDraw(CDC* pDC)
{
	CsplitDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	cv::Mat newImage;
	if (pDoc->img.channels() == 1)
	{
		cv::cvtColor(pDoc->img, newImage, CV_GRAY2BGRA);
	}
	else if (pDoc->img.channels() == 3)
	{
		cv::cvtColor(pDoc->img, newImage, CV_BGR2BGRA);
	}
	else
	{
		newImage = pDoc->img;
	}
	// TODO: add draw code for native data here
	if (!pDoc->img.empty())
	{
		CImage  image;
		int cx, cy;
		CRect   rect;
		std::wstring str;
		std::string path1 = pDoc->path;
		StringToWString(path1, str);
		image.Load((LPCTSTR)str.c_str());

		CImage image2 = image;

		//获取图片的宽 高  
		SetRect(rect, 0, 0, newImage.cols, newImage.rows);

		pDC = GetDC();//获取picture control的DC
		image.Draw(pDC->m_hDC, rect);
		ReleaseDC(pDC);
	}
	
}

方法一中用到的StringToWString函数如下

BOOL StringToWString(const std::string &str,std::wstring &wstr)
 {    
     int nLen = (int)str.length();    
     wstr.resize(nLen,L' ');
 
     int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);
 
     if (nResult == 0)
     {
         return FALSE;
     }
 
     return TRUE;
 }
这个函数使用的时候要位置要放在OnDraw函数的前面即可;

方法二:1.打开视图类里面的OnDraw函数

void CAgainTestView::OnDraw(CDC* pDC)
{
	CAgainTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	// TODO: 在此处为本机数据添加绘制代码  
	cv::Mat newImage;
	if (pDoc->img.channels() == 1)
	{
		cv::cvtColor(pDoc->img, newImage, CV_GRAY2BGRA);
	}
	else if (pDoc->img.channels() == 3)
	{
		cv::cvtColor(pDoc->img, newImage, CV_BGR2BGRA);
	}
	else
	{
		newImage = pDoc->img;
	}

	Gdiplus::Bitmap bitmap(newImage.cols, newImage.rows, newImage.step1(), PixelFormat32bppARGB, newImage.data);
	Gdiplus::Graphics graphics(pDC->GetSafeHdc());
	graphics.DrawImage(&bitmap, 0, 0);

	// TODO: add draw code for native data here
	Gdiplus::Bitmap bitmap1(newImage.cols, newImage.rows, newImage.step1(), PixelFormat32bppARGB, newImage.data);
	graphics.DrawImage(&bitmap1, newImage.cols, 0);
	CSize scroll_size;
	scroll_size.cx = newImage.cols * 2;
	scroll_size.cy = newImage.rows;
	SetScrollSizes(MM_TEXT, scroll_size);
	ReleaseDC(pDC);
}
然后进入APP头文件中添加
#include<gdiplus.h>
using namespace Gdiplus;
class CAgainTestApp : public CWinAppEx
{
public:
	CAgainTestApp();
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
然后进入APP的.cpp中添加如下

	// The one and only window has been initialized, so show and update it
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	return TRUE;
}

int CAgainTestApp::ExitInstance()
{
	//TODO: handle additional resources you may have added
	AfxOleTerm(FALSE);
	GdiplusShutdown(gdiplusToken);
	return CWinAppEx::ExitInstance();
}
单文档图像显示




  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值