Wavelet1DDlg.cpp
上传用户:whhgrj
上传日期:2013-03-18
资源大小:169k
文件大小:23k
- // 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);
- 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];
- if(*(Data+n)==NULL)
- flag=FALSE;
- }
- DotData=new double[sizeof(double)*(m_nImageWidth+m_nImageHeight)];
- if(DotData==NULL)
- flag=FALSE;
- return flag;
- }
- // 释放小波变换所用内存资源
- void CWavelet1DDlg::ReleaseMemory()
- {
- for(int k=0;k<m_nDecomposeStage;k++)
- if(*(Data+k))
- delete *(Data+k);
- if(Data)
- {
- delete []Data;
- Data=NULL;
- }
- if(DotData)
- {
- delete DotData;
- DotData=NULL;
- }
- if(DIB)
- {
- ::GlobalFree((HGLOBAL) DIB);
- DIB=NULL;
- }
- }
- // 返回H算子周期化后得值
- double CWavelet1DDlg::HH(int i,int m)
- {
- return h[i+m];
- }
- // 返回G算子周期化后的值
- double CWavelet1DDlg::GG(int i, int m)
- {
- char flag;
- if(i%2)
- flag=-1;
- else
- flag=1;
- return HH(-1*i+1,m)*flag;
- }
- // H算子周期化过程
- BOOL CWavelet1DDlg::HHH(int level,int size)
- {
- int len,m,i;
- double temp;
- int sign;
- len=size>>level;
- m=m_nFilterLen/2;
- if((ph=new double[sizeof(double)*len])==NULL)
- {
- AfxMessageBox("H算子内存分配失败!",MB_OK|MB_ICONINFORMATION);
- return FALSE;
- }
- for(sign=0;sign<len;sign++)
- {
- temp=0;
- for(i=(-1*m+1);i<=m;i++)
- {
- if(((sign-i)%len)==0)
- temp+=HH(i-1,m);
- }
- ph[sign]=temp;
- }
- return TRUE;
- }
- // G算子周期化过程
- BOOL CWavelet1DDlg::GGG(int level,int size)
- {
- int len,m,i;
- double temp;
- int sign;
- len=size>>level;
- m=m_nFilterLen/2;
- if((pg=new double[sizeof(double)*len])==NULL)
- {
- AfxMessageBox("G算子内存分配失败!",MB_OK|MB_ICONINFORMATION);
- return FALSE;
- }
- for(sign=0;sign<len;sign++)
- {
- temp=0;
- for(i=(-1*m+3);i<=m+2;i++)
- {
- if(((sign-i)%len)==0)
- temp+=GG(i-1,m);
- }
- pg[sign]=temp;
- }
- return TRUE;
- }
- // H算子作用于一维数据
- void CWavelet1DDlg::HOperatorOn(int filterlen, int start, int len,int level)
- {
- int i,i2,m,k,len1;
- double temp1;
- m=m_nFilterLen/2;
- len1=len>>1;
- for(i=0;i<len1;i++)
- {
- i2=2*i;
- Data[level][i+start]=0;
- if(len>filterlen)
- {
- for(k=i2-len;k<=i2-len+m;k++)
- {
- if((k>=0)&&(k<len))
- if((temp1=ph[(k-i2+len)%len])!=0)
- Data[level][i+start]+=temp1*Data[level-1][k+start];
- }
- for(k=i2-m+1;k<=i2+m;k++)
- {
- if((k>=0)&&(k<len))
- if((temp1=ph[(k-i2+len)%len])!=0)
- Data[level][i+start]+=temp1*Data[level-1][k+start];
- }
- for(k=i2+len-m+1;k<=i2+len;k++)
- {
- if((k>=0)&&(k<len))
- if((temp1=ph[(k-i2+len)%len])!=0)
- Data[level][i+start]+=temp1*Data[level-1][k+start];
- }
- }
- else
- {
- for(k=0;k<len;k++)
- {
- if((temp1=ph[(k-i2+len)%len])!=0)
- Data[level][i+start]+=temp1*Data[level-1][k+start];
- }
- }
- }
- }
- // G算子作用于一维数据
- void CWavelet1DDlg::GOperatorOn(int filterlen, int start, int len,int level)
- {
- int i,i2,m,k,len1;
- double temp1;
- m=m_nFilterLen/2;
- len1=len>>1;
- for(i=0;i<len1;i++)
- {
- i2=2*i;
- Data[level][i+start+len1]=0;
- if(len>filterlen)
- {
- for(k=i2-len;k<=i2-len+m+2;k++)
- {
- if((k>=0)&&(k<len))
- if((temp1=pg[(k-i2+len)%len])!=0)
- Data[level][i+start+len1]+=temp1*Data[level-1][k+start];
- }
- for(k=i2-m+3;k<=i2+m+2;k++)
- {
- if((k>=0)&&(k<len))
- if((temp1=pg[(k-i2+len)%len])!=0)
- Data[level][i+start+len1]+=temp1*Data[level-1][k+start];
- }
- for(k=i2+len-m+3;k<=i2+len;k++)
- {
- if((k>=0)&&(k<len))
- if((temp1=pg[(k-i2+len)%len])!=0)
- Data[level][i+start+len1]+=temp1*Data[level-1][k+start];
- }
- }
- else
- {
- for(k=0;k<len;k++)
- {
- if((temp1=pg[(k-i2+len)%len])!=0)
- Data[level][i+start+len1]+=temp1*Data[level-1][k+start];
- }
- }
- }
- }
- // 对一维数据进行level层小波变换
- BOOL CWavelet1DDlg::DecompStage(int level, int filterlen)
- {
- int start,len;
- len=(m_nImageWidth+m_nImageHeight)>>(level-1);
- start=0;
- if((!HHH(level-1,m_nImageWidth+m_nImageHeight))||(!GGG(level-1,m_nImageWidth+m_nImageHeight)))
- return FALSE;
- HOperatorOn(filterlen,start,len,level);
- GOperatorOn(filterlen,start,len,level);
- if(ph&&pg)
- {
- delete ph;
- delete pg;
- ph=NULL;
- pg=NULL;
- }
- return TRUE;
- }
- // 绘制m_nDisplayStage指定层次的小波变换结果
- void CWavelet1DDlg::DrawWavelet1D()
- {
- // 创建绘图所需资源
- int Ox=0,Oy=0;
- int Ox1=0,Oy1=0,Ox2=0,Oy2=0;
- CString Text=_T("");
- CPen *OldPen,PenWhite,PenGreen,PenBlue,Pen;
- CPen* OldPen2;
- PenWhite.CreatePen(PS_SOLID,1,0x00FFFFFF);
- PenGreen.CreatePen(PS_SOLID,1,0x0000FF00);
- PenBlue.CreatePen(PS_SOLID,1,0x00FF0000);
- Pen.CreatePen(PS_SOLID,1,0x00FFFFFF);
- CDC* pDC=GetDC();
- CRect RectClient,Workarea;
- GetClientRect(RectClient);
-
- // 锁定DIB
- lpDIB = (LPSTR)::GlobalLock((HGLOBAL) GetHDIB());
- // 在对话框右上部分绘制原始数据图像及二值化后点阵投影图像
- int temp;
- int i,j;
- // 创建绘制区
- Workarea.left=RectClient.left+(RectClient.right-RectClient.left)/5*2;
- Workarea.right=RectClient.right-10;
- Workarea.top=RectClient.top+10;
- Workarea.bottom=RectClient.top+(RectClient.bottom-RectClient.top-10)/2+40;
- pDC->Rectangle(Workarea);
- pDC->SetBkColor(0x00FFFFFF);
- // 绘制坐标轴
- Ox = Workarea.left+20; Oy = Workarea.bottom-20;
- pDC->MoveTo(Ox,Oy);
- pDC->LineTo(Ox,Workarea.top+10);
- pDC->MoveTo(Ox,Oy);
- pDC->LineTo(Workarea.right-10,Oy);
- // 绘制二值化后数据图像
- Ox1= Ox+m_nImageWidth; Oy1= Oy-m_nImageHeight;
- pDC->MoveTo(Ox1,Oy1);
- pDC->LineTo(Ox1,Oy1-m_nImageHeight);
- pDC->LineTo(Ox1+m_nImageWidth,Oy1-m_nImageHeight);
- pDC->LineTo(Ox1+m_nImageWidth,Oy1);
- pDC->LineTo(Ox1,Oy1);
- pDC->SetTextColor(0x000000FF);
- pDC->TextOut(Ox,Workarea.top+3,"原始数据图像二值化及投影图");
- OldPen=pDC->SelectObject(&PenBlue);
- 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)
- {
- *((unsigned char *)lpDIBBits+m_nImageWidth*i+j)=0;
- pDC->MoveTo(Ox1+j,Oy1-i);
- pDC->LineTo(Ox1+j,Oy1-i-2);
- OldPen2=pDC->SelectObject(&PenWhite);
- pDC->LineTo(Ox1+j,Oy1-i-1);
- pDC->SelectObject(OldPen2);
- }
- else
- {
- *((unsigned char *)lpDIBBits+m_nImageWidth*i+j)=255;
- }
- }
- // 绘制垂直和水平方向的投影
- for(i=0;i<m_nImageHeight;i++)
- {
- temp=Data[0][i];
- pDC->MoveTo(Ox,Oy1-i);
- pDC->LineTo(Ox+temp,Oy1-i);
- }
- for(j=0;j<m_nImageWidth;j++)
- {
- temp=Data[0][m_nImageHeight+j];
- pDC->MoveTo(Ox1+j,Oy);
- pDC->LineTo(Ox1+j,Oy-temp);
- }
- pDC->SelectObject(OldPen);
- // 在对话框右下部分绘制小波变换后第m_nDisplayStage层数据图像
- int DataLen=0;
- double XUnit=1,YUnit=1,tempX=0,tempY=0;
- int temp1=0,temp2=0;
- double max=0,min=0,scale=0;
- // 创建绘制区
- Workarea.left=RectClient.left+(RectClient.right-RectClient.left)/5*2;
- Workarea.right=RectClient.right-10;
- Workarea.top=RectClient.top+10+(RectClient.bottom-RectClient.top-10)/2+40;
- Workarea.bottom=RectClient.bottom-10;
- pDC->Rectangle(Workarea);
- pDC->SetBkColor(0x00FFFFFF);
- // 绘制坐标轴
- Ox = Workarea.left+20; Oy = Workarea.bottom-20;
- pDC->MoveTo(Ox,Oy);
- pDC->LineTo(Ox,Workarea.top+10);
- pDC->MoveTo(Ox,Oy);
- pDC->LineTo(Workarea.right-10,Oy);
- // 归一化小波变换后数据并绘制
- max=Data[m_nDisplayStage][0];
- min=Data[m_nDisplayStage][0];
- for(j=0;j<m_nImageWidth+m_nImageHeight;j++)
- {
- if(max<Data[m_nDisplayStage][j])
- max=Data[m_nDisplayStage][j];
- if(min>Data[m_nDisplayStage][j])
- min=Data[m_nDisplayStage][j];
- }
- scale=max-min;
- XUnit=double(Workarea.right-Workarea.left-30)/double(m_nImageHeight+m_nImageWidth);
- YUnit=double(Workarea.bottom-Workarea.top-30-10)/scale;
- if(min>0)
- scale=0;
- else
- scale=(0-min)*YUnit;
- pDC->MoveTo(Ox,Oy-scale);
- pDC->LineTo(Workarea.right-10,Oy-scale);
- pDC->SetTextColor(0x000000FF);
- pDC->TextOut(Ox-10,Oy-scale-6,"O");
- Text.Format("Max:%f Min:%f",max,min);
- pDC->TextOut(Ox+10,Workarea.top+5,Text);
- pDC->SelectObject(&PenBlue);
- for(j=0;j<m_nImageWidth+m_nImageHeight-1;j++)
- {
- tempX=j*XUnit;
- tempY=(Data[m_nDisplayStage][j]-min)*YUnit;
- pDC->MoveTo(tempX+Ox,Oy-tempY);
- tempX=(j+1)*XUnit;
- tempY=(Data[m_nDisplayStage][j+1]-min)*YUnit;
- pDC->LineTo(tempX+Ox,Oy-tempY);
- }
- pDC->SelectObject(OldPen);
- // 绘制说明文字如C0,D0等
- DataLen=m_nImageWidth+m_nImageHeight;
- for(j=0;j<=m_nDisplayStage;j++)
- {
- temp1=temp2;
- temp2=DataLen>>(m_nDisplayStage-j);
- tempX=(temp2-1)*XUnit;
- tempY=(Data[m_nDisplayStage][temp2-1]-min)*YUnit;
- pDC->MoveTo(tempX+Ox,Oy-tempY);
- pDC->LineTo(tempX+Ox,Oy);
- if(j==0)
- Text.Format("C%d",m_nDisplayStage);
- else
- Text.Format("D%d",m_nDisplayStage+1-j);
- tempX=((temp2-temp1-1)/2+temp1)*XUnit-5;
- pDC->SetTextColor(0x000000FF);
- pDC->TextOut(Ox+tempX,Oy+3,Text);
- }
- // 释放资源
- PenWhite.DeleteObject();
- PenGreen.DeleteObject();
- PenBlue.DeleteObject();
- Pen.DeleteObject();
- // 解除锁定
- ::GlobalUnlock((HGLOBAL)GetHDIB());
- }
- // Hn系数文件产生函数,因为Hn文件只需产生一次,所以程序中并没有使用此函数。
- // 若要重新产生Hn文件,读者可以调用此函数。
- BOOL CWavelet1DDlg::GenerateHnFile()
- {
- double Hn[10][20] ={ {0,0},
- { 0.4829629131445341, 0.8365163037378077, 0.2241438680420134,
- -0.1294095225512603 },
- { 0.3326705529500825, 0.8068915093110924, 0.4598775021184914,
- -0.1350110200102546,-0.0854412738820267, 0.0352262918857095 },
- { 0.2303778133088964, 0.7148465705529154, 0.6308807679398587,
- -0.0279837694168599,-0.1870348117190931, 0.0308413818355607,
- 0.0328830116668852,-0.0105974017850890 },
- { 0.1601023979741929, 0.6038292697971895, 0.7243085284377726,
- 0.1384281459013203,-0.2422948870663823,-0.0322448695846381,
- 0.0775714938400459,-0.0062414902127983,-0.0125807519990820,
- 0.0033357252854738 },
- { 0.1115407433501095, 0.4946238903984533, 0.7511339080210959,
- 0.3152503517091982,-0.2262646939654400,-0.1297668675672625,
- 0.0975016055873225, 0.0275228655303053,-0.0315820393174862,
- 0.0005538422011614, 0.0047772575109455, 0.0010773010853085 },
- { 0.0778520540850037, 0.3965393194818912, 0.7291320908461957,
- 0.4697822874051889,-0.1439060039285212,-0.2240361549938412,
- 0.0713092192668272, 0.0806123091510774,-0.0380299369350104,
- -0.0165745416306655, 0.0125509985560986, 0.0004295779729214,
- -0.0018016407040473, 0.0003537137999745 },
- { 0.0544158422431072, 0.3128715909143166, 0.6756307362973195,
- 0.5852546836542159,-0.0158291052563823,-0.2840155429615824,
- 0.0004724845739124, 0.1287474266204893,-0.0173693010018090,
- -0.0440882539307971, 0.0139810279174001, 0.0087160940474065,
- -0.0048703529934520,-0.0003917403733770, 0.0006754494064506,
- -0.0001174767841248 },
- { 0.0380779473638778, 0.2438346746125858, 0.6048231236900955,
- 0.6572880780512736, 0.1331973858249883,-0.2932737832791663,
- -0.0968407832229492, 0.1485407493381256, 0.0307256814793385,
- -0.0676328290613279, 0.0002509471148340, 0.0223616621236798,
- -0.0047232047577518,-0.0042815036824635, 0.0018476468830563,
- 0.0002303857645232,-0.0002519631889427, 0.0000393473203163 },
- { 0.0266700579005473, 0.1881768000776347, 0.5272011889315757,
- 0.6884590394534363, 0.2811723436605715,-0.2498464243271598,
- -0.1959462743772862, 0.1273693403357541, 0.0930573646035547,
- -0.0713941471663501,-0.0294575368218399, 0.0332126740593612,
- 0.0036065535669870,-0.0107331754833007, 0.0013953517470688,
- 0.0019924052951925,-0.0006858566949564,-0.001164668551285,
- 0.0000935886703202,-0.0000132642028945 },
- };
- BOOL flag=TRUE;
- FILE *fp;
- int i,j;
- for(i=0;i<10;i++)
- {
- if((fp=fopen(m_strFilterFile[i],"wb+"))==NULL)
- {
- AfxMessageBox("Hn算子文件生成失败!",MB_OK|MB_ICONINFORMATION);
- flag=FALSE;
- break;
- }
- for(j=0;j<2*(i+1);j++)
- fwrite(&Hn[i][j],sizeof(double),1,fp);
- fclose(fp);
- }
- return flag;
- }