⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmatcom_imageview.cpp

📁 《精通matlab与c++混合编程》的光盘内容
💻 CPP
字号:
// testmatcom_imageView.cpp : implementation of the CTestmatcom_imageView class
//

#include "stdafx.h"
#include "testmatcom_image.h"

#include "testmatcom_imageDoc.h"
#include "testmatcom_imageView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_imageView

IMPLEMENT_DYNCREATE(CTestmatcom_imageView, CView)

BEGIN_MESSAGE_MAP(CTestmatcom_imageView, CView)
	//{{AFX_MSG_MAP(CTestmatcom_imageView)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_WM_SIZE()
	ON_COMMAND(ID_FFT, OnFft)
	ON_COMMAND(ID_LargeToSmall, OnLargeToSmall)
	ON_COMMAND(ID_SmallToLarge, OnSmallToLarge)
	ON_COMMAND(ID_RESET, OnReset)
	ON_COMMAND(ID_COOL, OnCool)	
	ON_COMMAND(ID_GRAY, OnGray)
	ON_COMMAND(ID_JET, OnJet)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_imageView construction/destruction

CTestmatcom_imageView::CTestmatcom_imageView()
{
	// TODO: add construction code here
	isInit = false;
	if(isInit == false)
	{
		initM(MATCOM_VERSION);
		isInit=true;
	}
}

CTestmatcom_imageView::~CTestmatcom_imageView()
{
	if(isInit)
	{
		exitM();
		isInit=false;
	}
}

BOOL CTestmatcom_imageView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_imageView drawing

void CTestmatcom_imageView::OnDraw(CDC* pDC)
{
	CTestmatcom_imageDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_imageView printing

BOOL CTestmatcom_imageView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTestmatcom_imageView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTestmatcom_imageView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_imageView diagnostics

#ifdef _DEBUG
void CTestmatcom_imageView::AssertValid() const
{
	CView::AssertValid();
}

void CTestmatcom_imageView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTestmatcom_imageDoc* CTestmatcom_imageView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestmatcom_imageDoc)));
	return (CTestmatcom_imageDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_imageView message handlers
/*打开图像文件*/
void CTestmatcom_imageView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	static char BASED_CODE szFilter[] = 
		"BMP 格式文件(*.bmp)|*.bmp|所有格式的文件(*.*)|*.*||";
	CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,this);
	if(dlg.DoModal()==IDOK)
	{
		CString filename = dlg.GetPathName();
		Mm mFileName;
		ChangeStrToMm(mFileName, filename);
		if(!this->IsMatcomHandleValid(m_h))
		{
			m_h = winaxes(this->m_hWnd);		
		}
		imread(mFileName,TM("*.bmp"),i_o,m_data,m_map);
		int * pdims = m_data.getdims();
		int ndims = m_data.getndims();		
		int nrow = m_data.rows();
		int ncol = m_data.cols();
		m_data = im2double(m_data);
		if(ndims == 3 )
		{
			Mm m_data1,maxdata;
			rgb2gray(m_data,m_data1);
			m_data = m_data1;
			maxdata = max(max(max(m_data)));
			m_data = (m_data/maxdata)*255;
		}		
		set(m_h,TM("Cdata"),m_data);
		colormap(gray());		
		drawnow();		
	}
}

/*将C字符串转换为MATCOM Mm类型字符串矩阵*/
void CTestmatcom_imageView::ChangeStrToMm(Mm &mstring, CString instring)
{
	int nlen = instring.GetLength();
	mstring = zeros(1,nlen);
	mstring.setstr(1);
	for(int i=0;i<nlen;i++)
	{
		mstring.r(i+1) = instring.GetAt(i);
	}
}

void CTestmatcom_imageView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if(this->IsMatcomHandleValid(this->m_h))
	{
		Mm pos;
		pos = zeros(1,4);
		pos.r(1)=0;pos.r(2)=0;
		pos.r(3)=cx;pos.r(4)=cy;
		set(m_h,TM("RealPosition"),pos);
	}	
}

