📄 wavelet1ddlg.cpp
字号:
// Wavelet1DDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Wavelet1D.h"
#include "Wavelet1DDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWavelet1DDlg dialog
// 参数初始化
CWavelet1DDlg::CWavelet1DDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWavelet1DDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWavelet1DDlg)
// NOTE: the ClassWizard will add member initialization here
m_bWavelet = FALSE;
m_strBmpFile=_T("Sample.bmp");
m_nDecomposeStage = 1;
m_nDisplayStage = 1;
m_nFilterLen = 6;
m_nBinGate =100;
Data = NULL;
DotData = NULL;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_strFilterFile[0]="h1.dat";
m_strFilterFile[1]="h2.dat";
m_strFilterFile[2]="h3.dat";
m_strFilterFile[3]="h4.dat";
m_strFilterFile[4]="h5.dat";
m_strFilterFile[5]="h6.dat";
m_strFilterFile[6]="h7.dat";
m_strFilterFile[7]="h8.dat";
m_strFilterFile[8]="h9.dat";
m_strFilterFile[9]="h10.dat";
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWavelet1DDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWavelet1DDlg)
DDX_Control(pDX, IDC_COMBO_DISPLAYSTAGE, m_combDisplayStage);
DDX_Control(pDX, IDC_COMBO_FILTERLEN, m_combFilterLen);
DDX_Control(pDX, IDC_COMBO_STAGE, m_combDecomposeStage);
DDX_Control(pDX, IDC_EDIT_BMPFILE, m_editBmpFile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWavelet1DDlg, CDialog)
//{{AFX_MSG_MAP(CWavelet1DDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_BMPFILE, OnBtnBmpFile)
ON_BN_CLICKED(IDC_BUTTON_WAVELET1D, OnBtnWavelet1D)
ON_CBN_SELCHANGE(IDC_COMBO_DISPLAYSTAGE, OnSelchangeCombDisplayStage)
ON_CBN_EDITCHANGE(IDC_COMBO_DISPLAYSTAGE, OnEditchangeCombDisplayStage)
ON_CBN_EDITCHANGE(IDC_COMBO_DECOMPOSESTAGE, OnEditchangeCombDecomposeStage)
ON_CBN_EDITCHANGE(IDC_COMBO_FILTERLEN, OnEditchangeCombFilterLen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWavelet1DDlg message handlers
BOOL CWavelet1DDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// 初始化控件
m_editBmpFile.SetWindowText(m_strBmpFile);
CString strComb;
int k=0;
for(k=0;k<6;k++)
{
strComb.Format("%d",k+1);
m_combDecomposeStage.InsertString(k,strComb);
m_combDecomposeStage.SetItemData(k,k+1);
m_combDecomposeStage.SetCurSel(0);
}
strComb.Format("%d",1);
m_combFilterLen.InsertString(0,strComb);
m_combFilterLen.SetItemData(0,2);
for(k=1;k<10;k++)
{
strComb.Format("%d",2*(k+1));
m_combFilterLen.InsertString(k,strComb);
m_combFilterLen.SetItemData(k,2*(k+1));
m_combFilterLen.SetCurSel(2);
}
m_combDisplayStage.EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CWavelet1DDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CWavelet1DDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
if(m_bWavelet)
DrawWavelet1D();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CWavelet1DDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWavelet1DDlg::OnSelchangeCombDisplayStage()
{
m_nDisplayStage=m_combDisplayStage.GetItemData(m_combDisplayStage.GetCurSel());
DrawWavelet1D();
}
void CWavelet1DDlg::OnEditchangeCombDisplayStage()
{
m_combDisplayStage.SetCurSel(1);
}
void CWavelet1DDlg::OnEditchangeCombDecomposeStage()
{
m_combDecomposeStage.SetCurSel(0);
}
void CWavelet1DDlg::OnEditchangeCombFilterLen()
{
m_combFilterLen.SetCurSel(2);
}
// 选择原始数据图像文件
void CWavelet1DDlg::OnBtnBmpFile()
{
CFileDialog BmpFileDlg(TRUE,"bmp","*.bmp");
if(BmpFileDlg.DoModal()==IDOK)
{
m_strBmpFile=BmpFileDlg.GetPathName();
m_editBmpFile.SetWindowText(m_strBmpFile);
m_bWavelet=FALSE;
}
}
// 确定退出
void CWavelet1DDlg::OnOK()
{
// 若已进行过小波变换,则所用资源
if(m_bWavelet)
{
ReleaseMemory();
}
CDialog::OnOK();
}
// “一维小波变换”按钮消息处理函数
// 功能:打开文件,分配内存,进行变换,绘制结果
void CWavelet1DDlg::OnBtnWavelet1D()
{
CFile BmpFile;
CFileException fe;
// 打开原始数据图像文件
if(m_strBmpFile.IsEmpty())
{
AfxMessageBox (_T("请选择一个位图(*.bmp)文件!"),MB_ICONINFORMATION|MB_OK,NULL);
return;
}
if(!BmpFile.Open(m_strBmpFile,CFile::modeRead,&fe))
{
AfxMessageBox (_T("打开文件失败!"),MB_ICONINFORMATION|MB_OK,NULL);
return;
}
// 若已进行过小波变换,则释放上次变换所用资源
if(m_bWavelet)
{
ReleaseMemory();
}
// 读取原始数据图像文件
TRY
{
DIB = ::ReadDIBFile(BmpFile);
}
CATCH (CFileException, eLoad)
{
BmpFile.Abort(); // 读取失败
AfxMessageBox (_T("读取数据失败!"),MB_ICONINFORMATION|MB_OK,NULL);
DIB = NULL; // 设置DIB为空
return ;
}
END_CATCH
// 锁定DIB
lpDIB=(LPSTR) ::GlobalLock((HGLOBAL)GetHDIB());
//打开待处理图像
// 判断是否是8-bpp位图
if (::DIBNumColors(lpDIB) > 256||m_nImageWidth>=400||m_nImageHeight>=300)
{
// 提示用户
AfxMessageBox(_T("不是256色位图或者图像过大!") , MB_ICONINFORMATION | MB_OK,NULL);
// 解除锁定
::GlobalUnlock((HGLOBAL) GetHDIB());
return ;
}
// 得到原始数据图像的宽度和高度
m_nImageWidth=::DIBWidth(lpDIB);
m_nImageHeight=::DIBHeight(lpDIB);
// 更改光标形状
BeginWaitCursor();
// 找到DIB图像象素起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
// 得到小波变换所需的层次、滤波器长度等参数
GetParam();
// 获取滤波器参数
if(!GetHn())
return;
// 给Data数组分配内存
if(!GetMemory())
return;
// 将原始数据图像二值化并投影,将其存储到Data[0]中
PrepareData();
// 对Data[0]中数据进行小波变换
Wavelet1D();
// 绘制小波变换结果
DrawWavelet1D();
// 解除锁定
::GlobalUnlock((HGLOBAL)GetHDIB());
// 恢复光标
EndWaitCursor();
// 设置已进行过小波变换标志
m_bWavelet=TRUE;
}
// 得到小波变换所需的层次、滤波器长度等参数
void CWavelet1DDlg::GetParam()
{
// 得到要进行小波变换的层次
if(m_combDecomposeStage.GetCurSel()==CB_ERR)
m_nDecomposeStage=1;
else
m_nDecomposeStage=m_combDecomposeStage.GetItemData(m_combDecomposeStage.GetCurSel());
// 得到滤波器长度
if(m_combFilterLen.GetCurSel()==CB_ERR)
m_nFilterLen=6;
else
m_nFilterLen=m_combFilterLen.GetItemData(m_combFilterLen.GetCurSel());
// 显示第一层小波变换后的结果
m_nDisplayStage=1;
// 只有在进行小波变换后才能初始化显示变换结果层次的Comb控件
CString strComb;
m_combDisplayStage.EnableWindow(TRUE);
m_combDisplayStage.ResetContent();
for(int k=0;k<=m_nDecomposeStage;k++)
{
strComb.Format("%d",k);
m_combDisplayStage.InsertString(k,strComb);
m_combDisplayStage.SetItemData(k,k);
m_combDisplayStage.SetCurSel(1);
}
}
// 将原始数据图像二值化并投影,将其存储到Data[0]中
void CWavelet1DDlg::PrepareData()
{
// 原始数据图像二值化及垂直、水平方向投影
// DotData[i]存放投影后一维待变换数据
// Data[0][i]同DotData[i],存放投影后一维待变换数据
// Data[1][i]存放一层小波变换后数据,依此类推
int temp;
int i,j;
for(i=0;i<m_nImageHeight+m_nImageWidth;i++)
*(DotData+i)=0;
for(i=0;i<m_nImageHeight;i++)
for(j=0;j<m_nImageWidth;j++)
{
temp=*((unsigned char *)lpDIBBits+m_nImageWidth*i+j);
if(temp<=m_nBinGate)
{
(*(DotData+i))++;
(*(DotData+m_nImageHeight+j))++;
*((unsigned char *)lpDIBBits+m_nImageWidth*i+j)=0;
}
else
{
*((unsigned char *)lpDIBBits+m_nImageWidth*i+j)=255;
}
}
// 将二值化后图像点阵投影数据读入Data数组
for(i=0;i<m_nImageHeight+m_nImageWidth;i++)
Data[0][i]=DotData[i];
}
// 一维小波变换
void CWavelet1DDlg::Wavelet1D()
{
int i,j;
for(i=1;i<=m_nDecomposeStage;i++)
{
// 复制数据
for(j=0;j<(m_nImageWidth+m_nImageHeight);j++)
Data[i][j]=Data[i-1][j];
// 第i层小波变换
if(!DecompStage(i,m_nFilterLen))
{
AfxMessageBox("变换失败!",MB_OK|MB_ICONINFORMATION);
break;
}
}
}
// 得到滤波器参数
BOOL CWavelet1DDlg::GetHn()
{
int i;
double temp1;
FILE *fp1;
if((fp1=fopen(m_strFilterFile[m_nFilterLen/2-1],"rb+"))==NULL)
{
AfxMessageBox("打开Hn数据文件失败!",MB_OK|MB_ICONINFORMATION);
return FALSE;
}
for(i=0;i<30;i++)
h[i]=0;
for(i=0;i<m_nFilterLen;i++)
{
fread(&temp1,sizeof(double),1,fp1);
h[i]=temp1;
}
fclose(fp1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -