1、数学形态学处理程序实例/ JEFFView.cpp : implementation of the CJEFFView class/#include “stdafx.h“#include “JEFF.h“#include “JEFFDoc.h“#include “JEFFView.h“#define Out(x,y) lpPoints(x)+(y)*nWidth#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CJEFFViewIMPLEMENT_DYNCREATE(
2、CJEFFView, CScrollView)BEGIN_MESSAGE_MAP(CJEFFView, CScrollView)/AFX_MSG_MAP(CJEFFView)ON_COMMAND(ID_EROSION, OnErosion)ON_COMMAND(ID_DILATION, OnDilation)ON_COMMAND(ID_OPEN, OnOpen)ON_COMMAND(ID_CLOSE, OnClose)ON_COMMAND(ID_ZAOSHENG, OnZaosheng)ON_COMMAND(ID_BIANYUAN, OnBianyuan)ON_COMMAND(ID_1, On
3、1)ON_COMMAND(ID_2, On2)ON_COMMAND(ID_4, On4)ON_COMMAND(ID_8, On8)ON_COMMAND(ID_DILATION2, OnDilation2)ON_COMMAND(ID_EROSION2, OnErosion2)ON_COMMAND(ID_OPEN2, OnOpen2)ON_COMMAND(ID_CLOSE2, OnClose2)ON_COMMAND(ID_SMOOTH, OnSmooth)ON_COMMAND(ID_RELOAD, OnReload)ON_COMMAND(ID_TIDU, OnTidu)ON_COMMAND(ID_
4、BIANYUAN_H, OnBianyuanH)ON_COMMAND(ID_QUSE, OnQuse)ON_WM_LBUTTONDOWN()/AFX_MSG_MAP/ Standard printing commandsON_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()/
5、 CJEFFView construction/destructionCJEFFView:CJEFFView()/ TODO: add construction code hereValidDoc=FALSE;m_i=1;CJEFFView:CJEFFView()BOOL CJEFFView:PreCreateWindow(CREATESTRUCT/ CJEFFView drawingvoid CJEFFView:OnDraw(CDC* pDC)CJEFFDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for
6、 native data hereif (pDoc-lpBitmap)ValidDoc=TRUE;nWidth=pDoc-nWidth;nHeight=pDoc-nHeight;lpBits=pDoc-lpBits;lpPoints=pDoc-lpPoints;BITMAPINFOHEADER bmi;bmi.biSize=sizeof(BITMAPINFOHEADER);bmi.biWidth=nWidth;bmi.biHeight=nHeight;bmi.biPlanes=1;bmi.biBitCount=24;bmi.biCompression=BI_RGB;bmi.biSizeImag
7、e=0;bmi.biXPelsPerMeter=0;bmi.biYPelsPerMeter=0;bmi.biClrUsed=0;bmi.biClrImportant=0;StretchDIBits(pDC-m_hDC,0,0,nWidth*m_i,nHeight*m_i,0,0,nWidth,nHeight,lpBits,(BITMAPINFO *)void CJEFFView:OnInitialUpdate()CScrollView:OnInitialUpdate();CSize sizeTotal;/ TODO: calculate the total size of this views
8、izeTotal.cx =2000; sizeTotal.cy = 1700;SetScrollSizes(MM_TEXT, sizeTotal);/ CJEFFView printingBOOL CJEFFView:OnPreparePrinting(CPrintInfo* pInfo)/ default preparationreturn DoPreparePrinting(pInfo);void CJEFFView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add extra initialization be
9、fore printingvoid CJEFFView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add cleanup after printing/ CJEFFView diagnostics#ifdef _DEBUGvoid CJEFFView:AssertValid() constCScrollView:AssertValid();void CJEFFView:Dump(CDumpContextCJEFFDoc* CJEFFView:GetDocument() / non-debug version is inl
10、ineASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CJEFFDoc);return (CJEFFDoc*)m_pDocument;#endif /_DEBUG/ CJEFFView message handlers/二值形态学:腐蚀void CJEFFView:OnErosion() int *lpMarks=new intnWidth*nHeight;int x,y,x1,y1;int count;/中间变量for(y=1;yPutPoint();Invalidate();/二值形态学:膨胀void CJEFFView:OnDilation() int
11、 x,y,x1,y1;int count;/中间变量int *lpMarks=new intnWidth*nHeight;for(y=1;yPutPoint();Invalidate();/二值形态学:开启void CJEFFView:OnOpen() /先腐蚀后膨胀OnErosion();OnDilation();/二值形态学:闭合void CJEFFView:OnClose() /先膨胀后腐蚀OnDilation();OnErosion();/二值形态学:噪声滤除void CJEFFView:OnZaosheng() /先开启后闭合OnOpen();OnClose();/二值形态学:边缘提
12、取void CJEFFView:OnBianyuan() int *lpMarks=new intnWidth*nHeight;/用于保存腐蚀图象灰度值int *lpMarks1=new intnWidth*nHeight;/用于保存原图象灰度值int x,y,x1,y1,count;for(y=0;yPutPoint();Invalidate();/放大镜:1 倍void CJEFFView:On1() m_i=1;Invalidate();/放大镜:2 倍void CJEFFView:On2() m_i=2;Invalidate();/放大镜:4 倍void CJEFFView:On4()
13、 m_i=4;Invalidate();/放大镜:8 倍void CJEFFView:On8() m_i=8;Invalidate();/灰度形态学:膨胀void CJEFFView:OnDilation2() int *lpMarks=new intnWidth*nHeight;/定义一块内存,大小为图象像素数int x,y,x1,y1,s,t;for(y=1;yPutPoint();Invalidate();/灰度形态学:腐蚀void CJEFFView:OnErosion2() int x,y,x1,y1,s,t;int *lpMarks=new intnWidth*nHeight;/定
14、义一块内存,大小为图象像素数for(y=1;yt) t=s;/将九个点灰度的最大值传给 t lpMarksx+y*nWidth=t;/将处理后的图像灰度值暂存于内存中 for(y=1;yPutPoint();Invalidate();/灰度形态学:开启void CJEFFView:OnOpen2() /先腐蚀后膨胀OnErosion2();OnDilation2();/灰度形态学:闭合void CJEFFView:OnClose2() /先膨胀后腐蚀OnDilation2();OnErosion2();/灰度形态学:平滑void CJEFFView:OnSmooth() /先开启后闭合OnOpen2();OnClose2();/灰度形态学:梯度void CJEFFView:OnTidu() / TODO: Add your command handler code hereint *lpMarks=new intnWidth*nHeight;int x,y,x1,y1,s,t;for(y=1;ynHeight-1;y+)for(x=1;xnWidth-1;x+)s=t=255;for(y1=-1;y12;y1+) for(x1=-1;x12;x1+) s=Out(x+x1,y+y1);if(st) t=s;