小波变换Doc.cpp
上传用户:aqingfeng
上传日期:2014-03-25
资源大小:1839k
文件大小:38k
- // 小波变换Doc.cpp : implementation of the CMyDoc class
- //
- #include "stdafx.h"
- #include "小波变换.h"
- #include "WvltTrans.h"
- #include "小波变换Doc.h"
- #include "Diproc.h"
- #include "WFilter.h"
- #include "math.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyDoc
- IMPLEMENT_DYNCREATE(CMyDoc, CDocument)
- BEGIN_MESSAGE_MAP(CMyDoc, CDocument)
- //{{AFX_MSG_MAP(CMyDoc)
- ON_COMMAND(ID_FILE_REOPEN, OnFileReopen)
- ON_COMMAND(IDM_WVLT_HORTRANS, OnWvltHortrans)
- ON_COMMAND(IDM_WVLT_VERTRANS, OnWvltVertrans)
- ON_COMMAND(IDM_WVLT_ONCE, OnWvltTransOnce)
- ON_COMMAND(IDM_WVLT_TWICE, OnWvltTransTwice)
- ON_COMMAND(IDM_WVLT_TRBL, OnWvltTransTrbl)
- ON_COMMAND(IDM_DIPROC_FUSION, OnDiprocFusion)
- ON_COMMAND(IDM_DIPROC_REVER, OnDiprocRever)
- ON_COMMAND(IDM_FILTER_BLUR, OnFilterBlur)
- ON_COMMAND(IDM_FILTER_SHARPNESS, OnFilterSharpness)
- ON_COMMAND(IDM_FILTER_BLUR2, OnFilterBlur2)
- ON_COMMAND(IDM_FILTER_SHARPNESS2, OnFilterSharpness2)
-
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyDoc construction/destruction
- CMyDoc::CMyDoc()
- {
- // TODO: add one-time construction code here
- m_fDirty = FALSE;
- m_pBitmap = NULL ;
- m_pTransfered = NULL;
- m_bTribl = FALSE; m_bOnce = FALSE; m_bTwice = FALSE; m_bFilter = FALSE;
- m_hDIB = NULL;
- m_palDIB = NULL;
- m_sizeDoc = CSize(1,1);
- m_hMulDIB= NULL;
- }
- CMyDoc::~CMyDoc()
- {
- if( m_pBitmap != NULL)
- free( m_pBitmap);
- if( m_pTransfered != NULL)
- free( m_pTransfered );
- // 判断DIB对象是否存在
- if (m_hDIB != NULL)
- {
- // 清除DIB对象
- ::GlobalFree((HGLOBAL) m_hDIB);
- }
-
- // 判断调色板是否存在
- if (m_palDIB != NULL)
- {
- // 清除调色板
- delete m_palDIB;
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyDoc serialization
- void CMyDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- }
- else
- {
- // TODO: add loading code here
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyDoc diagnostics
- #ifdef _DEBUG
- void CMyDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CMyDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMyDoc commands
- BOOL CMyDoc::OnNewDocument()//建立新文档
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
- return TRUE;
- }
- BOOL CMyDoc::OnOpenDocument(LPCTSTR lpszPathName)//打开已有文档,lpszPathName是待打开文件的文件名
- {
- CFile file;
- CFileException fe;
- // 打开文件
- if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
- {
- // 失败
- ReportSaveLoadException(lpszPathName, &fe,FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
- // 返回FALSE
- return FALSE;
- }
- DeleteContents();
- // 更改光标形状
- BeginWaitCursor();
- // 尝试调用ReadDIBFile()读取图像
- TRY
- {
- m_hDIB = ::ReadDIBFile(file);
- }
- CATCH (CFileException, eLoad)
- {
- // 读取失败
- file.Abort();
- // 恢复光标形状
- EndWaitCursor();
- // 报告失败
- ReportSaveLoadException(lpszPathName, eLoad,FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
- // 设置DIB为空
- m_hDIB = NULL;
- // 返回FALSE
- return FALSE;
- }
- END_CATCH
- // 初始化DIB
- InitDIBData();
- // 恢复光标形状
- EndWaitCursor();
- // 判断读取文件是否成功
- if (m_hDIB == NULL)
- {
- // 失败,可能非BMP格式
- CString strMsg;
- strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
- // 提示出错
- MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
- // 返回FALSE
- return FALSE;
- }
- // 设置文件名称
- SetPathName(lpszPathName);
- // 初始化胀标记为FALSE
- SetModifiedFlag(FALSE);
- // 返回TRUE
- return TRUE;
- }
- void CMyDoc::OnFileReopen()
- {
- // 重新打开图像,放弃所有修改
-
- // 判断当前图像是否已经被改动
- if (IsModified())
- {
- // 提示用户该操作将丢失所有当前的修改
- if (MessageBox(NULL, "重新打开图像将丢失所有改动!是否继续?", "系统提示", MB_ICONQUESTION | MB_YESNO) == IDNO)
- {
- // 用户取消操作,直接返回
- return;
- }
- }
-
- CFile file;
- CFileException fe;
-
- CString strPathName;
-
- // 获取当前文件路径
- strPathName = GetPathName();
-
- // 重新打开文件
- if (!file.Open(strPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
- {
- // 失败
- ReportSaveLoadException(strPathName, &fe,
- FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
-
- // 返回
- return;
- }
-
- // 更改光标形状
- BeginWaitCursor();
-
- // 尝试调用ReadDIBFile()读取图像
- TRY
- {
- m_hDIB = ::ReadDIBFile(file);
- }
- CATCH (CFileException, eLoad)
- {
- // 读取失败
- file.Abort();
-
- // 恢复光标形状
- EndWaitCursor();
-
- // 报告失败
- ReportSaveLoadException(strPathName, eLoad,
- FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
-
- // 设置DIB为空
- m_hDIB = NULL;
-
- // 返回
- return;
- }
- END_CATCH
-
- // 初始化DIB
- InitDIBData();
-
- // 判断读取文件是否成功
- if (m_hDIB == NULL)
- {
- // 失败,可能非BMP格式
- CString strMsg;
- strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
-
- // 提示出错
- MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
-
- // 恢复光标形状
- EndWaitCursor();
-
- // 返回
- return;
- }
-
- // 初始化胀标记为FALSE
- SetModifiedFlag(FALSE);
-
- // 刷新
- UpdateAllViews(NULL);
-
- // 恢复光标形状
- EndWaitCursor();
-
- // 返回
- return;
-
- }
- void CMyDoc::InitDIBData()
- {
- // 初始化DIB对象
- // 判断调色板是否为空
- if (m_palDIB != NULL)
- {
- // 删除调色板对象
- delete m_palDIB;
- // 重置调色板为空
- m_palDIB = NULL;
- }
- // 如果DIB对象为空,直接返回
- if (m_hDIB == NULL)
- {
- // 返回
- return;
- }
- LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
- // 判断图像是否过大
- if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX)
- {
- ::GlobalUnlock((HGLOBAL) m_hDIB);
- // 释放DIB对象
- ::GlobalFree((HGLOBAL) m_hDIB);
- // 设置DIB为空
- m_hDIB = NULL;
- CString strMsg;
- strMsg = "BMP图像太大!";
- // 提示用户
- MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
- // 返回
- return;
- }
- // 设置文档大小
- m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));
- ::GlobalUnlock((HGLOBAL) m_hDIB);
- // 创建新调色板
- m_palDIB = new CPalette;
- // 判断是否创建成功
- if (m_palDIB == NULL)
- {
- // 失败,可能是内存不足
- ::GlobalFree((HGLOBAL) m_hDIB);
- // 设置DIB对象为空
- m_hDIB = NULL;
- // 返回
- return;
- }
- // 调用CreateDIBPalette来创建调色板
- if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
- {
- // 返回空,可能该DIB对象没有调色板
- // 删除
- delete m_palDIB;
- // 设置为空
- m_palDIB = NULL;
- // 返回
- return;
- }
- }
- BOOL CMyDoc::OnSaveDocument(LPCTSTR lpszPathName)
- {
- CFile file;
- CFileException fe;
- // 打开文件
- if (!file.Open(lpszPathName, CFile::modeCreate |
- CFile::modeReadWrite | CFile::shareExclusive, &fe))
- {
- // 失败
- ReportSaveLoadException(lpszPathName, &fe,TRUE, AFX_IDP_INVALID_FILENAME);
- // 返回FALSE
- return FALSE;
- }
- // 尝试调用SaveDIB保存图像
- BOOL bSuccess = FALSE;
- TRY
- {
- // 更改光标形状
- BeginWaitCursor();
- // 尝试保存图像
- bSuccess = ::SaveDIB(m_hDIB, file);
- // 关闭文件
- file.Close();
- }
- CATCH (CException, eSave)
- {
- // 失败
- file.Abort();
- // 恢复光标形状
- EndWaitCursor();
- ReportSaveLoadException(lpszPathName, eSave,TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
- // 返回FALSE
- return FALSE;
- }
- END_CATCH
- // 恢复光标形状
- EndWaitCursor();
- // 重置胀标记为FALSE
- SetModifiedFlag(FALSE);
- if (!bSuccess)
- {
- // 保存失败,可能是其它格式的DIB,可以读取但是不能保存
- // 或者是SaveDIB函数有误
- CString strMsg;
- strMsg = "无法保存BMP图像!";
- // 提示出错
- MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
- }
- return bSuccess;
- }
- void CMyDoc::OnWvltHortrans()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //图像矩阵坐标与像素数值
- int x,y,cur;
- unsigned char tempR, tempG, tempB;
- short **spOriginData, **spTransData0;
- float fTempBufforDisp;
- short MaxPixVal,MinPixVal,Diff;
- //分配数据空间
- spOriginData = new short* [biHeight];
- spTransData0 = new short* [biHeight];
- //
- for(int i = 0; i < biHeight; i ++)
- {
- spOriginData[i] = new short [biWidth];
- spTransData0[i] = new short [biWidth];
- }
- //创建图像小波变换类
- CWvltTrans *pTrans;
- //从设备缓存中获得原始图像数据
- for(y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- cur = y*biAlign+3*x;
- tempB=lpData[cur];
- tempG=lpData[cur+1];
- tempR=lpData[cur+2];
- spOriginData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB); //no problem
- }
- }
- //完成图像水平方向的一次小波变换
- pTrans->Hori_Transform(spOriginData,spTransData0,biHeight,biWidth/2,1);
- //屏蔽图像复原操作标志
- m_bFilter = TRUE;
- m_bFilter = m_bFilter & ~m_bOnce & ~m_bTwice & ~m_bTribl;
- MaxPixVal=spTransData0[0][0];
- MinPixVal=spTransData0[0][0];
- //得到图像小波系数的极大值与极小值
- for( y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- if(MaxPixVal<spTransData0[y][x])
- MaxPixVal=spTransData0[y][x];
- if(MinPixVal>spTransData0[y][x])
- MinPixVal=spTransData0[y][x];
- }
- }
- //计算获取图像小波系数的极值差
- Diff=MaxPixVal-MinPixVal;
- //小波经过处理后,放入显示缓存中
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- //因为小波变换后的小波系数有可能超过255甚至更多,那么就将
- //小波系数的范围映射到0~255区间内,以后出现类似的处理,目的都是一样的
- fTempBufforDisp=spTransData0[biHeight-1-y][x];
- fTempBufforDisp-=MinPixVal;
- fTempBufforDisp*=255;
- fTempBufforDisp/=Diff;
- cur= y*biAlign+3*x; //current pixel
- lpData[cur] = (unsigned char)fTempBufforDisp;
- lpData[cur+1]= (unsigned char)fTempBufforDisp;
- lpData[cur+2]= (unsigned char)fTempBufforDisp;
- }
- }
- //小波图像水平方向小波变换
- SetModifiedFlag(TRUE);
- UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //删除临时的数据空间
- delete spOriginData;
- delete spTransData0;
- }
- void CMyDoc::OnWvltVertrans()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //图像矩阵坐标与像素数据
- int x,y,cur;
- unsigned char tempR, tempG, tempB;
- float fTempBufforDisp;
- short MaxPixVal,MinPixVal,Diff;
- short **spOriginData, **spTransData1;
- //分配图像小波变换所需的数据空间
- spOriginData = new short* [biHeight];
- spTransData1 = new short* [biHeight];
- for(int i = 0; i < biHeight; i ++)
- {
- spOriginData[i] = new short [biWidth];
- spTransData1[i] = new short [biWidth];
- }
- //创建图像小波变换类
- CWvltTrans *pTrans;
- //从设备缓存中获取原始图像数据
- for(y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- cur = y*biAlign+3*x;
- tempB=lpData[cur];
- tempG=lpData[cur+1];
- tempR=lpData[cur+2];
- spOriginData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB); //no problem
- }
- }
- //完成一次竖直方向的图像小波变换
- pTrans->Vert_Transform(spOriginData,spTransData1,biHeight/2,biWidth,1);
- //屏蔽图像复原操作标志
- m_bFilter = TRUE;
- m_bFilter = m_bFilter & ~m_bOnce & ~m_bTwice & ~m_bTribl;
- MaxPixVal=spTransData1[0][0];
- MinPixVal=spTransData1[0][0];
- //计算得到图像小波系数的极大值和极小值
- for( y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- if(MaxPixVal<spTransData1[y][x])
- MaxPixVal=spTransData1[y][x];
- if(MinPixVal>spTransData1[y][x])
- MinPixVal=spTransData1[y][x];
- }
- }
- //计算小波系数的极值差
- Diff=MaxPixVal-MinPixVal;
- //将小波系数处理后,放入显示缓存中
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- //因为小波变换后的小波系数有可能超过255甚至更多,那么就将
- //小波系数的范围映射到0~255区间内,以后出现类似的处理,目的都是一样的
- fTempBufforDisp=spTransData1[biHeight-1-y][x];
- fTempBufforDisp-=MinPixVal;
- fTempBufforDisp*=255;
- fTempBufforDisp/=Diff;
- cur= y*biAlign+3*x; //current pixel
- lpData[cur] = (unsigned char)fTempBufforDisp;
- lpData[cur+1]= (unsigned char)fTempBufforDisp;
- lpData[cur+2]= (unsigned char)fTempBufforDisp;
- }
- }
- SetModifiedFlag(TRUE);
- //显示图像的小波系数
- UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //删除临时的数据空间
- delete spOriginData;
- delete spTransData1;
- }
- void CMyDoc::OnWvltTransOnce()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //图像矩阵坐标与像素数据
- int x,y,cur;
- unsigned char tempR, tempG, tempB;
- float fTempBufforDisp;
- short MaxPixVal,MinPixVal,Diff;
- short **spOriginData, **spTransData0, **spTransData1;
- //分配图像小波变换所用的数据空间
- spOriginData = new short* [biHeight];
- spTransData0 = new short* [biHeight];
- spTransData1 = new short* [biHeight];
- m_WvltCoeff = new short * [biHeight];
- for(int i = 0; i < biHeight; i ++)
- {
- spOriginData[i] = new short [biWidth];
- spTransData0[i] = new short [biWidth];
- spTransData1[i] = new short [biWidth];
- m_WvltCoeff[i] = new short [biWidth];
- }
- //创建图像小波变换类
- CWvltTrans *pTrans;
- //从设备缓存中获取原始图像数据
- for(y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- cur = y*biAlign+3*x;
- tempB=lpData[cur];
- tempG=lpData[cur+1];
- tempR=lpData[cur+2];
- spOriginData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB); //no problem
- }
- }
- //完成一次图像小波变换
- pTrans->DWT_Once(spOriginData,spTransData0,spTransData1,biHeight,biHeight/2,biWidth,biWidth/2,1,1.414);
- //允许图像复原操作标志
- m_bOnce = TRUE;
- m_bOnce = m_bOnce & ~m_bTwice & ~m_bTribl & ~m_bFilter;
- MaxPixVal=spTransData1[0][0];
- MinPixVal=spTransData1[0][0];
- for( y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- if(MaxPixVal<spTransData1[y][x])
- MaxPixVal=spTransData1[y][x];
- if(MinPixVal>spTransData1[y][x])
- MinPixVal=spTransData1[y][x];
- m_WvltCoeff[y][x] = spTransData1[y][x];
- }
- }
- Diff=MaxPixVal-MinPixVal;
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- //因为小波变换后的小波系数有可能超过255甚至更多,那么就将
- //小波系数的范围映射到0~255区间内,以后出现类似的处理,目的都是一样的
- fTempBufforDisp=spTransData1[biHeight-1-y][x];
- fTempBufforDisp-=MinPixVal;
- fTempBufforDisp*=255;
- fTempBufforDisp/=Diff;
- cur= y*biAlign+3*x; //current pixel
- lpData[cur] = (unsigned char)fTempBufforDisp;
- lpData[cur+1]= (unsigned char)fTempBufforDisp;
- lpData[cur+2]= (unsigned char)fTempBufforDisp;
- }
- }
- SetModifiedFlag(TRUE);
- //显示图像的小波系数
- UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //删除临时的数据空间
- delete spOriginData;
- delete spTransData0;
- delete spTransData1;
- }
- void CMyDoc::OnWvltTransTwice()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //图像矩阵坐标与像素数值
- int x,y,cur;
- unsigned char tempR, tempG, tempB;
- float fTempBufforDisp;
- short MaxPixVal,MinPixVal,Diff;
- short **spOriginData, **spTransData0, **spTransData1;
- //分配数据空间
- spOriginData = new short* [biHeight];
- spTransData0 = new short* [biHeight];
- spTransData1 = new short* [biHeight];
- m_WvltCoeff = new short * [biHeight];
- for(int i = 0; i < biHeight; i ++)
- {
- spOriginData[i] = new short [biWidth];
- spTransData0[i] = new short [biWidth];
- spTransData1[i] = new short [biWidth];
- m_WvltCoeff[i] = new short [biWidth];
- }
- //创建图像小波变化类
- CWvltTrans *pTrans;
- //从设备缓存中获取原始图像数据
- for(y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- cur = y*biAlign+3*x;
- tempB=lpData[cur];
- tempG=lpData[cur+1];
- tempR=lpData[cur+2];
- spOriginData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB); //no problem
- }
- }
- //完成图像的两次小波变换
- pTrans->DWT_TwoLayers(spOriginData,spTransData0,spTransData1,biHeight,biHeight/2,biWidth,biWidth/2,2,1.414);
- //允许图像复原操作标志
- m_bTwice = TRUE;
- m_bTwice = m_bTwice & ~m_bTribl & ~m_bOnce & ~m_bFilter;
- MaxPixVal=spTransData1[0][0];
- MinPixVal=spTransData1[0][0];
- //计算得到图像小波系数的极大值与极小值
- for( y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- if(MaxPixVal<spTransData1[y][x])
- MaxPixVal=spTransData1[y][x];
- if(MinPixVal>spTransData1[y][x])
- MinPixVal=spTransData1[y][x];
- m_WvltCoeff[y][x] = spTransData1[y][x];
- }
- }
- //计算获得小波系数的极值差
- Diff=MaxPixVal-MinPixVal;
- //小波系数经过处理后,放入显示缓存中
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- //因为小波变换后的小波系数有可能超过255甚至更多,那么就将
- //小波系数的范围映射到0~255区间内,以后出现类似的处理,目的都是一样的
- fTempBufforDisp=spTransData1[biHeight-1-y][x];
- fTempBufforDisp-=MinPixVal;
- fTempBufforDisp*=255;
- fTempBufforDisp/=Diff;
- cur= y*biAlign+3*x; //current pixel
- lpData[cur] = (unsigned char)fTempBufforDisp;
- lpData[cur+1]= (unsigned char)fTempBufforDisp;
- lpData[cur+2]= (unsigned char)fTempBufforDisp;
- }
- }
- SetModifiedFlag(TRUE);
- //显示图像
- UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //删除临时的数据空间
- delete spOriginData;
- delete spTransData0;
- delete spTransData1;
- }
- void CMyDoc::OnWvltTransTrbl()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //图像矩阵坐标与像素数据
- int x,y,cur;
- unsigned char tempR, tempG, tempB;
- float fTempBufforDisp;
- short MaxPixVal,MinPixVal,Diff;
- short **spOriginData, **spTransData0, **spTransData1;
- //分配图像小波变换的数据内存空间
- spOriginData = new short* [biHeight];
- spTransData0 = new short* [biHeight];
- spTransData1 = new short* [biHeight];
- m_WvltCoeff = new short * [biHeight];
- for(int i = 0; i < biHeight; i ++)
- {
- spOriginData[i] = new short [biWidth];
- spTransData0[i] = new short [biWidth];
- spTransData1[i] = new short [biWidth];
- m_WvltCoeff[i] = new short [biWidth];
- }
- //创建图像小波类
- CWvltTrans *pTrans;
- //从设备的图像缓存中获取原始图像的数据
- for(y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- cur = y*biAlign+3*x;
- tempB=lpData[cur];
- tempG=lpData[cur+1];
- tempR=lpData[cur+2];
- spOriginData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB); //no problem
- }
- }
- //完成图像的三次小波变换
- pTrans->DWT_TriLayers(spOriginData,spTransData0,spTransData1,biHeight,biHeight/2,biWidth,biWidth/2,3,1.414);
- //允许图像复员操作标志
- m_bTribl = TRUE;
- m_bTribl = m_bTribl & ~m_bTwice & ~m_bOnce & ~m_bFilter;
- MaxPixVal=spTransData1[0][0];
- MinPixVal=spTransData1[0][0];
- //得到小波系数的极大值和极小值
- for( y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- if(MaxPixVal<spTransData1[y][x])
- MaxPixVal=spTransData1[y][x];
- if(MinPixVal>spTransData1[y][x])
- MinPixVal=spTransData1[y][x];
- m_WvltCoeff[y][x] = spTransData1[y][x];
- }
- }
- //计算出小波系数的极值差
- Diff=MaxPixVal-MinPixVal;
- //将图像的小波数据处理后放入显示缓存中
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- //因为小波变换后的小波系数有可能超过255甚至更多,那么就将
- //小波系数的范围映射到0~255区间内,以后出现类似的处理,目的都是一样的
- fTempBufforDisp=spTransData1[biHeight-1-y][x];
- fTempBufforDisp-=MinPixVal;
- fTempBufforDisp*=255;
- fTempBufforDisp/=(float)Diff;
- cur= y*biAlign+3*x; //current pixel
- lpData[cur] = (unsigned char)fTempBufforDisp;
- lpData[cur+1]= (unsigned char)fTempBufforDisp;
- lpData[cur+2]= (unsigned char)fTempBufforDisp;
- }
- }
- SetModifiedFlag(TRUE);
- //显示图像
- UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //删除临时的数据空间
- delete spOriginData;
- delete spTransData0;
- delete spTransData1;
- }
- void CMyDoc::OnDiprocFusion()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //定义图像的内存数据空间指针
- short **pData, **pDataFusion;
- CString cstrFileName = m_strFileFusion;
- //图像矩阵的坐标与像素数据
- int x, y, cur;
- int tempR, tempG, tempB;
- //分配图像的数据空间
- pData = new short * [biHeight];
- pDataFusion = new short * [biHeight];
- for(int i = 0; i < biWidth; i ++)
- {
- pDataFusion[i] = new short [biWidth];
- pData[i] = new short [biWidth];
- }
- //从设备的显示缓存中获取原始图像的数据
- for(y = 0; y < (int) biHeight; y ++)
- {
- for(x = 0; x < (int) biWidth; x ++)
- {
- cur = y * biAlign + 3 * x;
- tempB = lpData[cur];
- tempG = lpData[cur + 1];
- tempR = lpData[cur + 2];
- pData[biHeight - 1 - y][x] = 0.3 * tempR + 0.59 * tempG + 0.11 * tempB;
- }
- }
- //释放设备占用的显示缓存
- m_hDIB = NULL;
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //打开另外一幅用于融合的图像文件
- MessageBoxA(NULL,_T("请在WvltDip\Fusion目录中打开另外一幅图像,进行图像融合"),_T("Message"),MB_ICONEXCLAMATION|MB_OK);
- while(!abs((int)strcmp(m_strFileFusion, cstrFileName)))
- {
- CFileDialog dlg( TRUE,NULL,NULL,
- OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
- _T("Bitmap (*.BMP)|*.BMP|"));
- if( dlg.DoModal()==IDOK )
- cstrFileName = dlg.GetPathName();
- //如果文件重名,提示重新打开文件
-
- if(!abs((int)strcmp(m_strFileFusion, cstrFileName)))
- MessageBoxA(NULL,_T("文件"+m_strFileFusion+"已打开,请打开该目录中另外一个文件"),_T("Message"),MB_ICONEXCLAMATION|MB_OK);
- else
- MessageBoxA(NULL,_T("用于图像融合的文件是:n"+m_strFileFusion+"n"+cstrFileName),_T("Message"),MB_ICONEXCLAMATION|MB_OK);
- }
- CFile file;
- CFileException fe;
- if (!file.Open(cstrFileName, CFile::modeRead | CFile::shareDenyWrite, &fe))
- {
- // 失败
- ReportSaveLoadException(cstrFileName, &fe,
- FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
- }
-
- // 更改光标形状
- BeginWaitCursor();
-
- // 尝试调用ReadDIBFile()读取图像,读取位图数据,载入显示缓存中
- TRY
- {
- m_hDIB = ::ReadDIBFile(file);
- }
- CATCH (CFileException, eLoad)
- {
- // 读取失败
- file.Abort();
-
- // 恢复光标形状
- EndWaitCursor();
-
- // 报告失败
- ReportSaveLoadException(cstrFileName, eLoad,
- FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
-
- // 设置DIB为空
- m_hDIB = NULL;
-
- }
- END_CATCH
-
- // 初始化DIB
- InitDIBData();
-
- // 判断读取文件是否成功
- if (m_hDIB == NULL)
- {
- // 失败,可能非BMP格式
- CString strMsg;
- strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
-
- // 提示出错
- MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
-
- // 恢复光标形状
- EndWaitCursor();
- }
-
- // 初始化胀标记为FALSE
- SetModifiedFlag(FALSE);
-
- //显示新打开的图像
- UpdateAllViews(NULL);
-
-
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- //读取另外一幅图像的数据
- unsigned char *lpImgData = (unsigned char*)lpDIBBits ;
- //从设备的显示缓存中获取原始图像的数据
- for(y = 0; y < biHeight; y ++)
- {
- for (x = 0; x < biWidth; x++)
- {
- cur = y * biAlign + 3 * x;
- tempB = lpImgData[cur];
- tempG = lpImgData[cur + 1];
- tempR = lpImgData[cur + 2];
- pDataFusion[biHeight - 1 - y][x] = 0.3 * tempR + 0.59 * tempG + 0.11 * tempB;
- }
- }
- //创建用于图像融合的Diproc类
- CDiproc *pDIP;
- pDIP->DIP_ImageFusion(pData, pDataFusion, biHeight, biWidth);
- //将图像数据放入显示缓存中
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- cur= y*biAlign+3*x; //current pixel
- lpImgData[cur] = (unsigned char)pDataFusion[biHeight - 1- y][x];
- lpImgData[cur+1]= (unsigned char)pDataFusion[biHeight - 1 - y][x];
- lpImgData[cur+2]= (unsigned char)pDataFusion[biHeight - 1 - y][x];
- }
- }
- SetModifiedFlag(TRUE);
- //显示图像
- UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //删除临时的数据内存空间
- delete pData;
- delete pDataFusion;
- }
- void CMyDoc::OnDiprocRever()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //图像矩阵的坐标
- int x,y,cur;
- int tempR, tempG, tempB;
- //图像数据与图像处理数据的存放空间
- short **spOriginData, **spTransData0,**spTransData1;
- spOriginData = new short* [biHeight];
- spTransData0 = new short* [biHeight];
- spTransData1 = new short* [biHeight];
- //分配数据的内存空间
- for(int i = 0; i < biHeight; i ++)
- {
- spOriginData[i] = new short [biWidth];
- spTransData0[i] = new short [biWidth];
- spTransData1[i] = new short [biWidth];
- }
- //创建图像小波变换类
- CWvltTrans *pTrans;
- //从设备缓存中获取原始图像数据
- for(y=0; y<(int)biHeight; y++)
- {
- for( x=0; x<(int)biWidth; x++)
- {
- cur = y*biAlign+3*x;
- tempB=lpData[cur];
- tempG=lpData[cur+1];
- tempR=lpData[cur+2];
- spOriginData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB); //no problem
- }
- }
- //创建数字处理类
- CDiproc *pDip;
- //如果图像只经过一次小波变换,则进行相应的逆变换
- if(m_bOnce)
- { //完成一次图像小波变换
- pDip->DIP_WvltRevers(spOriginData, spTransData0, m_WvltCoeff,biHeight,biHeight/2,biWidth,biWidth/2,1,1.414);
- m_bOnce = FALSE;
- delete m_WvltCoeff;
- }
- //如果图像只经过两次小波变换,则进行相应的逆变换
- else if(m_bTwice)
- {
- pDip->DIP_WvltRevers(spOriginData, spTransData0, m_WvltCoeff,biHeight ,biHeight/2,biWidth,biWidth/2,2,1.414);
- m_bTwice = FALSE;
- delete m_WvltCoeff;
- }
- //如果图像只经过三次小波变换,则进行相应的逆变换
- else if(m_bTribl)
- {
- pDip->DIP_WvltRevers(spOriginData, spTransData0, m_WvltCoeff, biHeight, biHeight/2,biWidth,biWidth/2,3,1.414);
- m_bTribl = FALSE;
- delete m_WvltCoeff;
- }
- //判断是否有小波系数可以进行图像的复原
- else if(m_bFilter)
- {
- MessageBoxA(NULL,_T("没有找到可用小波系数,或者是未做小波变换"),_T("Message"),MB_ICONEXCLAMATION|MB_OK);
- m_bFilter = FALSE;
- }
- //将图像数据放入显示缓存中
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- cur= y*biAlign+3*x; //current pixel
- lpData[cur] = (unsigned char)spOriginData[biHeight - 1- y][x];
- lpData[cur+1]= (unsigned char)spOriginData[biHeight - 1 - y][x];
- lpData[cur+2]= (unsigned char)spOriginData[biHeight - 1 - y][x];
- }
- }
- SetModifiedFlag(TRUE);
- //显示图像
- UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //删除临时的数据内存空间
- delete spOriginData;
- delete spTransData0;
- //delete spTransData1;
- }
- void CMyDoc::OnFilterBlur()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //图像小波系数的低通滤波层数为3层
- int MaxLayer = 3;
- short **ImgData;
- float fTempBufforDisp;
- //图像矩阵坐标与图像数据
- int tempR, tempG, tempB, x, y, cur;
- //分配图像数据的内存空间
- ImgData = new short * [biHeight];
- for(int i = 0; i < biHeight; i ++)
- {
- ImgData[i] = new short [biWidth];
- }
- //获取显示缓存中的原始图像数据
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- cur = y*biAlign+3*x;
- tempB=lpData[cur];
- tempG=lpData[cur+1];
- tempR=lpData[cur+2];
- ImgData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB); //no problem
- }
- }
- //创建小波滤波器类
- CWFilter Filter;
- //小波低通滤波处理
- Filter.LPass_Filter(ImgData, biHeight, biWidth, MaxLayer, 1);
- //屏蔽图像复原操作标志
- m_bFilter = TRUE;
- //将处理后的图像数据放入显示缓存中
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- cur= y*biAlign+3*x; //当前像素的位置
- lpData[cur] = (unsigned char)Filter.m_FilterData[biHeight - 1- y][x];
- lpData[cur+1]= (unsigned char)Filter.m_FilterData[biHeight - 1 - y][x];
- lpData[cur+2]= (unsigned char)Filter.m_FilterData[biHeight - 1 - y][x];
- }
- }
- SetModifiedFlag(TRUE);
- //显示图像
- UpdateAllViews(NULL);
- //删除临时的图像数据空间
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //删除临时的图像数据空间
- delete ImgData;
- }
- void CMyDoc::OnFilterBlur2()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //图像小波系数的低通滤波层数为3层
- int MaxLayer = 3;
- short **ImgData;
- float fTempBufforDisp;
- //图像矩阵坐标与图像数据
- int tempR, tempG, tempB, x, y, cur;
- //分配图像数据的内存空间
- ImgData = new short * [biHeight];
- for(int i = 0; i < biHeight; i ++)
- {
- ImgData[i] = new short [biWidth];
- }
- //获取显示缓存中的原始图像数据
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- cur = y*biAlign+3*x;
- tempB=lpData[cur];
- tempG=lpData[cur+1];
- tempR=lpData[cur+2];
- ImgData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB); //no problem
- }
- }
- //创建小波滤波器类
- CWFilter Filter;
- //小波低通滤波处理
- Filter.LPass_Filter(ImgData, biHeight, biWidth, MaxLayer, 2);
- //屏蔽图像复原操作标志
- m_bFilter = TRUE;
- //将处理后的图像数据放入显示缓存中
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- cur= y*biAlign+3*x; //当前像素的位置
- lpData[cur] = (unsigned char)Filter.m_FilterData[biHeight - 1- y][x];
- lpData[cur+1]= (unsigned char)Filter.m_FilterData[biHeight - 1 - y][x];
- lpData[cur+2]= (unsigned char)Filter.m_FilterData[biHeight - 1 - y][x];
- }
- }
- SetModifiedFlag(TRUE);
- //显示图像
- UpdateAllViews(NULL);
- //删除临时的图像数据空间
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- delete ImgData;
-
- }
- void CMyDoc::OnFilterSharpness()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //图像小波系数的高通滤波层数为3层
- int MaxLayer = 3;
- short **ImgData;
- float fTempBufforDisp;
- //图像矩阵坐标与图像数据
- int tempR, tempG, tempB, x, y, cur;
- //分配图像数据的内存空间
- ImgData = new short * [biHeight];
- for(int i = 0; i < biHeight; i ++)
- {
- ImgData[i] = new short [biWidth];
- }
- //获取显示缓存中的原始图像数据
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- cur = y*biAlign+3*x;
- tempB=lpData[cur];
- tempG=lpData[cur+1];
- tempR=lpData[cur+2];
- ImgData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB); //no problem
- }
- }
- //创建小波滤波器类
- CWFilter Filter;
- //小波高通滤波处理
- Filter.HPass_Filter(ImgData, biHeight, biWidth, MaxLayer);
- //屏蔽图像复原操作标志
- m_bFilter = TRUE;
- //将处理后的图像数据放入显示缓存中
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- cur= y*biAlign+3*x; //当前像素的位置
- lpData[cur] = (unsigned char)Filter.m_FilterData[biHeight - 1- y][x];
- lpData[cur+1]= (unsigned char)Filter.m_FilterData[biHeight - 1 - y][x];
- lpData[cur+2]= (unsigned char)Filter.m_FilterData[biHeight - 1 - y][x];
- }
- }
- SetModifiedFlag(TRUE);
- //显示图像
- UpdateAllViews(NULL);
- //删除临时的图像数据空间
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //删除临时的图像数据空间
- delete ImgData;
- }
- void CMyDoc::OnFilterSharpness2()
- {
- // TODO: Add your command handler code here
- //读取数字图像的文件头,获取图像的属性参数
-
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- unsigned long biWidth =::DIBWidth(lpDIB);
- unsigned long biHeight = ::DIBHeight(lpDIB);
- unsigned long biAlign = (biWidth*3+3)/4 *4;
- unsigned long bmSize = biHeight * biAlign;
- unsigned char *lpData = (unsigned char*)lpDIBBits ;
- //图像小波系数的高通滤波层数为3层
- int MaxLayer = 3;
- short **ImgData;
- float fTempBufforDisp;
- //图像矩阵坐标与图像数据
- int tempR, tempG, tempB, x, y, cur;
- //分配图像数据的内存空间
- ImgData = new short * [biHeight];
- for(int i = 0; i < biHeight; i ++)
- {
- ImgData[i] = new short [biWidth];
- }
- //获取显示缓存中的原始图像数据
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- cur = y*biAlign+3*x;
- tempB=lpData[cur];
- tempG=lpData[cur+1];
- tempR=lpData[cur+2];
- ImgData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB); //no problem
- }
- }
- //创建小波滤波器类
- CWFilter Filter;
- //小波高通滤波处理
- Filter.HPass_Filter2(ImgData, biHeight, biWidth, MaxLayer);
- //屏蔽图像复原操作标志
- m_bFilter = TRUE;
- //将处理后的图像数据放入显示缓存中
- for(y=0; y<(int)biHeight; y++)
- {
- for(x=0; x<(int)biWidth; x++)
- {
- cur= y*biAlign+3*x; //当前像素的位置
- lpData[cur] = (unsigned char)Filter.m_FilterData[biHeight - 1- y][x];
- lpData[cur+1]= (unsigned char)Filter.m_FilterData[biHeight - 1 - y][x];
- lpData[cur+2]= (unsigned char)Filter.m_FilterData[biHeight - 1 - y][x];
- }
- }
- SetModifiedFlag(TRUE);
- //显示图像
- UpdateAllViews(NULL);
- //删除临时的图像数据空间
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- delete ImgData;
- }
- void CMyDoc::Openanother()
- {CString cstrFileName = m_strFileFusion;
- //释放设备占用的显示缓存
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- //打开另外一幅用于融合的图像文件
- MessageBoxA(NULL,_T("请在WvltDip\Fusion目录中打开另外一幅图像,进行图像融合"),_T("Message"),MB_ICONEXCLAMATION|MB_OK);
- while(!abs((int)strcmp(m_strFileFusion, cstrFileName)))
- {
- CFileDialog dlg( TRUE,NULL,NULL,
- OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
- _T("Bitmap (*.BMP)|*.BMP|"));
- if( dlg.DoModal()==IDOK )
- cstrFileName = dlg.GetPathName();
- //如果文件重名,提示重新打开文件
-
- if(!abs((int)strcmp(m_strFileFusion, cstrFileName)))
- MessageBoxA(NULL,_T("文件"+m_strFileFusion+"已打开,请打开该目录中另外一个文件"),_T("Message"),MB_ICONEXCLAMATION|MB_OK);
- else
- MessageBoxA(NULL,_T("用于图像融合的文件是:n"+m_strFileFusion+"n"+cstrFileName),_T("Message"),MB_ICONEXCLAMATION|MB_OK);
- }
- CFile file;
- CFileException fe;
- if (!file.Open(cstrFileName, CFile::modeRead | CFile::shareDenyWrite, &fe))
- {
- // 失败
- ReportSaveLoadException(cstrFileName, &fe,
- FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
- }
-
- // 更改光标形状
- BeginWaitCursor();
-
- // 尝试调用ReadDIBFile()读取图像,读取位图数据,载入显示缓存中
- TRY
- {
- m_hMulDIB = ::ReadDIBFile(file);
- }
- CATCH (CFileException, eLoad)
- {
- // 读取失败
- file.Abort();
-
- // 恢复光标形状
- EndWaitCursor();
-
- // 报告失败
- ReportSaveLoadException(cstrFileName, eLoad,
- FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
-
- // 设置DIB为空
- m_hMulDIB = NULL;
-
- }
- END_CATCH
-
- // 初始化DIB
- InitDIBData();
-
- // 判断读取文件是否成功
- if (m_hMulDIB == NULL)
- {
- // 失败,可能非BMP格式
- CString strMsg;
- strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
-
- // 提示出错
- MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
-
- // 恢复光标形状
- EndWaitCursor();
- }
- }