www.pudn.com > hongmoyuan.rar > hongmoView.cpp
// hongmoView.cpp : implementation of the CHongmoView class // #include "stdafx.h" #include "hongmo.h" #include "hongmoDoc.h" #include "hongmoView.h" #include "GlobalApi.h" #include "GUIYIHUA.h" #include "Cdib.h" #include "math.h" #includeusing namespace std; #define pi 3.1415927 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CHongmoView IMPLEMENT_DYNCREATE(CHongmoView, CScrollView) BEGIN_MESSAGE_MAP(CHongmoView, CScrollView) //{{AFX_MSG_MAP(CHongmoView) ON_COMMAND(ID_CAOZUO_CANNYDETECTION, OnCaozuoCannydetection) ON_COMMAND(ID_CAOZUO_DINGWEI, OnCaozuoDingwei) ON_COMMAND(ID_CAOZUO_GUIYIHUA, OnCaozuoGuiyihua) ON_COMMAND(ID_CAOZUO_JUNHENG, OnCaozuoJunheng) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CHongmoView construction/destruction CHongmoView::CHongmoView() { // TODO: add construction code here } CHongmoView::~CHongmoView() { } BOOL CHongmoView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CScrollView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CHongmoView drawing void CHongmoView::OnDraw(CDC* pDC) { CHongmoDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here CSize sizeDibDisplay; if(!pDoc->m_pDibInit->IsEmpty()) { sizeDibDisplay = pDoc->m_pDibInit->GetDimensions(); pDoc->m_pDibInit->Draw(pDC,CPoint(0,0),sizeDibDisplay); } } ///////////////////////////////////////////////////////////////////////////// // CHongmoView printing BOOL CHongmoView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CHongmoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CHongmoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CHongmoView diagnostics #ifdef _DEBUG void CHongmoView::AssertValid() const { CScrollView::AssertValid(); } void CHongmoView::Dump(CDumpContext& dc) const { CScrollView::Dump(dc); } CHongmoDoc* CHongmoView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHongmoDoc))); return (CHongmoDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CHongmoView message handlers void CHongmoView::OnInitialUpdate() { // CView::OnInitialUpdate(); // TODO: Add your specialized code here and/or call the base class CScrollView::OnInitialUpdate(); CHongmoDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CSize sizeTotal = pDoc->m_pDibInit->GetDimensions(); //已改动 SetScrollSizes(MM_TEXT, sizeTotal); GetParentFrame()->RecalcLayout(); ResizeParentToFit(); } void CHongmoView::OnCaozuoCannydetection() { // TODO: Add your command handler code here CHongmoDoc * pDoc = (CHongmoDoc *)this->GetDocument(); CDib * pDib = pDoc->m_pDibInit; LPBITMAPINFOHEADER lpBMIH=pDib->m_lpBMIH; // 判断是否是8-bpp位图 if (lpBMIH->biBitCount != 8) { // 提示用户 MessageBox("目前只支持256色位虹膜图像的边缘检测!", "系统提示" , MB_ICONINFORMATION | MB_OK); // 返回 return; } //更改光标形状 BeginWaitCursor(); /*************************************************/ //循环变量 int i; int j; /*************************************************/ CSize sizeImage = pDib->GetDimensions(); int nWidth = sizeImage.cx; int nHeight= sizeImage.cy; int nSaveWidth = pDib->GetDibSaveDim().cx; // 开辟内存,存储图象数据 unsigned char * pUnchImage = new unsigned char[nWidth*nHeight]; for(i=0; i m_lpImage[i*nSaveWidth+j]; } } // canny算子计算后的结果 unsigned char * pUnchEdge = new unsigned char[nWidth*nHeight]; // 导数x的方向指针 double * CosTheta = new double [nWidth*nHeight]; // 导数y的方向指针 double * SinTheta = new double [nWidth*nHeight]; /*************************************************/ // 调用Canny()函数对虹膜图像进行边缘检测 Canny(pUnchImage, nWidth, nHeight, 0.8, 0.44, 0.83, pUnchEdge,CosTheta,SinTheta); /*************************************************/ for(i = 0; i < nHeight; i++) { for(j = 0; j < nWidth;j++) { pDib->m_lpImage[i*nWidth+j]=pUnchEdge[i*nWidth+j]; } } /*************************************************/ // 释放内存 delete []pUnchImage; pUnchImage = NULL; delete []pUnchEdge; pUnchEdge = NULL; delete []CosTheta; CosTheta = NULL; delete []SinTheta; SinTheta = NULL; /*************************************************/ // 恢复光标 EndWaitCursor(); // 设置脏标记 pDoc->SetModifiedFlag(TRUE); // 更新视图 pDoc->UpdateAllViews(NULL); } void CHongmoView::OnCaozuoDingwei() { // TODO: Add your command handler code here CHongmoDoc * pDoc = (CHongmoDoc *)this->GetDocument(); CDib * pDib = pDoc->m_pDibInit; LPBITMAPINFOHEADER lpBMIH=pDib->m_lpBMIH; // 判断是否是8-bpp位图 if (lpBMIH->biBitCount != 8) { // 提示用户 MessageBox("目前只支持256色位虹膜图像的边缘检测!", "系统提示" , MB_ICONINFORMATION | MB_OK); // 返回 return; } //更改光标形状 BeginWaitCursor(); /*************************************************/ //循环变量 int i; int j; /*************************************************/ CSize sizeImage = pDib->GetDimensions(); int nWidth = sizeImage.cx; int nHeight= sizeImage.cy; int nSaveWidth = pDib->GetDibSaveDim().cx; // 开辟内存,存储图象数据 unsigned char * pUnchImage = new unsigned char[nWidth*nHeight]; unsigned char* HoughImage = new unsigned char[nWidth*nHeight]; for(i=0; i m_lpImage[i*nSaveWidth+j]; } } for(i=0; i m_lpImage[i*nSaveWidth+j]; } } // canny算子计算后的结果 unsigned char * pUnchEdge = new unsigned char[nWidth*nHeight]; // 导数x的方向指针 double * CosTheta = new double [nWidth*nHeight]; // 导数y的方向指针 double * SinTheta = new double [nWidth*nHeight]; /*************************************************/ // 调用Canny()函数对虹膜图像进行边缘检测 Canny(pUnchImage, nWidth, nHeight, 0.8, 0.44, 0.83, pUnchEdge,CosTheta,SinTheta); /*************************************************/ // 确定内圆半径范围 int temp1 = 30; int temp2 = 70; int temp_2 = 0; int temp_3 = 0; int temp_4 = 0; // 调用Hough()函数提取内圆圆心和半径 Hough(temp1,temp2,nWidth,nHeight,CosTheta,SinTheta,pUnchEdge,temp_2,temp_3,temp_4); //在原图像上用Bresenham画圆算法生成小圆 Bresenham(temp_2,temp_3,temp_4,nWidth,nHeight,HoughImage); /*************************************************/ //去除不必要的边界点 for(i = 0; i < nHeight; i++) { for(j = 0; j < nWidth; j++) { if((i-temp_2)*(i-temp_2)+(j-temp_3)*(j-temp_3)>temp_4*temp_4 && (i-temp_2)*(i-temp_2)+(j-temp_3)*(j-temp_3)<(temp_4+50)*(temp_4+50)) { pUnchEdge[i*nWidth+j] = 0; } } } for(i = 0; i < nHeight; i++) { for(j = 0; j < nWidth; j++) { if((i-temp_2)*(i-temp_2)+(j-temp_3)*(j-temp_3)>(temp_4+80)*(temp_4+80)) { pUnchEdge[i*nWidth+j] = 0; } } } for(i = 0; i < nHeight; i++) { for(j = 0; j < nWidth; j++) { if(i*nWidth+j>(temp_2+temp_4)*nWidth) { pUnchEdge[i*nWidth+j] = 0; } } } /*************************************************/ // 确定外圆半径范围 int temp3 = 90; int temp4 = 120; int temp_5 = 0; int temp_6 = 0; int temp_7 = 0; // 调用Hough()函数提取外圆圆心和半径 Hough(temp3,temp4,nWidth,nHeight,CosTheta,SinTheta,pUnchEdge,temp_5,temp_6,temp_7); //在原图像上用Bresenham画圆算法生成大圆 Bresenham(temp_5,temp_6,temp_7,nWidth,nHeight,HoughImage); /*************************************************/ for(i = 0; i < nHeight; i++) { for(j = 0; j < nWidth;j++) { pDib->m_lpImage[i*nWidth+j]=HoughImage[i*nWidth+j]; } } /*************************************************/ // 释放内存 delete []pUnchImage; pUnchImage = NULL; delete []HoughImage; HoughImage = NULL; delete []pUnchEdge; pUnchEdge = NULL; delete []CosTheta; CosTheta = NULL; delete []SinTheta; SinTheta = NULL; /*************************************************/ // 设置脏标记 pDoc->SetModifiedFlag(TRUE); // 更新视图 pDoc->UpdateAllViews(NULL); } void CHongmoView::OnCaozuoGuiyihua() { // TODO: Add your command handler code here CHongmoDoc * pDoc = (CHongmoDoc *)this->GetDocument(); CDib * pDib = pDoc->m_pDibInit; LPBITMAPINFOHEADER lpBMIH=pDib->m_lpBMIH; // 判断是否是8-bpp位图 if (lpBMIH->biBitCount != 8) { // 提示用户 MessageBox("目前只支持256色位虹膜图像的边缘检测!", "系统提示" , MB_ICONINFORMATION | MB_OK); // 返回 return; } //更改光标形状 BeginWaitCursor(); /*************************************************/ //循环变量 int i; int j; /*************************************************/ CSize sizeImage = pDib->GetDimensions(); int nWidth = sizeImage.cx; int nHeight= sizeImage.cy; int nSaveWidth = pDib->GetDibSaveDim().cx; // 开辟内存,存储图象数据 unsigned char * pUnchImage = new unsigned char[nWidth*nHeight]; unsigned char* HoughImage = new unsigned char[nWidth*nHeight]; for(i=0; i m_lpImage[i*nSaveWidth+j]; } } for(i=0; i m_lpImage[i*nSaveWidth+j]; } } // canny算子计算后的结果 unsigned char * pUnchEdge = new unsigned char[nWidth*nHeight]; // 导数x的方向指针 double * CosTheta = new double [nWidth*nHeight]; // 导数y的方向指针 double * SinTheta = new double [nWidth*nHeight]; /*************************************************/ // 调用Canny()函数对虹膜图像进行边缘检测 Canny(pUnchImage, nWidth, nHeight, 0.8, 0.44, 0.83, pUnchEdge,CosTheta,SinTheta); /*************************************************/ // 确定内圆半径范围 int temp1 = 30; int temp2 = 70; int temp_2 = 0; int temp_3 = 0; int temp_4 = 0; // 调用Hough()函数提取内圆圆心和半径 Hough(temp1,temp2,nWidth,nHeight,CosTheta,SinTheta,pUnchEdge,temp_2,temp_3,temp_4); //在原图像上用Bresenham画圆算法生成内圆 Bresenham(temp_2,temp_3,temp_4,nWidth,nHeight,HoughImage); /*************************************************/ //去除不必要的边界点 for(i = 0; i < nHeight; i++) { for(j = 0; j < nWidth; j++) { if((i-temp_2)*(i-temp_2)+(j-temp_3)*(j-temp_3)>temp_4*temp_4 && (i-temp_2)*(i-temp_2)+(j-temp_3)*(j-temp_3)<(temp_4+50)*(temp_4+50)) { pUnchEdge[i*nWidth+j] = 0; } } } for(i = 0; i < nHeight; i++) { for(j = 0; j < nWidth; j++) { if((i-temp_2)*(i-temp_2)+(j-temp_3)*(j-temp_3)>(temp_4+80)*(temp_4+80)) { pUnchEdge[i*nWidth+j] = 0; } } } for(i = 0; i < nHeight; i++) { for(j = 0; j < nWidth; j++) { if(i*nWidth+j>(temp_2+temp_4)*nWidth) { pUnchEdge[i*nWidth+j] = 0; } } } /*************************************************/ // 确定外圆半径范围 int temp3 = 90; int temp4 = 120; int temp_5 = 0; int temp_6 = 0; int temp_7 = 0; // 调用Hough()函数提取外圆圆心和半径 Hough(temp3,temp4,nWidth,nHeight,CosTheta,SinTheta,pUnchEdge,temp_5,temp_6,temp_7); //在原图像上用Bresenham画圆算法生成大圆 Bresenham(temp_5,temp_6,temp_7,nWidth,nHeight,HoughImage); /*************************************************/ // 归一化后图像的宽度和高度 int nNewWidth = 1024; int nNewHeight = 128; // 存储归一化后的图像 unsigned char *NormalizeImage = new unsigned char[nNewWidth*nNewHeight]; for(i = 0; i < nNewHeight; i++) { for(j = 0; j < nNewWidth;j++) { NormalizeImage[i*nNewWidth+j] = 0; } } /*************************************************/ // 归一化 Normalization(NormalizeImage,HoughImage,temp_2,temp_3,temp_4,temp_5,temp_6,temp_7,nWidth,nHeight); /*************************************************/ // 直方图均衡 InteEqualize(NormalizeImage,nNewWidth,nNewHeight); /*************************************************/ m_pDibResult = new CDib(CSize(nNewWidth,nNewHeight), 8); // 拷贝调色板 memcpy(m_pDibResult->m_lpvColorTable, pDib->m_lpvColorTable, m_pDibResult->m_nColorTableEntries*sizeof(RGBQUAD)); // 应用调色板 m_pDibResult->MakePalette(); // 将指针赋值给CDib类的数据 m_pDibResult->m_lpImage = NormalizeImage; /**************************************************/ CString strSaveFileType = "位图文件 (*.bmp)|*.bmp; *.dib|All Files (*.*)|*.*||"; CFileDialog FileDlg(FALSE, "*.bmp", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strSaveFileType); CFile fileOpen ; if( FileDlg.DoModal() == IDOK ) { if(!fileOpen.Open( FileDlg.GetPathName() , CFile::modeCreate|CFile::modeWrite )) { AfxMessageBox("cannot create the file to save!"); return; } if( !m_pDibResult->Write( &fileOpen ) ) { return; } fileOpen.Close(); } /**************************************************/ // 显示归一化后的图象 GUIYIHUA* pDlg; pDlg = new GUIYIHUA(NULL,m_pDibResult); pDlg->DoModal(); /*************************************************/ // 释放内存 delete []pUnchImage; pUnchImage = NULL; delete []HoughImage; HoughImage = NULL; delete []pUnchEdge; pUnchEdge = NULL; delete []NormalizeImage; NormalizeImage = NULL; delete []CosTheta; CosTheta = NULL; delete []SinTheta; SinTheta = NULL; } void CHongmoView::OnCaozuoJunheng() { // TODO: Add your command handler code here CHongmoDoc * pDoc = (CHongmoDoc *)this->GetDocument(); CDib * pDib = pDoc->m_pDibInit; LPBITMAPINFOHEADER lpBMIH=pDib->m_lpBMIH; // 判断是否是8-bpp位图 if (lpBMIH->biBitCount != 8) { // 提示用户 MessageBox("目前只支持256色位虹膜图像的边缘检测!", "系统提示" , MB_ICONINFORMATION | MB_OK); // 返回 return; } //更改光标形状 BeginWaitCursor(); /*************************************************/ //循环变量 int i; int j; /*************************************************/ CSize sizeImage = pDib->GetDimensions(); int nWidth = sizeImage.cx; int nHeight= sizeImage.cy; int nSaveWidth = pDib->GetDibSaveDim().cx; // 开辟内存,存储图象数据 unsigned char * pUnchImage = new unsigned char[nWidth*nHeight]; for(i=0; i m_lpImage[i*nSaveWidth+j]; } } /*************************************************/ //调用InteEqualize()函数进行直方图均衡 InteEqualize(pUnchImage,nWidth,nHeight); /*************************************************/ for(i = 0; i < nHeight; i++) { for(j = 0; j < nWidth;j++) { pDib->m_lpImage[i*nWidth+j]=pUnchImage[i*nWidth+j]; } } /*************************************************/ // 释放内存 delete []pUnchImage; pUnchImage = NULL; // 恢复光标 EndWaitCursor(); // 设置脏标记 pDoc->SetModifiedFlag(TRUE); // 更新视图 pDoc->UpdateAllViews(NULL); }