/*判断Matcom 图形窗口句柄的有效性*/
bool CTestmatcom_imageView::IsMatcomHandleValid(Mm handle)
{
	if(this->isInit)
	{
		Mm len,isH;		
		isH = ishandle(handle);
		len = length(isH);
		if(((int)(len.r(1)))&&((int)(isH.r(1))))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

/*对当前图像进行FFT,并显示结果*/
void CTestmatcom_imageView::OnFft() 
{
	// TODO: Add your command handler code here
	if(this->isInit)
	{
		if(this->IsMatcomHandleValid(this->m_h))
		{
			
			set(m_h,TM("Cdata"),mabs(fft2(m_data)));
		}
	}
}

/*将当前图像缩小显示*/
void CTestmatcom_imageView::OnLargeToSmall() 
{
	// TODO: Add your command handler code here
	if(this->isInit)
	{
		if(this->IsMatcomHandleValid(this->m_h))
		{
			axes(CL(m_h));			
			Mm pos;
			Mm xa,ya;
			xa = get(m_h,TM("Xdata"));
			ya = get(m_h,TM("Ydata"));
			pos = zeros(1,4);
			pos.r(1)=0;pos.r(2)=0;
			pos.r(3)=xa.r(2);pos.r(4)=ya.r(2);
			set(m_h,TM("RealPosition"),pos);			
		}
	}
}

/*将当前图像放大显示*/
void CTestmatcom_imageView::OnSmallToLarge() 
{
	// TODO: Add your command handler code here
	if(this->isInit)
	{
		if(this->IsMatcomHandleValid(this->m_h))
		{			
			CRect rect;
			GetClientRect(&rect);
			Mm pos;
			pos = zeros(1,4);
			pos.r(1)=0;pos.r(2)=0;
			pos.r(3)=rect.Width();pos.r(4)=rect.Height();
			set(m_h,TM("RealPosition"),pos);
		}
	}	
}

/*显示原始图像*/
void CTestmatcom_imageView::OnReset() 
{
	// TODO: Add your command handler code here
	if(this->isInit)
	{
		if(this->IsMatcomHandleValid(this->m_h))
		{
			set(m_h,TM("Cdata"),m_data);			
		}
	}	
}

/*改变当前调色板为cool*/
void CTestmatcom_imageView::OnCool() 
{
	// TODO: Add your command handler code here
	if(this->isInit)
	{
		if(this->IsMatcomHandleValid(this->m_h))
		{
			colormap(cool());
		}
	}	
}

/*将当前输入RGB24位真彩图像转换为灰度图像*/
void CTestmatcom_imageView::rgb2gray(Mm &m_data, Mm &m_outData)
{
	if(this->isInit)
	{
		if(this->IsMatcomHandleValid(this->m_h))
		{
			int ndims;
			ndims = m_data.getndims();
			if(ndims != 3)
			{
				return;
			}
			else
			{
				m_outData = zeros(m_data.rows(),m_data.cols());
				for(int i=1;i<=m_data.rows();i++)
				{
					for(int j=1;j<=m_data.cols();j++)
					{
						m_outData.r(i,j) = m_data.r(i,j,1)*0.3+m_data.r(i,j,2)*0.587+m_data.r(i,j,3)*0.114;
					}
				}
			}
		}
	}
}
/*将当前图像调色板转换为灰度调色板*/
void CTestmatcom_imageView::OnGray() 
{
	// TODO: Add your command handler code here
	if(this->isInit)
	{
		if(this->IsMatcomHandleValid(this->m_h))
		{
			colormap(gray());
		}
	}
}

/*将当前调色板转换为jet调色板*/
void CTestmatcom_imageView::OnJet() 
{
	// TODO: Add your command handler code here
	if(this->isInit)
	{
		if(this->IsMatcomHandleValid(this->m_h))
		{
			colormap(jet());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -