?? 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)
virtual void OnOK();
//}}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)
//}}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)
ON_CBN_EDITCHANGE(IDM_ABOUTBOX, OnEditchangeAboutbox)
ON_EN_CHANGE(IDC_EDIT_BMPFILE, OnChangeEditBmpfile)
//}}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);
return TRUE;
}
// 分配小波變換所需內存
BOOL CWavelet1DDlg::GetMemory()
{
BOOL flag=TRUE;
Data=new double *[m_nImageHeight];
for(int n=0;n<=m_nDecomposeStage;n++)
{
*(Data+n)=new double[sizeof(double)*m_nImageWidth];
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -