VS2010平台下ImageDib类开发----------------------二值化

1、新建 【注意是多文档】   项目名为“Pic”


2、 修改 Menu 


 可以新建Menu , 

BOOL CPicApp::InitInstance(){
                pDocTemplate = new CMultiDocTemplate(
		IDR_PicTYPE,  /*这里修改新建的ID*/
		RUNTIME_CLASS(CPicDoc),
		RUNTIME_CLASS(CChildFrame), // 自定义 MDI 子框架
		RUNTIME_CLASS(CPicView));
}

3、 各种灰度变换算法,以“二值化算法”为例。

.h

#include "Imagedib.h"

class GrayTrans : public ImageDib
{
public: 
	 int m_nBitCountOut ;
	 unsigned char * m_pImgDataOut ;
	 LPRGBQUAD m_lpColorTableOut ;
	 int m_imgHeightOut ;
	 int m_imgWidthOut  ;
	 int m_nColorTableLengthOut ;
public:
	GrayTrans(void);
	~GrayTrans(void);
	GrayTrans(CSize size , int nBitCount , LPRGBQUAD lpColorTable , unsigned char *pImgData) ;
public: 
	CSize GetDimensions() ; 
	void  BinaryImage(int threshold = 128) ;

};


.cpp   (只展示灰度图片部分)

GrayTrans::GrayTrans(void)
{   
	m_nBitCountOut = 0 ;
	m_pImgDataOut = NULL ;
	m_lpColorTableOut = NULL ;
	m_imgHeightOut = 0  ;
	m_imgWidthOut  = 0 ;
	m_nColorTableLengthOut = 0 ;
}


GrayTrans::~GrayTrans(void)
{
	if(m_lpColorTableOut != NULL){
		 delete [] m_lpColorTableOut  ;
		 m_lpColorTableOut = NULL ;
	}
	if(m_pImgDataOut != NULL){
		 delete [] m_pImgDataOut  ;
		 m_pImgDataOut = NULL  ;
	}
}

GrayTrans::GrayTrans(CSize size , int nBitCount , LPRGBQUAD lpColorTable , unsigned char *pImgData)
	:ImageDib(size , nBitCount , lpColorTable , pImgData)
{
	m_nBitCountOut = 0 ;
	m_pImgDataOut = NULL ;
	m_lpColorTableOut = NULL ;
	m_imgHeightOut = 0  ;
	m_imgWidthOut  = 0 ;
	m_nColorTableLengthOut = 0 ;
}

CSize GrayTrans::GetDimensions(){
	  if(m_pImgDataOut == NULL) return CSize(0 , 0) ;
	  else  return CSize(m_imgWidthOut , m_imgHeightOut) ;
}

void  GrayTrans::BinaryImage(int threshold){
	  if (m_nBitCount == 8){
		  if(m_pImgDataOut != NULL){
			  delete []m_pImgDataOut ;
			  m_pImgDataOut = NULL ;
		  }
		  if (m_lpColorTableOut != NULL){
			  delete []m_lpColorTableOut ;
			  m_lpColorTableOut = NULL ;
		  }

		  m_nBitCountOut = m_nBitCount ;
		  m_nColorTableLengthOut = ComputeColorTabalLength(m_nBitCountOut) ;
		   
		  m_lpColorTableOut = new RGBQUAD[m_nColorTableLengthOut] ;
		  memcpy(m_lpColorTableOut , m_lpColorTable , sizeof(RGBQUAD) * m_nColorTableLengthOut) ;

		  m_imgHeightOut = m_imgHeight ;
		  m_imgWidthOut  = m_imgWidth  ;

		  int lineByte = (m_imgWidthOut * m_nBitCountOut / 8 + 3) / 4 * 4 ;

		  m_pImgDataOut = new unsigned char[lineByte * m_imgHeightOut] ;

		  for(int i = 0 ; i < m_imgHeightOut ; i++){
			  for(int j =  0 ; j < m_imgWidthOut ; j++){
			      if(* (m_pImgData + i * lineByte + j) < threshold)
					   *(m_pImgDataOut + i * lineByte + j) = 0 ;
				  else *(m_pImgDataOut + i * lineByte + j) = 255 ;
			  }
		  }
	  }
}


4、 调用“二值化”算法

       4.1  : Menu 中加入“二值化” , 并设置ID号(随意,自己认识就行)



     4.2   消息映射


CPicView.cpp  会出现

BEGIN_MESSAGE_MAP(CPicView, CView)
	ON_COMMAND(ID_Binary, &CPicView::OnBinary)
END_MESSAGE_MAP()

调用灰度变换类。

void CPicView::OnBinary()
{
	CPicDoc  *pDoc=GetDocument();
	ImageDib *pDib=pDoc->m_dib;

	//异常判断
	if(pDib->m_nBitCount != 24 && pDib->m_nBitCount != 8){
		::MessageBox(0, _T("只处理彩色和灰度图像") , MB_OK , 0);
		return ;
	}

	//将pDib中的图像数据作为输入数据,调用带参数的构造函数,
	//定义GrayTrans类的对象graytrans
	GrayTrans graytrans(pDib->GetDimensions() , pDib->m_nBitCount,
		                pDib->m_lpColorTable ,  pDib->m_pImgData );

	//调用Binary()对图像进行二值化,缺省状态下阈值为
	graytrans.BinaryImage();

	//建立一个新视图,显示分割结果
	CMainFrame *pFrame = (CMainFrame *)(AfxGetApp()->m_pMainWnd) ;
	pFrame->SendMessage(WM_COMMAND , ID_FILE_NEW) ;
	CPicView  *pView = (CPicView *)pFrame->MDIGetActive()->GetActiveView();
	CPicDoc *pDocNew = pView->GetDocument();
	ImageDib *dibNew = pDocNew->m_dib;
	dibNew->ReplaceDib(graytrans.GetDimensions(),graytrans.m_nBitCountOut,graytrans.m_lpColorTableOut, graytrans.m_pImgDataOut);
	pView->OnInitialUpdate();	
	pDocNew->SetModifiedFlag(TRUE);
	pDocNew->UpdateAllViews(pView);
}

5、 运行结果







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值