www.pudn.com > vehiclehsv.rar > Bmptestview.cpp, change:2003-10-20,size:66807b


// BmpTestView.cpp : implementation of the CBmpTestView class 
// 
#include "stdafx.h" 
#include "BmpTest.h" 
#include "stack.h" 
#include "BmpTestDoc.h" 
#include "BmpTestView.h" 
#include <math.h> 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
#define MIN(a,b) (a<b? a:b) 
#define MAX(a,b) (a>b? a:b) 
#define NO_HUE -1; 
int m_Grade[256],m_Gradmax=0,m_Grade_Blue[256],m_Grade_Green[256],m_Grade_Red[256],b[1024][1024]; 
extern void str(int quotient,char addr[15]); 
extern BOOL  Contour(CBmpTestDoc* pDoc,double *pAnew_mon,BOOL bmoment,double n,double *protate); 
extern BOOL  GCC(CBmpTestDoc* pDoc,double *n,POINT *gcc); 
extern void Sh_Rotate(CBmpTestDoc* pDoc,double ro_angle,double size,POINT *pav_cent); 
DISTANCE distbmp[50]; 
 
///////////////////////////////////////////////////////////////////////////// 
// CBmpTestView 
 
IMPLEMENT_DYNCREATE(CBmpTestView, CScrollView) 
 
BEGIN_MESSAGE_MAP(CBmpTestView, CScrollView) 
	//{{AFX_MSG_MAP(CBmpTestView) 
	ON_COMMAND(ID_BT_BLACKNESS, OnBtBlackness) 
	ON_COMMAND(ID_BT_DSTINVERT, OnBtDstinvert) 
	ON_COMMAND(ID_BT_MERGECOPY, OnBtMergecopy) 
	ON_COMMAND(ID_BT_MERGEPAINT, OnBtMergepaint) 
	ON_COMMAND(ID_BT_NOTSRCCOPY, OnBtNotsrccopy) 
	ON_COMMAND(ID_BT_NOTSRCERASE, OnBtNotsrcerase) 
	ON_COMMAND(ID_BT_PATCOPY, OnBtPatcopy) 
	ON_COMMAND(ID_BT_PATINVERT, OnBtPatinvert) 
	ON_COMMAND(ID_BT_PATPAINT, OnBtPatpaint) 
	ON_COMMAND(ID_BT_SRCAND, OnBtSrcand) 
	ON_COMMAND(ID_BT_SRCCOPY, OnBtSrccopy) 
	ON_COMMAND(ID_BT_SRCERASE, OnBtSrcerase) 
	ON_COMMAND(ID_BT_SRCINVERT, OnBtSrcinvert) 
	ON_COMMAND(ID_BT_SRCPAINT, OnBtSrcpaint) 
	ON_COMMAND(ID_BT_WHITENESS, OnBtWhiteness) 
	ON_COMMAND(ID_ZFT_SHOW, OnZftShow) 
	ON_COMMAND(ID_ZFT_BALANCE, OnZftBalance) 
	ON_COMMAND(ID_IMAGE_SHOW, OnImageShow) 
	ON_COMMAND(ID_DTSM_IN, OnDtsmIn) 
	ON_COMMAND(ID_DTSM_SHOW, OnDtsmShow) 
	ON_COMMAND(ID_DTSM_OUT, OnDtsmOut) 
	ON_COMMAND(ID_REC_FILT, OnRecFilt) 
	ON_COMMAND(ID_MID_FILT, OnMidFilt) 
	ON_COMMAND(ID_EDGE_ROBERT, OnEdgeRobert) 
	ON_COMMAND(ID_EDGE_LAPLACE, OnEdgeLaplace) 
	ON_COMMAND(ID_GAUSE_FILT, OnGauseFilt) 
	ON_COMMAND(ID_EDGE_MARR, OnEdgeMarr) 
	ON_COMMAND(ID_STRU_ADD, OnStruAdd) 
	ON_COMMAND(ID_STRU_SUB, OnStruSub) 
	ON_COMMAND(ID_STRU_OPEN, OnStruOpen) 
	ON_COMMAND(ID_STRU_CLOSE, OnStruClose) 
	ON_COMMAND(ID_EDIT_BACKUP, OnEditBackup) 
	ON_COMMAND(ID_EDIT_BLUE, OnEditBlue) 
	ON_COMMAND(ID_EDIT_GREEN, OnEditGreen) 
	ON_COMMAND(ID_EDIT_RED, OnEditRed) 
	ON_COMMAND(ID_EDGE_COLORPREWITT, OnEdgeColorprewitt) 
	ON_COMMAND(ID_EDGE_COLORROBERT, OnEdgeColorrobert) 
	ON_COMMAND(ID_MID_FILT_LINE, OnMidFiltLine) 
	ON_COMMAND(ID_DENSITY_SCAN, OnDensityScan) 
	ON_COMMAND(ID_ROW_DILATION, OnRowDilation) 
	ON_COMMAND(ID_EDGE_COLORPREWITT1, OnEdgeColorprewitt1) 
	ON_COMMAND(ID_EDGE_COLORROBERT1, OnEdgeColorrobert1) 
	ON_COMMAND(ID_ROW_EROSION, OnRowErosion) 
	ON_COMMAND(ID_CLOSE, OnClose) 
	ON_COMMAND(ID_GET, OnGet) 
	ON_COMMAND(ID_RELA_SCAN, OnRelaScan) 
	ON_COMMAND(ID_DENSITY_SCAN_MID, OnDensityScanMid) 
	ON_COMMAND(ID_AREA_MARK, OnAreaMark) 
	ON_COMMAND(ID_LOCATING, OnLocating) 
	ON_COMMAND(ID_VEHICLE_BITMAP, OnVehicleBitmap) 
	ON_COMMAND(ID_BLUE_CUT, OnBlueCut) 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CBmpTestView construction/destruction 
 
CBmpTestView::CBmpTestView() 
{ 
	// TODO: add construction code here 
	m_Rop=SRCCOPY; 
} 
 
CBmpTestView::~CBmpTestView() 
{ 
} 
 
BOOL CBmpTestView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CScrollView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CBmpTestView drawing 
 
void CBmpTestView::OnDraw(CDC* pDC) 
{ 
	CPen pen,*open; 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
//设置滚动视图大小,使之与Dib大小一致: 
	CSize sizeTotal; 
	sizeTotal.cx = pDoc->m_Dib.Width; 
	sizeTotal.cy = pDoc->m_Dib.Height+100; 
	SetScrollSizes(MM_TEXT, sizeTotal); 
//绘制Dib: 
	if (pDoc->m_BM) 
	{ 
//在滚动视图中绘制背景: 
    	CBitmap pat; 
	    CBrush br,* obr; 
		CRect rc; 
		rc.top=0; 
		rc.left=0; 
		rc.right=sizeTotal.cx; 
		rc.bottom=sizeTotal.cy; 
		GetClientRect(&rc);  //获得矩形区域大小 
		pat.LoadBitmap(IDB_BITMAP2); //IDB_BITMAP2->pat 
		br.CreatePatternBrush(&pat); //创建模式刷, pat->br 
		obr=pDC->SelectObject(&br); 
		pDC->FillRect(&rc,&br); //用画刷填充整个矩形区域 
		pDC->SelectObject(obr); 
		br.DeleteObject();  
		CRect dibrect; 
		dibrect.top=0; 
		dibrect.left=0; 
		dibrect.right=sizeTotal.cx; 
		dibrect.bottom=sizeTotal.cy; 
		pDoc->m_Dib.Draw(pDC->GetSafeHdc(),dibrect,dibrect); 
    } 
/*绘制DDB: 
	CBitmap ddb; 
	ddb.LoadBitmap( IDB_BITMAP1 ); 
    // Calculate bitmap size using a BITMAP structure. 
    BITMAP bm; 
    ddb.GetObject( sizeof(BITMAP), &bm ); 
    // Create a memory DC, select the bitmap into the 
    // memory DC, and BitBlt it into the view. 
    CDC dcMem; 
    dcMem.CreateCompatibleDC( pDC ); 
    CBitmap* pbmpOld = dcMem.SelectObject( &ddb ); 
    pDC->BitBlt( dibrect.Width()/2,dibrect.Height()/2, 
				 dibrect.Width()/2+bm.bmWidth,dibrect.Height()/2+bm.bmHeight, 
                 &dcMem, 
				 0,0, 
				 m_Rop  
				 );  
    // Reselect the original bitmap into the memory DC. 
    dcMem.SelectObject( pbmpOld ); */ 
	else 
	{ 
    	CBrush br,* obr; 
	    CRect rc; 
		CString s; 
		rc.top=0; 
		rc.left=0; 
		rc.right=sizeTotal.cx; 
		rc.bottom=sizeTotal.cy; 
    	GetClientRect(&rc); 
	    br.CreateSolidBrush(RGB(255,255,255)); 
	    obr=pDC->SelectObject(&br); 
    	pDC->FillRect(&rc,&br); 
	    pDC->SelectObject(obr); 
	    br.DeleteObject();  
		if(pDoc->m_Dib.BPP>=16) 
		{ 
			pen.CreatePen(0,2,RGB(0,0,255)); 
    		open=pDC->SelectObject(&pen); 
			pDC->MoveTo(10,150); 
			pDC->LineTo(520,150); 
    		int i,j=10,k=0; 
			for ( i=0;i<256;i=i+20) 
			{	 
				s.Format("%d",i); 
               pDC->TextOut(10+i*2, 150,  s); 
			} 
			for ( i=0;i<256;i++) 
			{	 
    			pDC->MoveTo(j,150); 
			    if (m_Gradmax!=0) k=150-(int)m_Grade_Blue[i]*150/m_Gradmax; 
	        	pDC->LineTo(j,k); 
        		j=j+2; 
			} 
			pen.DeleteObject(); 
			pen.CreatePen(0,2,RGB(0,255,0)); 
    		open=pDC->SelectObject(&pen); 
			pDC->MoveTo(10,310); 
			pDC->LineTo(520,310); 
			j=10; 
			for ( i=0;i<256;i=i+20) 
			{					 
				s.Format("%d",i); 
               pDC->TextOut(10+i*2, 310, s); 
			} 
			for ( i=0;i<256;i++) 
			{	 
    			pDC->MoveTo(j,310); 
			    if (m_Gradmax!=0) k=310-(int)m_Grade_Green[i]*150/m_Gradmax; 
	        	pDC->LineTo(j,k); 
        		j=j+2; 
			} 
			pen.DeleteObject(); 
			pen.CreatePen(0,2,RGB(255,0,0)); 
    		open=pDC->SelectObject(&pen); 
			pDC->MoveTo(10,470); 
			pDC->LineTo(520,470); 
			for ( i=0;i<256;i=i+20) 
			{	 
				s.Format("%d",i); 
               pDC->TextOut(10+i*2, 470, s); 
			} 
			j=10; 
			for ( i=0;i<256;i++) 
			{	 
    			pDC->MoveTo(j,470); 
			    if (m_Gradmax!=0) k=470-(int)m_Grade_Red[i]*150/m_Gradmax; 
	        	pDC->LineTo(j,k); 
        		j=j+2; 
			} 
			j=10; 
			pDC->SelectObject(open); 
			pen.DeleteObject(); 
			pDC->MoveTo(j,10); 
		} 
		else  
		{ 
    		pen.CreatePen(0,2,RGB(0,0,0)); 
    		open=pDC->SelectObject(&pen); 
			pDC->MoveTo(10,260); 
			pDC->LineTo(520,260); 
			pDC->TextOut(200, 300, " 的直方图"); 
    		int i,j=10,k=0; 
			for ( i=0;i<256;i=i+20) 
			{	 
				s.Format("%d",i); 
				pDC->TextOut(10+i*2, 260, s); 
			} 
			for ( i=0;i<256;i++) 
			{	 
    			pDC->MoveTo(j,260); 
			    if (m_Gradmax!=0) k=260-(int)m_Grade[i]*250/m_Gradmax; 
	        	pDC->LineTo(j,k); 
        		j=j+2; 
			} 
			pDC->SelectObject(open); 
			pen.DeleteObject(); 
			pDC->MoveTo(j,10); 
		} 
	} 
} 
 
void CBmpTestView::OnInitialUpdate() 
{ 
	CScrollView::OnInitialUpdate(); 
 
	CSize sizeTotal; 
	// TODO: calculate the total size of this view 
	sizeTotal.cx = sizeTotal.cy = 100; 
	SetScrollSizes(MM_TEXT, sizeTotal); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CBmpTestView printing 
 
BOOL CBmpTestView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CBmpTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CBmpTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CBmpTestView diagnostics 
 
#ifdef _DEBUG 
void CBmpTestView::AssertValid() const 
{ 
	CScrollView::AssertValid(); 
} 
 
void CBmpTestView::Dump(CDumpContext& dc) const 
{ 
	CScrollView::Dump(dc); 
} 
 
CBmpTestDoc* CBmpTestView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBmpTestDoc))); 
	return (CBmpTestDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CBmpTestView message handlers 
 
void CBmpTestView::OnBtBlackness()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=BLACKNESS; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtDstinvert()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=DSTINVERT; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtMergecopy()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=MERGECOPY; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtMergepaint()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=MERGEPAINT; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtNotsrccopy()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=NOTSRCCOPY; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtNotsrcerase()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=NOTSRCERASE; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtPatcopy()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=PATCOPY; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtPatinvert()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=PATINVERT; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtPatpaint()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=PATPAINT; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtSrcand()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=SRCAND; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtSrccopy()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=SRCCOPY; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtSrcerase()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=SRCERASE; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtSrcinvert()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=SRCINVERT; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtSrcpaint()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=SRCPAINT; 
	Invalidate(); 
} 
 
void CBmpTestView::OnBtWhiteness()  
{ 
	// TODO: Add your command handler code here 
	m_Rop=WHITENESS; 
	Invalidate(); 
} 
 
void CBmpTestView::OnZftShow(CDC *pDC)  
{ 
	// TODO: Add your command handler code here 
/*	CRect rc; 
	GetClientRect(&rc); */ 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(!pDoc->m_Dib.m_BmpLoaded) 
	{ 
		AfxMessageBox("还没有打开图像文件"); 
		return; 
	} 
    int i; 
    long j; 
	if (pDoc->m_Dib.BPP>=16) 
    {    
	   m_Gradmax=0; 
	   for ( i=0;i<256;i++) 
	   { 
	      m_Grade_Blue[i]=0; 
		  m_Grade_Green[i]=0; 
		  m_Grade_Red[i]=0; 
	   } 
       for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j=j+3) 
	   { 
	      m_Grade_Blue[int(*(pDoc->m_Dib.m_Buffer+j))]++; 
		  m_Grade_Green[int(*(pDoc->m_Dib.m_Buffer+j+1))]++; 
		  m_Grade_Red[int(*(pDoc->m_Dib.m_Buffer+j+2))]++; 
	   } 
       for ( i=0;i<256;i++) 
	   { 
	     if (m_Grade_Blue[i]>m_Gradmax) 
			m_Gradmax=m_Grade_Blue[i]; 
		 if (m_Grade_Green[i]>m_Gradmax) 
			m_Gradmax=m_Grade_Green[i]; 
		 if (m_Grade_Red[i]>m_Gradmax) 
			m_Gradmax=m_Grade_Red[i]; 
	   } 
	}	 
    else if (pDoc->m_Dib.BPP==8) 
	{ 
	   m_Gradmax=0; 
       for ( i=0;i<256;i++) 
	   { 
	      m_Grade[i]=0; 
	   } 
       for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	   { 
	      m_Grade[int(*(pDoc->m_Dib.m_Buffer+j))]++; 
	   } 
	   m_Gradmax=0; 
       for ( i=0;i<256;i++) 
	   { 
	     if (m_Grade[i]>m_Gradmax) 
			m_Gradmax=m_Grade[i]; 
	   } 
	} 
	else 
	{ 
	   long int black=0, white=0; 
	   int a; 
	   for ( i=0;i<256;i++) 
	   { 
	      m_Grade[i]=0; 
	   } 
       for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	   { 
		   a=128; 
	       for ( i=0;i<8;i++) 
		   { 
			    if( ((int)*(pDoc->m_Dib.m_Buffer+j) & a)==0) 
					black++; 
			    else 
					white++; 
			    a=a>>1; 
		   } 
	   } 
	   if (black>white) 
			m_Gradmax=black; 
	   else 
		   m_Gradmax=white; 
       m_Grade[0]=black; 
	   m_Grade[255]=white; 
	} 
	pDoc->m_BM=false; 
	Invalidate(); 
} 
 
void CBmpTestView::OnZftBalance()  
{ 
	// TODO: Add your command handler code here 
 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
    int i; 
	double m_GradAdd[256]; 
	long j; 
    m_GradAdd[0]=m_Grade[0]; 
// calculate integral 
	for ( i=1;i<256;i++) 
	{ 
	    m_GradAdd[i]=0; 
	} 
	for ( i=1;i<256;i++) 
	{ 
	    m_GradAdd[i]=m_GradAdd[i-1]+m_Grade[i]; 
	} 
	for ( i=1;i<256;i++) 
	{ 
	    m_GradAdd[i]=m_GradAdd[i]*255/m_GradAdd[255]; 
	} 
// 归一化阈值面积function 
    for ( j=0;j<(long int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(pDoc->m_Dib.m_Buffer+j)=(int) (m_GradAdd[(*(pDoc->m_Dib.m_Buffer+j))]*255/m_GradAdd[255]); 
	} 
	for ( i=0;i<256;i++) 
	{ 
	    m_Grade[i]=0; 
	} 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	m_Grade[int(*(pDoc->m_Dib.m_Buffer+j))]++; 
	} 
	for ( i=0;i<256;i++) 
	{ 
	    if (m_Grade[i]>m_Gradmax) 
			m_Gradmax=m_Grade[i]; 
	} 
	pDoc->m_BM=false; 
	Invalidate(); 
} 
 
void CBmpTestView::OnImageShow()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnDtsmIn()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP!=8) 
	{ 
       AfxMessageBox( "此图像为非256色图像,目前该算子还不支持这种图像格式"); 
	   return; 
	} 
	float T0,u1,u2,T1,w1,w2,u; 
	int i,decimal, sign; 
	long j; 
	char string[80]="二值化最佳阈值为",*string1,*string2; 
 	for ( i=0;i<256;i++) 
	{ 
	    m_Grade[i]=0; 
	} 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	m_Grade[int(*(pDoc->m_Dib.m_Buffer+j))]++; 
	} 
	u=0; 
	for(i=0;i<256;i++) 
		u=u+(float) i*m_Grade[i]/(int) pDoc->m_Dib.BufferSize; 
	T0=u;T1=0; 
    while (fabs(T0-T1)>0.01) 
	{ 
		w1=0;w2=0;u1=0;u2=0; 
		for(i=0;i<=int(T0);i++) 
		{ 
			w1=w1+m_Grade[i]; 
			u1=u1+i*m_Grade[i]; 
		} 
		for(i=int(T0)+1;i<256;i++) 
		{ 
			w2=w2+m_Grade[i]; 
			u2=u2+i*m_Grade[i]; 
		} 
		T1=T0;u1=u1/w1;u2=u2/w2;T0=0.5*(u1+u2); 
	} 
	string2=_fcvt(T0, 0, &decimal, &sign ); 
	string1=strcat(string,string2); 
    AfxMessageBox(string1); 
    for ( j=0;j<(long int) pDoc->m_Dib.BufferSize;j++) 
	{ 
		*(pDoc->m_Dib.m_Buffer+j)=(*(pDoc->m_Dib.m_Buffer+j)>=T0)?255:0; 
	} 
} 
 
 
void CBmpTestView::OnDtsmShow()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestView::OnImageShow();	 
} 
 
void CBmpTestView::OnDtsmOut()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	float u,uT[255],w[255],sigma[255],sigmamax; 
	int i,T,Topt; 
	long j; 
	char string[80]="类间方差法二值化最佳阈值为",*string1,*string2,buffer[10]; 
 	for ( i=0;i<256;i++) 
	{ 
	    m_Grade[i]=0; 
	} 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	m_Grade[int(*(pDoc->m_Dib.m_Buffer+j))]++; 
	} 
	u=0; 
	for(i=0;i<256;i++) 
	{ 
		u=u+i*m_Grade[i]; 
	} 
	u=u/pDoc->m_Dib.BufferSize; 
	sigmamax=0;Topt=0; 
	for(T=0;T<256;T++) 
	{ 
		w[T]=0;uT[T]=0; 
		for(i=0;i<=T;i++) 
		{ 
			w[T]=w[T]+m_Grade[i]; 
			uT[T]=uT[T]+i*m_Grade[i]; 
		} 
		uT[T]=uT[T]/pDoc->m_Dib.BufferSize; 
		w[T]=w[T]/pDoc->m_Dib.BufferSize; 
		sigma[T]=(u*w[T]-uT[T])*(u*w[T]-uT[T])/w[T]*(1-w[T]); 
		if(sigma[T]>sigmamax) 
		{ 
			sigmamax=sigma[T];Topt=T; 
		} 
	} 
	string2=_itoa(Topt, buffer,10); 
	string1=strcat(string,string2); 
    AfxMessageBox(string1);	 
} 
 
void CBmpTestView::OnRecFilt()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	int i,j,image[4][4],Height,Width; 
	//filtering 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
 	for (i=1;i<Height-1;i++) 
	{ 
	    for (j=1;j<Width +1;j++) 
		{ 
			image[2][2]=(int) *(pDoc->m_Dib.m_Buffer1+i*Width+j); 
			image[2][1]=(int) *(pDoc->m_Dib.m_Buffer1+i*Width+j-1); 
			image[2][3]=(int) *(pDoc->m_Dib.m_Buffer1+i*Width+j+1); 
			image[1][2]=(int) *(pDoc->m_Dib.m_Buffer1+(i-1)*Width+j); 
			image[1][1]=(int) *(pDoc->m_Dib.m_Buffer1+(i-1)*Width+j-1); 
			image[1][3]=(int) *(pDoc->m_Dib.m_Buffer1+(i-1)*Width+j+1); 
			image[3][2]=(int) *(pDoc->m_Dib.m_Buffer1+(i+1)*Width+j); 
			image[3][1]=(int) *(pDoc->m_Dib.m_Buffer1+(i+1)*Width+j-1); 
			image[3][3]=(int) *(pDoc->m_Dib.m_Buffer1+(i+1)*Width+j+1); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j)=(image[1][1]+image[1][2]+image[1][3]+image[2][1]+image[2][2]+image[2][3]+image[3][1]+image[3][2]+image[3][3])/9; 
		} 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
/*void queue(int *im,int n) 
//int im[],n; 
{ 
	int i,j,medium; 
	for(i=1;i<n;i++) 
	{ 
		for(j=i+1;j<n+1;j++) 
		{ 
			if(im(j)>im(i)) 
			{ 
				medium=im(i); 
				im(i)=im(j); 
				im(j)=medium; 
			} 
		} 
	} 
}*/ 
 
void CBmpTestView::OnMidFilt()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	BYTE * m_Buffer1; 
	int i,Height,Width,i1,j1,j; 
	//filtering 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
	if(pDoc->m_Dib.BPP==8) 
	{ 
		int image[10],medium; 
		Height=(int) pDoc->m_Dib.Height; 
		Width=(int) pDoc->m_Dib.Bpl; 
 		for (i=1;i<Height-1;i++) 
		{ 
			for (j=1;j<Width +1;j++) 
			{ 
				image[5]=(int) *(m_Buffer1+i*Width+j); 
				image[4]=(int) *(m_Buffer1+i*Width+j-1); 
				image[6]=(int) *(m_Buffer1+i*Width+j+1); 
				image[2]=(int) *(m_Buffer1+(i-1)*Width+j); 
				image[1]=(int) *(m_Buffer1+(i-1)*Width+j-1); 
				image[3]=(int) *(m_Buffer1+(i-1)*Width+j+1); 
				image[8]=(int) *(m_Buffer1+(i+1)*Width+j); 
				image[7]=(int) *(m_Buffer1+(i+1)*Width+j-1); 
				image[9]=(int) *(m_Buffer1+(i+1)*Width+j+1); 
        		for(i1=1;i1<9;i1++) 
				{ 
		        	for(j1=i1+1;j1<10;j1++) 
					{ 
			    		if(image[j1]>image[i1]) 
						{ 
					    	medium=image[i1]; 
		        			image[i1]=image[j1]; 
			        		image[j1]=medium; 
						} 
					} 
				} 
				*(pDoc->m_Dib.m_Buffer+i*Width+j)=image[5]; 
			} 
		} 
	} 
	else 
	{ 
// 24位彩色中值滤波 
		int image[10][4],medium[4],k; 
		Height=(int) pDoc->m_Dib.Height; 
		Width=(int) pDoc->m_Dib.Bpl; 
 		for (i=1;i<Height-1;i++) 
		{ 
			for (j=1;j<(int)Width/3 ;j++) 
			{ 
				for(k=0;k<3;k++) 
				{ 
					image[5][k+1]=(int) *(m_Buffer1+i*Width+j*3+k); 
					image[4][k+1]=(int) *(m_Buffer1+i*Width+(j-1)*3+k); 
					image[6][k+1]=(int) *(m_Buffer1+i*Width+(j+1)*3+k); 
					image[2][k+1]=(int) *(m_Buffer1+(i-1)*Width+j*3+k); 
					image[1][k+1]=(int) *(m_Buffer1+(i-1)*Width+(j-1)*3+k); 
					image[3][k+1]=(int) *(m_Buffer1+(i-1)*Width+(j+1)*3+k); 
					image[8][k+1]=(int) *(m_Buffer1+(i+1)*Width+j*3+k); 
					image[7][k+1]=(int) *(m_Buffer1+(i+1)*Width+(j-1)*3+k); 
					image[9][k+1]=(int) *(m_Buffer1+(i+1)*Width+(j+1)*3+k); 
				} 
//冒泡排序 
				for(i1=1;i1<9;i1++) 
				{ 
		        	for(j1=i1+1;j1<10;j1++) 
					{ 
			    		if((image[j1][1]+image[j1][2]+image[j1][3])/3>(image[i1][1]+image[i1][2]+image[i1][3])/3) 
						{ 
							for(k=1;k<4;k++) 
							{ 
					    		medium[k]=image[i1][k]; 
								image[i1][k]=image[j1][k]; 
								image[j1][k]=medium[k]; 
							} 
						} 
					} 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=image[5][1]; 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=image[5][2]; 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=image[5][3]; 
				} 
			} 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
	 
} 
 
void CBmpTestView::OnEdgeRobert()  
{ 
	// TODO: Add your command handler code here 
	#include  <math.h> 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	if(pDoc->m_Dib.BPP>8) 
	{ 
		AfxMessageBox("该算子不能处理8位图像"); 
		return; 
	} 
	BYTE * m_Buffer1; 
	int i,j,image[10],Height,Width,a,b; 
	//filtering 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
 	for (i=1;i<Height-1;i++) 
	{ 
	    for (j=1;j<Width -1;j++) 
		{ 
			image[1]=(unsigned int) *(m_Buffer1+(i-1)*Width+j-1); 
			image[3]=(unsigned int) *(m_Buffer1+(i-1)*Width+j+1); 
			image[7]=(unsigned int) *(m_Buffer1+(i+1)*Width+j-1); 
			image[9]=(unsigned int) *(m_Buffer1+(i+1)*Width+j+1); 
            a=abs(image[1]-image[9]); 
			b=abs(image[3]-image[7]); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j)=((a>b)?a:b); 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnEdgeLaplace()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP>8) 
	{ 
		AfxMessageBox("该算子只能处理8位图像"); 
		return; 
	} 
	int i,j,image[10],Height,Width,a; 
	//filtering 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
//	Total=(int) pDoc->m_Dib.BufferSize; 
 	for (i=1;i<Height-1;i++) 
	{ 
	    for (j=1;j<Width +1;j++) 
		{ 
			image[5]=(unsigned int) *(pDoc->m_Dib.m_Buffer1+i*Width+j); 
			image[4]=(unsigned int) *(pDoc->m_Dib.m_Buffer1+i*Width+j-1); 
			image[6]=(unsigned int) *(pDoc->m_Dib.m_Buffer1+i*Width+j+1); 
			image[2]=(unsigned int) *(pDoc->m_Dib.m_Buffer1+(i-1)*Width+j); 
			image[1]=(unsigned int) *(pDoc->m_Dib.m_Buffer1+(i-1)*Width+j-1); 
			image[3]=(unsigned int) *(pDoc->m_Dib.m_Buffer1+(i-1)*Width+j+1); 
			image[8]=(unsigned int) *(pDoc->m_Dib.m_Buffer1+(i+1)*Width+j); 
			image[7]=(unsigned int) *(pDoc->m_Dib.m_Buffer1+(i+1)*Width+j-1); 
			image[9]=(unsigned int) *(pDoc->m_Dib.m_Buffer1+(i+1)*Width+j+1); 
			a=image[2]+image[4]+image[6]+image[8]-4*image[5]; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j)=((a>0)?a:0); 
		} 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnGauseFilt()  
{ 
	// TODO: Add your command handler code here 
 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP>8) 
	{ 
		AfxMessageBox("该算子只能处理8位图像"); 
		return; 
	} 
	int i,j,image[4][4],Height,Width; 
	double a0,a1,a2; 
	a0=1/sqrt(2*3.14); 
	a1=0.6065/sqrt(2*3.14); 
	a2=exp(-1)/sqrt(2*3.14); 
	//filtering 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
 	for (i=1;i<Height-1;i++) 
	{ 
	    for (j=1;j<Width +1;j++) 
		{ 
			image[2][2]=(int) *(pDoc->m_Dib.m_Buffer1+i*Width+j); 
			image[2][1]=(int) *(pDoc->m_Dib.m_Buffer1+i*Width+j-1); 
			image[2][3]=(int) *(pDoc->m_Dib.m_Buffer1+i*Width+j+1); 
			image[1][2]=(int) *(pDoc->m_Dib.m_Buffer1+(i-1)*Width+j); 
			image[1][1]=(int) *(pDoc->m_Dib.m_Buffer1+(i-1)*Width+j-1); 
			image[1][3]=(int) *(pDoc->m_Dib.m_Buffer1+(i-1)*Width+j+1); 
			image[3][2]=(int) *(pDoc->m_Dib.m_Buffer1+(i+1)*Width+j); 
			image[3][1]=(int) *(pDoc->m_Dib.m_Buffer1+(i+1)*Width+j-1); 
			image[3][3]=(int) *(pDoc->m_Dib.m_Buffer1+(i+1)*Width+j+1); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j)=(int)(image[1][1]*a2+image[1][2]*a1+image[1][3]*a2+image[2][1]*a1+image[2][2]*a0+image[2][3]*a1+image[3][1]*a2+image[3][2]*a1+image[3][3]*a2); 
		} 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
	 
} 
void CBmpTestView::OnEdgeMarr()  
{ 
	// TODO: Add your command handler code here 
	OnGauseFilt(); 
	OnEdgeLaplace(); 
} 
 
 
void CBmpTestView::OnStruAdd()  
{ 
	// 二值形态学:膨胀,结构和 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	int i,j,image[4][4],Height,Width,a,k; 
	//filtering 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	BYTE * m_Buffer1; 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
	m_Gradmax=0; 
    for ( i=0;i<256;i++) 
	{ 
	    m_Grade[i]=0; 
	} 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	    m_Grade[int(*(pDoc->m_Dib.m_Buffer+j))]++; 
	} 
	m_Gradmax=0; 
    for ( i=1;i<256;i++) 
	{ 
	   if (m_Grade[i]>m_Gradmax) 
	   { 
		  m_Gradmax=m_Grade[i]; 
		  k=i; 
	   } 
	} 
 	for (i=1;i<Height-1;i++) 
	{ 
	    for (j=1;j<Width +1;j++) 
		{ 
			image[2][2]=(unsigned int) *(m_Buffer1+i*Width+j); 
			image[2][1]=(unsigned int) *(m_Buffer1+i*Width+j-1); 
			image[2][3]=(unsigned int) *(m_Buffer1+i*Width+j+1); 
			image[1][2]=(unsigned int) *(m_Buffer1+(i-1)*Width+j); 
			image[1][1]=(unsigned int) *(m_Buffer1+(i-1)*Width+j-1); 
			image[1][3]=(unsigned int) *(m_Buffer1+(i-1)*Width+j+1); 
			image[3][2]=(unsigned int) *(m_Buffer1+(i+1)*Width+j); 
			image[3][1]=(unsigned int) *(m_Buffer1+(i+1)*Width+j-1); 
			image[3][3]=(unsigned int) *(m_Buffer1+(i+1)*Width+j+1); 
			a=image[1][1]||image[1][2]||image[1][3]||image[2][1]||image[2][2]||image[2][3]||image[3][1]||image[3][2]||image[3][3]; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j)=(a?k:0); 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnStruSub()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	int i,j,image[4][4],Height,Width,a,k; 
	//filtering 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
//如下代码可防止不同图像调色板的不同 
	BYTE * m_Buffer1; 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
	m_Gradmax=0; 
    for ( i=0;i<256;i++) 
	{ 
	    m_Grade[i]=0; 
	} 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	    m_Grade[int(*(pDoc->m_Dib.m_Buffer+j))]++; 
	} 
	m_Gradmax=0; 
    for ( i=1;i<256;i++) 
	{ 
	   if (m_Grade[i]>m_Gradmax) 
	   { 
		  m_Gradmax=m_Grade[i]; 
		  k=i; 
	   } 
	} 
 	for (i=1;i<Height-1;i++) 
	{ 
	    for (j=1;j<Width +1;j++) 
		{ 
			image[2][2]=(int) *(m_Buffer1+i*Width+j); 
			image[2][1]=(int) *(m_Buffer1+i*Width+j-1); 
			image[2][3]=(int) *(m_Buffer1+i*Width+j+1); 
			image[1][2]=(int) *(m_Buffer1+(i-1)*Width+j); 
			image[1][1]=(int) *(m_Buffer1+(i-1)*Width+j-1); 
			image[1][3]=(int) *(m_Buffer1+(i-1)*Width+j+1); 
			image[3][2]=(int) *(m_Buffer1+(i+1)*Width+j); 
			image[3][1]=(int) *(m_Buffer1+(i+1)*Width+j-1); 
			image[3][3]=(int) *(m_Buffer1+(i+1)*Width+j+1); 
			a=image[1][1]&&image[1][2]&&image[1][3]&&image[2][1]&&image[2][2]&&image[2][3]&&image[3][1]&&image[3][2]&&image[3][3]; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j)=(a?k:0); 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();		 
} 
 
void CBmpTestView::OnStruOpen()  
{ 
	// TODO: Add your command handler code here 
	OnStruSub(); 
	OnStruAdd(); 
} 
 
void CBmpTestView::OnStruClose()  
{ 
	// TODO: Add your command handler code here 
	OnStruAdd(); 
	OnStruSub();	 
} 
 
void CBmpTestView::OnEditBackup()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
    pDoc->m_Dib.restoreimage(); 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnEditBlue()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	if(pDoc->m_Dib.BPP<=8) 
	{ 
       AfxMessageBox( "此图像为256色图像,目前暂时不能分出独立通道"); 
	   return; 
	} 
	if(pDoc->m_Dib.GetBlueImg()) 
		Invalidate(); 
} 
 
void CBmpTestView::OnEditGreen()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP<=8) 
	{ 
       AfxMessageBox( "此图像为256色图像,目前暂时不能分出独立通道"); 
	   return; 
	} 
	if(pDoc->m_Dib.GetGreenImg()) 
		Invalidate();	 
} 
 
void CBmpTestView::OnEditRed()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP<=8) 
	{ 
       AfxMessageBox( "此图像为256色图像,目前暂时不能分出独立通道"); 
	   return; 
	} 
	if(pDoc->m_Dib.GetRedImg()) 
		Invalidate();	 
} 
 
void CBmpTestView::OnEdgeColorprewitt()  
{ 
	// TODO: 一种强有力的非线性算子,加强了纵向,但也造成横向断裂 
# include <math.h> 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP<=8) 
	{ 
       AfxMessageBox( "此图像为256色图像,目前该算子还不支持这种图像格式"); 
	   return; 
	} 
	int i,j,k,image[10][4],Height,Width; 
	unsigned int a; 
	BYTE * m_Buffer1;	 
	double e1,e2,e3; 
	//filtering 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
 	for (i=1;i<Height-1;i++) 
	{ 
	    for (j=1;j<(int)Width/3 -1;j++) 
		{ 
			for (k=0;k<3;k++) 
			{ 
				image[1][k+1]=(unsigned int) *(m_Buffer1+(i-1)*Width+(j-1)*3+k); 
				image[3][k+1]=(unsigned int) *(m_Buffer1+(i-1)*Width+(j+1)*3+k); 
				image[4][k+1]=(unsigned int) *(m_Buffer1+i*Width+(j-1)*3+k); 
				image[6][k+1]=(unsigned int) *(m_Buffer1+i*Width+(j+1)*3+k); 
				image[7][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+(j-1)*3+k); 
				image[9][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+(j+1)*3+k); 
			} 
            e1=sqrt(pow((image[1][1]-image[3][1]),2)+pow((image[1][2]-image[3][2]),2)+pow((image[1][3]-image[3][3]),2)); 
			e2=sqrt(pow((image[4][1]-image[6][1]),2)+pow((image[4][2]-image[6][2]),2)+pow((image[4][3]-image[6][3]),2)); 
			e3=sqrt(pow((image[7][1]-image[9][1]),2)+pow((image[7][2]-image[9][2]),2)+pow((image[7][3]-image[9][3]),2)); 
			a=(unsigned int)(e1+e2+e3); 
			a=((a>255)?255:a); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=a; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=a; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=a; 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnEdgeColorrobert()  
{ 
	// TODO: 一种强烈的非线性算子,加强了双45度方向,保持了字符笔划的完整性 
# include <math.h> 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP<=8) 
	{ 
       AfxMessageBox( "此图像为256色图像,目前该算子还不支持这种图像格式"); 
	   return; 
	} 
	int i,j,k,image[10][4],Height,Width; 
	unsigned int a; 
	BYTE * m_Buffer1; 
	double e2,e1; 
	//filtering 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
 	for (i=1;i<Height-1;i++) 
	{ 
	    for (j=1;j<(int)Width/3 -1;j++) 
		{ 
			for (k=0;k<3;k++) 
			{ 
				image[1][k+1]=(unsigned int) *(m_Buffer1+(i-1)*Width+(j-1)*3+k); 
				image[3][k+1]=(unsigned int) *(m_Buffer1+(i-1)*Width+(j+1)*3+k); 
				image[7][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+(j-1)*3+k); 
				image[9][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+(j+1)*3+k); 
			} 
            e1=pow((image[1][1]-image[9][1]),2)+pow((image[1][2]-image[9][2]),2)+pow((image[1][3]-image[9][3]),2); 
			e2=pow((image[3][1]-image[7][1]),2)+pow((image[3][2]-image[7][2]),2)+pow((image[3][3]-image[7][3]),2); 
			a=(unsigned int)sqrt(e1+e2); 
			a=((a>255)?255:a); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=a; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=a; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=a; 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
	 
} 
 
void CBmpTestView::OnMidFiltLine()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	BYTE * m_Buffer1; 
	int i,Height,Width,i1,j1,j; 
	//filtering 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
	if(pDoc->m_Dib.BPP==8) 
	{ 
		int image[6],medium; 
		Height=(int) pDoc->m_Dib.Height; 
		Width=(int) pDoc->m_Dib.Bpl; 
		for (j=0;j<Width +1;j++) 
		{ 
	 		for (i=2;i<Height-2;i++) 
			{ 
				image[3]=(int) *(m_Buffer1+i*Width+j); 
				image[2]=(int) *(m_Buffer1+(i-1)*Width+j); 
				image[4]=(int) *(m_Buffer1+(i+1)*Width+j); 
				image[1]=(int) *(m_Buffer1+(i-2)*Width+j); 
				image[5]=(int) *(m_Buffer1+(i+2)*Width+j); 
		//冒泡排序 
				for(i1=1;i1<5;i1++) 
				{ 
		        	for(j1=i1+1;j1<6;j1++) 
					{ 
			    		if(image[j1]>image[i1]) 
						{ 
					    	medium=image[i1]; 
		        			image[i1]=image[j1]; 
			        		image[j1]=medium; 
						} 
					} 
				} 
				*(pDoc->m_Dib.m_Buffer+i*Width+j)=image[3]; 
			} 
		} 
	} 
	else 
	{ 
// 24位彩色中值滤波 
		int image[6][4],medium[4],k; 
		Height=(int) pDoc->m_Dib.Height; 
		Width=(int) pDoc->m_Dib.Bpl; 
		for (j=0;j<(int)Width/3;j++) 
		{ 
	 		for (i=2;i<Height-2;i++) 
			{ 
				for(k=0;k<3;k++) 
				{ 
					image[3][k+1]=(int) *(m_Buffer1+i*Width+j*3+k); 
					image[2][k+1]=(int) *(m_Buffer1+(i-1)*Width+j*3+k); 
					image[4][k+1]=(int) *(m_Buffer1+(i+1)*Width+j*3+k); 
					image[1][k+1]=(int) *(m_Buffer1+(i-2)*Width+j*3+k); 
					image[5][k+1]=(int) *(m_Buffer1+(i+2)*Width+j*3+k); 
				} 
//冒泡排序 
				for(i1=1;i1<5;i1++) 
				{ 
		        	for(j1=i1+1;j1<6;j1++) 
					{ 
			    		if((image[j1][1]+image[j1][2]+image[j1][3])/3>(image[i1][1]+image[i1][2]+image[i1][3])/3) 
						{ 
							for(k=1;k<4;k++) 
							{ 
					    		medium[k]=image[i1][k]; 
								image[i1][k]=image[j1][k]; 
								image[j1][k]=medium[k]; 
							} 
						} 
					} 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=image[3][1]; 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=image[3][2]; 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=image[3][3]; 
				} 
			} 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnDensityScan()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
# include <math.h> 
	int i,j,Height,Width; 
	if(pDoc->m_Dib.BPP==8) 
		return; 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	int image[2048][4],b[2048],count; 
	double diff; 
//扫描从图像底部开始	 
	for (i=0;i<Height;i++) 
	{ 
		for(j=0;j<(int)Width/3;j++) 
		{ 
			image[j][1]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3); 
			image[j][2]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3+1); 
			image[j][3]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3+2); 
			if(image[j][1]<60) 
			{ 
				image[j][1]=0; 
				image[j][2]=0; 
				image[j][3]=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
			} 
		}  
		j=0; 
		count=0; 
		while (j<(int)Width/3) 
		{ 
//因每点三分量均相等,故可简化	diff=sqrt(pow((image[j][1]-image[j+3][1]),2)+pow((image[j][2]-image[j+3][2]),2)+pow((image[j][3]-image[j+3][3]),2)); 
			diff=abs(image[j][1]-image[j+1][1])*1.7; 
			b[j]=(((int)diff>150)?1:0); 
			count+=b[j]; 
			j++; 
		} 
//变化15次已是最高限了,再高就会使车牌出现空线 
		if (count<15)  
		{ 
    		for(j=0;j<(int)Width/3;j++) 
			{ 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
			} 
		} 
		else 
		{ 
//分区域扫描法 
			count=0; 
//第一节 
			for(j=0;j<(int)Width/45;j++) 
				count+=b[j]; 
//剩余部分的简化算法 
			do 
			{ 
			//变化6次是最佳经验值 
				if(count<4) 
				{ 
					*(pDoc->m_Dib.m_Buffer+i*Width+(j-(int)Width/45)*3)=0; 
					*(pDoc->m_Dib.m_Buffer+i*Width+(j-(int)Width/45)*3+1)=0; 
					*(pDoc->m_Dib.m_Buffer+i*Width+(j-(int)Width/45)*3+2)=0; 
				} 
				count=count+b[j]-b[j-(int)Width/45]; 
				j++; 
			}while(j<(int)Width/3); 
			for(j=j-(int)Width/45;j<(int)Width/3;j++) 
			{ 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
			}  
//进一步扫描,消除图像中的残留点 
			for(j=0;j<(int)Width/3;j++) 
			{ 
				image[j][1]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3); 
			}  
			j=0; 
			count=0; 
			while (j<(int)Width/3) 
			{ 
//因每点三分量均相等,故可简化	diff=sqrt(pow((image[j][1]-image[j+3][1]),2)+pow((image[j][2]-image[j+3][2]),2)+pow((image[j][3]-image[j+3][3]),2)); 
				diff=abs(image[j][1]-image[j+1][1])*1.7; 
				b[j]=(((int)diff>220)?1:0); 
				count+=b[j]; 
				j++; 
			} 
//变化3次已是最高限了,再高就会使车牌出现空线 
			if (count<8)  
			{ 
    			for(j=0;j<(int)Width/3;j++) 
				{ 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
				}  
			}  
		}  
	} 
 	for (i=Height-5;i<Height;i++) 
	{ 
	    for (j=0;j<(int)Width/3 ;j++) 
		{ 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
		} 
	}  
//反向扫描 
	for (i=0;i<Height;i++) 
	{ 
	    for (j=0;j<(int)Width/3 ;j++) 
		{ 
			image[j][1]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3); 
			image[j][2]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3+1); 
			image[j][3]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3+2); 
		}  
		j=(int)Width/3; 
		while (j>=1) 
		{ 
//因每点三分量均相等,故可简化	diff=sqrt(pow((image[j][1]-image[j+3][1]),2)+pow((image[j][2]-image[j+3][2]),2)+pow((image[j][3]-image[j+3][3]),2)); 
			diff=abs(image[j][1]-image[j-1][1])*1.7; 
			b[j]=(((int)diff>60)?1:0); 
			j--; 
		} 
//变化15次已是最高限了,再高就会使车牌出现空线 
//分区域扫描法 
		count=0; 
//第一节 
		for(j=(int)Width/3-1;j>=(int)Width/3-(int)Width/45;j--) 
				count+=b[j]; 
//剩余部分的简化算法 
		do 
		{ 
	//变化6次是最佳经验值 
			if(count<4) 
			{ 
				*(pDoc->m_Dib.m_Buffer+i*Width+(j+(int)Width/45)*3)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+(j+(int)Width/45)*3+1)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+(j+(int)Width/45)*3+2)=0; 
			} 
			count=count+b[j]-b[j+(int)Width/45]; 
			j--; 
		}while(j>=1);  
	}  
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnRowDilation()  
{ 
	// TODO: Add your command handler code here 
	// 形态学:横向膨胀,结构和,两边各扩展6个像素 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP==8) 
		return; 
	int i,j,i1,j1,Height,Width,a; 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	BYTE * m_Buffer1; 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
 	for (i=2;i<Height-3;i++) 
	{ 
	    for (j=4;j<(int)Width/3-5;j++) 
		{ 
			a=0; 
			for (i1=-2;i1<3;i1++) 
			{ 
				for(j1=-4;j1<5;j1++) 
				{ 
					if ((unsigned int) *(m_Buffer1+(i+i1)*Width+(j+j1)*3)>50) 
						a=1; 
				} 
			} 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=(a?255:0); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=(a?255:0); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=(a?255:0); 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnEdgeColorprewitt1()  
{ 
	// TODO: Add your command handler code here 
# include <math.h> 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP<=8) 
	{ 
       AfxMessageBox( "此图像为256色图像,目前该算子还不支持这种图像格式"); 
	   return; 
	} 
	int i,j,k,image[5][4],Height,Width; 
	unsigned int a; 
	BYTE * m_Buffer1;	 
	double e1,e2; 
	//filtering 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
 	for (i=0;i<Height-1;i++) 
	{ 
	    for (j=0;j<(int)Width/3 -1;j++) 
		{ 
			for (k=0;k<3;k++) 
			{ 
				image[1][k+1]=(unsigned int) *(m_Buffer1+i*Width+j*3+k); 
				image[2][k+1]=(unsigned int) *(m_Buffer1+i*Width+(j+1)*3+k); 
				image[3][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+j*3+k); 
				image[4][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+(j+1)*3+k); 
			} 
            e1=sqrt(pow((image[1][1]-image[2][1]),2)+pow((image[1][2]-image[2][2]),2)+pow((image[1][3]-image[2][3]),2)); 
			e2=sqrt(pow((image[3][1]-image[4][1]),2)+pow((image[3][2]-image[4][2]),2)+pow((image[3][3]-image[4][3]),2)); 
			a=(unsigned int)(e1+e2); 
			a=((a>255)?255:a); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=a; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=a; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=a; 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();		 
} 
 
void CBmpTestView::OnEdgeColorrobert1()  
{ 
	// TODO: Add your command handler code here 
	 
#include <math.h> 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP<=8) 
	{ 
       AfxMessageBox( "此图像为256色图像,目前该算子还不支持这种图像格式"); 
	   return; 
	} 
	int i,j,k,image[5][4],Height,Width; 
	unsigned int a; 
	BYTE * m_Buffer1; 
	double e2,e1; 
	//filtering 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
 	for (i=0;i<Height-1;i++) 
	{ 
	    for (j=0;j<(int)Width/3 -1;j++) 
		{ 
			for (k=0;k<3;k++) 
			{ 
				image[1][k+1]=(unsigned int) *(m_Buffer1+i*Width+j*3+k); 
				image[2][k+1]=(unsigned int) *(m_Buffer1+i*Width+(j+1)*3+k); 
				image[3][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+j*3+k); 
				image[4][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+(j+1)*3+k); 
			} 
            e1=sqrt(pow((image[1][1]-image[4][1]),2)+pow((image[1][2]-image[4][2]),2)+pow((image[1][3]-image[4][3]),2)); 
			e2=sqrt(pow((image[2][1]-image[3][1]),2)+pow((image[2][2]-image[3][2]),2)+pow((image[2][3]-image[3][3]),2)); 
			a=(unsigned int)(e1+e2); 
			a=((a>255)?255:a); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=a; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=a; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=a; 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnRowErosion()  
{ 
	// TODO: Add your command handler code here 
	// 形态学:横向收缩,结构差,两边各收缩4个像素 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP==8) 
		return; 
	int i,j,i1,j1,Height,Width,a; 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	BYTE * m_Buffer1; 
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize]; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j); 
	} 
 	for (i=2;i<Height-3;i++) 
	{ 
	    for (j=4;j<(int)Width/3-5;j++) 
		{ 
			a=0; 
			for (i1=-2;i1<3;i1++) 
			{ 
				for(j1=-4;j1<5;j1++) 
				{ 
					if ((unsigned int) *(m_Buffer1+(i+i1)*Width+(j+j1)*3)<50) 
						a=1; 
				} 
			} 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=(a?0:255); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=(a?0:255); 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=(a?0:255); 
		} 
	} 
	if(m_Buffer1) 
	{ 
		delete [] m_Buffer1; 
		m_Buffer1=0; 
	} 
	pDoc->m_BM=true; 
	Invalidate();		 
} 
 
void CBmpTestView::OnClose()  
{ 
	// TODO: Add your command handler code here 
	OnRowDilation(); 
	OnRowDilation(); 
	OnRowErosion(); 
//	OnRowDilation(); 
} 
 
void CBmpTestView::OnGet()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	if(pDoc->m_Dib.BPP==8) 
		return; 
	int j; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(pDoc->m_Dib.m_Buffer+j)=(*(pDoc->m_Dib.m_Buffer+j))&(*(pDoc->m_Dib.m_Buffer1+j)); 
	} 
	pDoc->m_BM=true; 
	Invalidate();			 
} 
 
void CBmpTestView::OnRelaScan()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
# include <math.h> 
	int i,j,i1,j1,Height,Width; 
//    char s[15]=""; 
	if(pDoc->m_Dib.BPP==8) 
		return; 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	int image[1024],count; 
	double diff; 
	CString strr,s; 
	LPCTSTR fn; 
	CFileDialog fd(FALSE, 
				   NULL, 
				   NULL, 
				   OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
				   "data files(*.dat)|*.dat|All files(*.*)|*.*||" 
				   ); 
	if(fd.DoModal()==IDOK) 
	{ 
		strr=fd.GetPathName(); 
		if(strr.Find(".dat")==-1) 
		{ 
			strr=strr+".dat"; 
		} 
	} 
	fn=strr.GetBuffer(100); 
	if(fn=="") 
	{ 
		return; 
	} 
	FILE * fp; 
	if((fp=fopen(fn,"wb"))==NULL) 
	{ 
		return; 
	} 
 
//扫描从图像底部开始	 
	for (i=0;i<Height;i++) 
	{ 
		for(j=0;j<(int)Width/3;j++) 
		{ 
			image[j]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3); 
			if(image[j]<60) 
			{ 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
			} 
		}  
		j=0; 
		count=0; 
		while (j<(int)Width/3) 
		{ 
//因每点三分量均相等,故可简化	diff=sqrt(pow((image[j][1]-image[j+3][1]),2)+pow((image[j][2]-image[j+3][2]),2)+pow((image[j][3]-image[j+3][3]),2)); 
			diff=abs(image[j]-image[j+1])*1.7; 
			b[i][j]=(((int)diff>150)?1:0); 
			count+=b[i][j]; 
			j++; 
		} 
//变化15次已是最高限了,再高就会使车牌出现空线 
		if (count<15)  
		{ 
    		for(j=0;j<(int)Width/3;j++) 
			{ 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
			} 
		} 
	}  
//将模板内计数并存入文件中以观查 
	for (i=(int)Width/144;i<Height-(int)Width/144;i++)  //车牌纵横比约为3:1 
	{ 
		for(j=(int)Width/48;j<(int)5*Width/16;j++)   //模板大小取车牌最小值,图像宽度的1/8 
		{ 
//		  str(b[i][j],s);   我的数字——>字符串函数,优点是速度快 
			count=0; 
			for(i1=i-(int)Width/144;i1<i+(int)Width/144;i1++) 
			{ 
				for(j1=j-(int)Width/48;j1<j+(int)Width/48;j1++) 
						count+=b[i1][j1]; 
			} 
			s.Format("%d",count); 
		    fprintf(fp,"%s ",s); 	 
		} 
        fprintf(fp,"\n"); 	 
	} 
	pDoc->m_BM=true; 
	Invalidate(); 
	fclose(fp); 
} 
 
 
 
void CBmpTestView::OnDensityScanMid()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
# include <math.h> 
	int i,j,Height,Width; 
	if(pDoc->m_Dib.BPP==8) 
		return; 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	int image[2048][4],b[2048],count; 
	double diff; 
//扫描从图像底部开始	 
	for (i=0;i<Height;i++) 
	{ 
		for(j=0;j<(int)Width/3;j++) 
		{ 
			image[j][1]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3); 
			image[j][2]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3+1); 
			image[j][3]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3+2); 
			if(image[j][1]<60) 
			{ 
				image[j][1]=0; 
				image[j][2]=0; 
				image[j][3]=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
			} 
		}  
		j=0; 
		count=0; 
		while (j<(int)Width/3) 
		{ 
//因每点三分量均相等,故可简化	diff=sqrt(pow((image[j][1]-image[j+3][1]),2)+pow((image[j][2]-image[j+3][2]),2)+pow((image[j][3]-image[j+3][3]),2)); 
			diff=abs(image[j][1]-image[j+1][1])*1.7; 
			b[j]=(((int)diff>150)?1:0); 
			count+=b[j]; 
			j++; 
		} 
//变化15次已是最高限了,再高就会使车牌出现空线 
		if (count<15)  
		{ 
    		for(j=0;j<(int)Width/3;j++) 
			{ 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
			} 
		} 
		else 
		{ 
//分区域扫描法 
			count=0; 
//第一节 
			for(j=0;j<(int)Width/45;j++) 
			{ 
				count+=b[j]; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
			} 
//剩余部分的简化算法 
			do 
			{ 
			//变化6次是最佳经验值 
				if(count<5) 
				{ 
					*(pDoc->m_Dib.m_Buffer+i*Width+(j-(int)Width/90)*3)=0; 
					*(pDoc->m_Dib.m_Buffer+i*Width+(j-(int)Width/90)*3+1)=0; 
					*(pDoc->m_Dib.m_Buffer+i*Width+(j-(int)Width/90)*3+2)=0; 
				} 
				count=count+b[j]-b[j-(int)Width/45]; 
				j++; 
			}while(j<(int)Width/3); 
			for(j=j-(int)Width/45;j<(int)Width/3;j++) 
			{ 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
			}  
//进一步扫描,消除图像中的残留点 
			for(j=0;j<(int)Width/3;j++) 
			{ 
				image[j][1]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3); 
			}  
			j=0; 
			count=0; 
			while (j<(int)Width/3) 
			{ 
//因每点三分量均相等,故可简化	diff=sqrt(pow((image[j][1]-image[j+3][1]),2)+pow((image[j][2]-image[j+3][2]),2)+pow((image[j][3]-image[j+3][3]),2)); 
				diff=abs(image[j][1]-image[j+1][1])*1.7; 
				b[j]=(((int)diff>150)?1:0); 
				count+=b[j]; 
				j++; 
			} 
//变化3次已是最高限了,再高就会使车牌出现空线 
			if (count<6)  
			{ 
    			for(j=0;j<(int)Width/3;j++) 
				{ 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
				}  
			}  
		}  
	} 
 	for (i=Height-5;i<Height;i++) 
	{ 
	    for (j=0;j<(int)Width/3 ;j++) 
		{ 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0; 
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0; 
		} 
	}  
	pDoc->m_BM=true; 
	Invalidate();	 
	 
} 
 
 
void rgbToHSV(float r,float g,float b,float *h,float *s,float *v) 
{ 
	float max=MAX(r,MAX(g,b)),min=MIN(r,MIN(g,b)); 
	float delta=max-min; 
	*v=max; 
	if(max!=0.0) *s=delta/max; 
	else *s=0.0; 
	if(*s==0.0)	*h=NO_HUE 
	else 
	{		 
		if(r==max) *h=(g-b)/delta; 
		else if(g==max) *h=2+(b-r)/delta; 
		else  *h=4+(r-g)/delta; 
		*h=*h*60; 
		if(*h<0) *h=*h+360; 
	} 
} 
 
 
void CBmpTestView::OnAreaMark()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	HWND      hWnd; 
	int left[100],right[100],top[100],bottom[100],*pIntensity,*pSaturation,*pHue,SaturationI,HueI,IntensityI,i,j,k,premark,min,mark,Blue,Red,Green,markmax,Height,Width,Wid,Hei,*pHeight,*pWidth,*Widmax,*Heimax,CountofBlue,CountofGreen,CountofRed,maxofIntensity,maxofHue,maxofSat,max; 
	bool begin; 
	float Saturation,Hue,Intensity,	ExpofIntensity,SigmaofIntensity,ExpofHue,SigmaofHue,ExpofSat,SigmaofSat; 
	float T0ofInt,T0,u1,u2,T1,w1,w2,u,sig1,sig2,u1ofInt,sig1ofInt,u2ofInt,sig2ofInt,distance,simulate; 
	CString s; 
	POINT  CurP,NeighborP,SeedP; 
	BYTE *lpTempPtr,*lpTempPtr1; 
	MYSTACK SeedFillStack; 
	if(pDoc->m_Dib.BPP<16) 
		return; 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	//建立堆栈,大小与图像相同 
	SeedFillStack.InitStack(hWnd,Height*Width); 
	mark=0; 
//初始化与标号 
	for (i=0;i<Height;i++) 
	{ 
		for(j=0;j<(int)Width/3;j++) 
		{ 
			b[i][j]=0; 
		} 
	} 
	for (i=0;i<Height;i++) 
	{ 
		for(j=0;j<(int)Width/3;j++) 
		{ 
	//种子法和迷宫法并求出每个区域的最左,最右,最上,最下 
			if((unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3)!=0 && b[i][j]==0) 
			{ 
				mark++; 
				left[mark]=(int)Width/3-1;right[mark]=0;bottom[mark]=Height-1;top[mark]=0; 
				SeedP.x=j; 
				SeedP.y=i; 
				SeedFillStack.MyPush(SeedP); 
				while(!SeedFillStack.IsStackEmpty()) 
				{ 
					CurP=SeedFillStack.MyPop(); 
					b[CurP.y][CurP.x]=mark; 
					if(CurP.y>top[mark]) 
						top[mark]=CurP.y; 
					if(CurP.y<bottom[mark]) 
						bottom[mark]=CurP.y; 
					if(CurP.x>right[mark]) 
						right[mark]=CurP.x; 
					if(CurP.x<left[mark]) 
						left[mark]=CurP.x; 
					lpTempPtr=pDoc->m_Dib.m_Buffer+CurP.y*Width+CurP.x*3; 
					*lpTempPtr=255; 
					*(lpTempPtr+1)=255; 
					*(lpTempPtr+2)=255; 
	//标记颜色 
/*					switch(mark%4) 
					{ 
					case 0: 
						*lpTempPtr=255; 
						*(lpTempPtr+1)=0; 
						*(lpTempPtr+2)=0; 
						break; 
					case 1: 
						*lpTempPtr=0; 
						*(lpTempPtr+1)=255; 
						*(lpTempPtr+2)=0; 
						break; 
					case 2: 
						*lpTempPtr=0; 
						*(lpTempPtr+1)=0; 
						*(lpTempPtr+2)=255; 
						break; 
					case 3: 
						*lpTempPtr=127; 
						*(lpTempPtr+1)=127; 
						*(lpTempPtr+2)=127; 
						break; 
					} */ 
			//left neighbour 
					if(CurP.x>0 ) 
					{ 
						NeighborP.x=CurP.x-1; 
						NeighborP.y=CurP.y; 
						lpTempPtr1=lpTempPtr-3; 
						if(*lpTempPtr1!=0 && b[NeighborP.y][NeighborP.x]==0) 
							SeedFillStack.MyPush(NeighborP); 
					} 
		//down neighbour 
					if(CurP.y>0) 
					{ 
						NeighborP.x=CurP.x; 
						NeighborP.y=CurP.y-1; 
						lpTempPtr1=lpTempPtr-Width; 
						if(*lpTempPtr1!=0 && b[NeighborP.y][NeighborP.x]==0) 
							SeedFillStack.MyPush(NeighborP); 
					} 
		//right neighbour 
					if(CurP.x<(int)Width/3-1) 
					{ 
						NeighborP.x=CurP.x+1; 
						NeighborP.y=CurP.y; 
						lpTempPtr1=lpTempPtr+3; 
						if(*lpTempPtr1!=0 && b[NeighborP.y][NeighborP.x]==0) 
							SeedFillStack.MyPush(NeighborP); 
					} 
		//up neighbour 
					if(CurP.y<Height-1) 
					{ 
						NeighborP.x=CurP.x; 
						NeighborP.y=CurP.y+1; 
						lpTempPtr1=lpTempPtr+Width; 
						if(*lpTempPtr1!=0 && b[NeighborP.y][NeighborP.x]==0) 
							SeedFillStack.MyPush(NeighborP); 
					} 
				} 
			} 
		}  //列结束 
	} //行结束 
	SeedFillStack.DeInitStack(); 
//计算每一区域的平均高度和宽度及区域面积 
	markmax=mark+1; 
	pWidth=new int[markmax]; 
	pHeight=new int[markmax]; 
	Heimax=new int[markmax]; 
	Widmax=new int[markmax]; 
	pIntensity=new int[256*markmax]; 
	pSaturation=new int[256*markmax]; 
	pHue=new int[256*markmax]; 
//先计算各个区域的平均宽度 
	for(i=0;i<markmax;i++) 
	{ 
		pWidth[i]=0; 
		pHeight[i]=0; 
		Heimax[i]=0; 
		Widmax[i]=0; 
		for (j=0;j<256;j++) 
		{ 
			pIntensity[j+i*256]=0; 
			pSaturation[j+i*256]=0; 
			pHue[j+i*256]=0; 
		} 
	} 
	for (i=0;i<Height;i++) 
	{ 
		Wid=0; 
		premark=0; 
		for(j=0;j<(int)Width/3;j++) 
		{ 
			mark=b[i][j]; 
			if(mark!=0) 
			{ 
				if(mark!=premark) 
				{ 
					pWidth[mark]++; 
					Wid++; 
					premark=mark; 
				} 
				else 
				{ 
					pWidth[mark]++; 
					Wid++; 
				} 
			} 
			else 
			{ 
				if((Wid>Widmax[premark])&&(premark!=0)) 
					Widmax[premark]=Wid; 
				Wid=0; 
				premark=0; 
			}  
		} 
	}  
//计算各区域的平均高度 
	for(j=0;j<(int)Width/3;j++) 
	{ 
		Hei=0; 
		premark=0; 
		for (i=0;i<Height;i++) 
		{ 
			mark=b[i][j]; 
			if(mark!=0) 
			{ 
				if(mark!=premark) 
				{ 
					pHeight[mark]++; 
					Hei++; 
					premark=mark; 
				} 
				else 
				{ 
					pHeight[mark]++; 
					Hei++; 
				} 
			} 
			else 
			{ 
				if((Hei>Heimax[premark])&&(premark!=0)) 
					Heimax[premark]=Hei; 
				Hei=0; 
				premark=0; 
			}  
		} 
	}  
//删除无用的区域 
	for(i=1;i<markmax;i++) 
	{ 
//求各区域平均宽度及平均高度 
		pWidth[i]/=Heimax[i]; 
		pHeight[i]/=Widmax[i]; 
		if(pWidth[i]>Width/9||pWidth[i]<Width/30) 
			Widmax[i]=0; 
		else 
		{ 
			if(abs(pWidth[i]/pHeight[i]-2.5)>2) 
				Widmax[i]=0; 
		} 
	} 
	for (i=0;i<Height;i++) 
	{ 
		for(j=0;j<(int)Width/3;j++) 
		{ 
			mark=b[i][j]; 
			if(mark!=0) 
			{ 
				if(Widmax[mark]==0) 
				{ 
					lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+j*3; 
					*lpTempPtr=0; 
					*(lpTempPtr+1)=0; 
					*(lpTempPtr+2)=0; 
					b[i][j]=0; 
				} 
				else 
				{ 
					lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+j*3; 
					*lpTempPtr=255; 
					*(lpTempPtr+1)=255; 
					*(lpTempPtr+2)=255; 
				} 
			} 
		} 
	} 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(pDoc->m_Dib.m_Buffer+j)=(*(pDoc->m_Dib.m_Buffer+j))&(*(pDoc->m_Dib.m_Buffer1+j)); 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
	AfxMessageBox("下面是光强、色度、色饱和度直方图显示!"); 
	m_Gradmax=0; 
 	for (i=0;i<Height;i++) 
	{ 
		for(j=0;j<(int)Width/3;j++) 
		{ 
			mark=b[i][j]; 
			if(mark!=0) 
			{ 
				lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+j*3; 
			    Blue=(int) *lpTempPtr; 
				Green=(int) *(lpTempPtr+1); 
				Red=(int) *(lpTempPtr+2); 
				rgbToHSV(Red,Green,Blue,&Hue,&Saturation,&Intensity); 
//如下是三个量的量化过程 
				HueI=(int) (Hue*255/360); 
				SaturationI=(int) (Saturation*255); 
				IntensityI= (int) Intensity; 
// 直方图统计过程 
				pIntensity[IntensityI+mark*256]++; 
				pHue[HueI+mark*256]++; 
				pSaturation[SaturationI+mark*256]++;  
			} 
		} 
	} 
	for(j=0;j<markmax;j++) 
	{ 
		m_Gradmax=0; 
		CountofBlue=0; 
		CountofGreen=0; 
		CountofRed=0; 
	    for ( i=0;i<256;i++) 
		{ 
			m_Grade_Blue[i]=pIntensity[i+j*256]; 
			m_Grade_Green[i]=pHue[i+j*256]; 
			m_Grade_Red[i]=pSaturation[i+j*256]; 
			if (m_Grade_Blue[i]>m_Gradmax) 
				m_Gradmax=m_Grade_Blue[i]; 
			if (m_Grade_Green[i]>m_Gradmax) 
				m_Gradmax=m_Grade_Green[i]; 
			if (m_Grade_Red[i]>m_Gradmax) 
				m_Gradmax=m_Grade_Red[i]; 
			CountofBlue=CountofBlue+m_Grade_Blue[i]; 
			CountofGreen=CountofGreen+m_Grade_Green[i]; 
			CountofRed=CountofRed+m_Grade_Red[i];		 
		} 
 
		if (m_Gradmax>0) 
		{ 
			pDoc->m_BM=false; 
			Invalidate(); 
			ExpofIntensity=0; 
			ExpofHue=0; 
			SigmaofIntensity=0; 
			SigmaofHue=0; 
			ExpofSat=0;SigmaofSat=0; 
// Calculate estimate and maxpoint of Intensity and Hue 
			max=0;min=0; 
		    for ( i=0;i<256;i++) 
			{ 
				if(max<m_Grade_Blue[i]) 
				{ 
					max=m_Grade_Blue[i]; 
					maxofIntensity=i; 
				} 
				if(min<m_Grade_Red[i]) 
				{ 
					min=m_Grade_Red[i]; 
					maxofSat=i; 
				} 
				ExpofIntensity=ExpofIntensity+(float) i*m_Grade_Blue[i]/CountofBlue; 
				ExpofHue=ExpofHue+(float) i*m_Grade_Green[i]/CountofGreen; 
				ExpofSat=ExpofSat+(float) i*m_Grade_Red[i]/CountofRed; 
			} 
			max=0; 
			for ( i=0;i<256;i++) 
			{ 
				if(max<m_Grade_Green[i]) 
				{ 
					max=m_Grade_Green[i]; 
					maxofHue=i; 
				} 
				SigmaofIntensity=SigmaofIntensity+(float) (i-ExpofIntensity)*(i-ExpofIntensity)*m_Grade_Blue[i]/CountofBlue; 
				SigmaofHue=SigmaofHue+(float) (i-ExpofHue)*(i-ExpofHue)*m_Grade_Green[i]/CountofGreen; 
				SigmaofSat=SigmaofSat+(float) (i-ExpofSat)*(i-ExpofSat)*m_Grade_Red[i]/CountofRed; 
			} 
			SigmaofIntensity=sqrt(SigmaofIntensity); 
			SigmaofHue=sqrt(SigmaofHue); 
			SigmaofSat=sqrt(SigmaofSat); 
//estimate if there is dual peak of Intensity 
			T0=ExpofIntensity;T1=0; 
			while (fabs(T0-T1)>0.01) 
			{ 
				w1=0;w2=0;u1=0;u2=0;sig1=0;sig2=0; 
				for(i=0;i<=int(T0);i++) 
				{ 
					w1=w1+(float)m_Grade_Blue[i]/CountofBlue; 
					u1=u1+(float)i*m_Grade_Blue[i]/CountofBlue; 
				} 
				u1=u1/w1; 
				for(i=0;i<=int(T0);i++) 
				{ 
					sig1=sig1+(float)(i-u1)*(i-u1)*m_Grade_Blue[i]/CountofBlue; 
				} 
				for(i=int(T0)+1;i<256;i++) 
				{ 
					w2=w2+(float)m_Grade_Blue[i]/CountofBlue; 
					u2=u2+(float)i*m_Grade_Blue[i]/CountofBlue; 
				} 
				u2=u2/w2; 
				for(i=int(T0)+1;i<256;i++) 
				{ 
					sig2=sig2+(float)(i-u2)*(i-u2)*m_Grade_Blue[i]/CountofBlue; 
				} 
				T1=T0;T0=0.5*(u1+u2);sig1=sqrt(sig1);sig2=sqrt(sig2); 
			} 
			T0ofInt=T0;u1ofInt=u1;sig1ofInt=sig1;u2ofInt=u2;sig2ofInt=sig2; 
//estimate if there is dual peak of Hue 
			T0=ExpofHue;T1=0; 
			while (fabs(T0-T1)>0.01) 
			{ 
				w1=0;w2=0;u1=0;u2=0;sig1=0;sig2=0; 
				for(i=0;i<=int(T0);i++) 
				{ 
					w1=w1+(float)m_Grade_Green[i]/CountofGreen; 
					u1=u1+(float)i*m_Grade_Green[i]/CountofGreen; 
				} 
				u1=u1/w1; 
				for(i=0;i<=int(T0);i++) 
				{ 
					sig1=sig1+(float)(i-u1)*(i-u1)*m_Grade_Green[i]/CountofGreen; 
				} 
				for(i=int(T0)+1;i<256;i++) 
				{ 
					w2=w2+(float)m_Grade_Green[i]/CountofGreen; 
					u2=u2+(float)i*m_Grade_Green[i]/CountofGreen; 
				} 
				u2=u2/w2; 
				for(i=int(T0)+1;i<256;i++) 
				{ 
					sig2=sig2+(float)(i-u2)*(i-u2)*m_Grade_Green[i]/CountofGreen; 
				} 
				T1=T0;T0=0.5*(u1+u2);sig1=sqrt(sig1);sig2=sqrt(sig2); 
			} 
			s.Format("ExpofIntensity=%f,SigmaofIntensity=%f,T0ofIntensity=%f,u1ofInt=%f,sig1ofInt=%f,u2ofInt=%f,sig2ofInt=%f,!!!!!!,ExpofHue=%f,SigmaofHue=%f,T0=%f,u1=%f,sig1=%f,u2=%f,sig2=%f,%d,%d",ExpofIntensity,SigmaofIntensity,T0ofInt,u1ofInt,sig1ofInt,u2ofInt,sig2ofInt,ExpofHue,SigmaofHue,T0,u1,sig1,u2,sig2,maxofIntensity,maxofHue); 
			AfxMessageBox(s); 
// judge if Intensity is dual peak or single peak 
			if (abs(ExpofIntensity-maxofIntensity)>15) 
// dual peak 
            { 
				if (abs(u1ofInt-maxofIntensity)<15) 
				{ 
					ExpofIntensity=u1ofInt; 
					SigmaofIntensity=sig1ofInt; 
				} 
				else 
				{ 
					if (abs(u2ofInt-maxofIntensity)<15) 
					{ 
						ExpofIntensity=u2ofInt; 
						SigmaofIntensity=sig2ofInt; 
					} 
//					else 
//						ExpofIntensity=-1; 
				} 
			} 
// judge if Hue is dual peak or single peak 
			if (abs(ExpofHue-maxofHue)>15) 
// dual peak 
            { 
				if (abs(u1-maxofHue)<15) 
				{ 
					ExpofHue=u1; 
					SigmaofHue=sig1; 
				} 
				else 
				{ 
					if (abs(u2-maxofHue)<15) 
					{ 
						ExpofHue=u2; 
						SigmaofHue=sig2; 
					} 
					else 
						ExpofHue=-1; 
				} 
			} 
			s.Format("ExpofIntensity=%f,SigmaofIntensity=%f,ExpofHue=%f,SigmaofHue=%f,ExpofSat=%f,SigmaofSat=%f",ExpofIntensity,SigmaofIntensity,ExpofHue,SigmaofHue,ExpofSat,SigmaofSat); 
			AfxMessageBox(s); 
// estimate the color of vehicle plate 
			//deal with blue 
			begin=false; 
			if(abs(ExpofHue-150)<19||(ExpofHue==-1&&abs(maxofHue-140)<9)) 
			{ 
				if((ExpofIntensity>60)&&(ExpofIntensity<242)) 
				{ 
					s.Format("车牌为兰色"); 
					begin=true; 
				} 
				else 
				{ 
					if(ExpofIntensity<=60) 
					{ 
						s.Format("车牌为黑色"); 
						begin=true; 
					} 
					else 
					{ 
						s.Format("无用区域,删除"); 
						for (i=0;i<Height;i++) 
						{ 
							for(k=0;k<(int)Width/3;k++) 
							{ 
								mark=b[i][k]; 
								if(mark==j) 
								{ 
									lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+k*3; 
									*lpTempPtr=0; 
									*(lpTempPtr+1)=0; 
									*(lpTempPtr+2)=0; 
									b[i][k]=0; 
								} 
							} 
						} 
					} 
				} 
			} 
			else 
			{ //deal with yellow 
				if(abs(ExpofHue-33)<5||(ExpofHue==-1&&abs(maxofHue-33)<5)) 
				{ 
					if ((ExpofIntensity>60)&&(ExpofIntensity<242)) 
					{ 
						s.Format("车牌为黄色"); 
						begin=true; 
					} 
				} 
				else 
				{ 
					if(ExpofIntensity<=60&&ExpofIntensity>=0) 
					{ 
						s.Format("车牌为黑色"); 
						begin=true; 
					} 
					else 
					{ 
						if(ExpofIntensity>242||(ExpofIntensity==-1&&maxofIntensity>242)) 
						{ 
							s.Format("车牌为白色"); 
							begin=true; 
						} 
						else 
						{ 
							s.Format("无用区域,删除"); 
							for (i=0;i<Height;i++) 
							{ 
								for(k=0;k<(int)Width/3;k++) 
								{ 
									mark=b[i][k]; 
									if(mark==j) 
									{ 
										lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+k*3; 
										*lpTempPtr=0; 
										*(lpTempPtr+1)=0; 
										*(lpTempPtr+2)=0; 
										b[i][k]=0; 
									} 
								} 
							} 
						} 
					} 
				} 
			} 
			AfxMessageBox(s); 
			if(begin) 
			{ 
			 	for (i=bottom[j]-3;i<=top[j]+3;i++) 
				{ 
					for(k=left[j];k<=right[j];k++) 
					{ 
						lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+k*3; 
						*lpTempPtr=255; 
						*(lpTempPtr+1)=255; 
						*(lpTempPtr+2)=255; 
					} 
				} 
//			s.Format("left=%d,right=%d,bottom=%d,top=%d",left[j],right[j],bottom[j],top[j]); 
//			AfxMessageBox(s); 
			 	for (i=bottom[j]-3;i<=top[j]+3;i++) 
				{ 
					for(k=left[j];k<=right[j];k++) 
					{ 
						lpTempPtr=pDoc->m_Dib.m_Buffer1+i*Width+k*3; 
					    Blue=(int) *lpTempPtr; 
						Green=(int) *(lpTempPtr+1); 
						Red=(int) *(lpTempPtr+2); 
						rgbToHSV(Red,Green,Blue,&Hue,&Saturation,&Intensity); 
//如下是三个量的量化过程 
						HueI=(int) (Hue*255/360); 
						SaturationI=(int) (Saturation*255); 
						IntensityI= (int) Intensity; 
						lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+k*3; 
						distance=sqrt(pow(SaturationI*cos(HueI*2*pi/255)/255-ExpofSat*cos(ExpofHue*2*pi/255)/255,2)+pow(SaturationI*sin(HueI*2*pi/255)/255-ExpofSat*sin(ExpofHue*2*pi/255)/255,2)+pow((IntensityI-ExpofIntensity)/255,2)); 
						simulate=1-distance/sqrt(5); 
//						s.Format("simulate=%f",simulate); 
//						AfxMessageBox(s); 
						if(simulate>0.85) 
						{ 
							*lpTempPtr=255; 
							*(lpTempPtr+1)=255; 
							*(lpTempPtr+2)=255; 
						} 
						else 
						{ 
							*lpTempPtr=0; 
							*(lpTempPtr+1)=0; 
							*(lpTempPtr+2)=0; 
						} 
					} 
				} 
			} 
		} 
	} 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++) 
	{ 
	   	*(pDoc->m_Dib.m_Buffer+j)=(*(pDoc->m_Dib.m_Buffer+j))&(*(pDoc->m_Dib.m_Buffer1+j)); 
	} 
	delete []pWidth; 
	delete []pHeight; 
	delete []Widmax; 
	delete []Heimax; 
	delete []pIntensity; 
	delete []pSaturation; 
	delete []pHue; 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnLocating()  
{ 
	// TODO: Add your command handler code here 
		OnEdgeColorprewitt1(); 
		OnDensityScanMid(); 
		OnMidFiltLine(); 
		OnClose(); 
		OnAreaMark(); 
} 
 
void CBmpTestView::OnVehicleBitmap()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	int i,j,Height,Width,th; 
	long s; 
	if(!pDoc->m_Dib.m_BmpLoaded) 
	{ 
		AfxMessageBox("还没有打开图像文件"); 
		return; 
	} 
	if(pDoc->m_Dib.BPP==8) 
		return; 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
	s=0; 
    for ( i=0;i<256;i++) 
	{ 
		m_Grade_Blue[i]=0; 
	} 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j=j+3) 
	{ 
	    m_Grade_Blue[int(*(pDoc->m_Dib.m_Buffer+j))]++; 
	} 
	i=255; 
	do { 
		s+=m_Grade_Blue[i]; 
		i--; 
	}while(s<(int) pDoc->m_Dib.BufferSize/120); 
	th=i; 
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j=j+3) 
	{ 
	    if(*(pDoc->m_Dib.m_Buffer+j)>th) 
		{ 
//			*(pDoc->m_Dib.m_Buffer+j)=255; 
//			*(pDoc->m_Dib.m_Buffer+j+1)=255; 
//			*(pDoc->m_Dib.m_Buffer+j+2)=255; 
		} 
		else 
		{ 
			*(pDoc->m_Dib.m_Buffer+j)=0; 
			*(pDoc->m_Dib.m_Buffer+j+1)=0; 
			*(pDoc->m_Dib.m_Buffer+j+2)=0; 
		} 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
} 
 
void CBmpTestView::OnBlueCut()  
{ 
	// TODO: Add your command handler code here 
	CBmpTestDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc);   
	int i,j,k,Blue,Red,Green,Height,Width; 
	float Saturation,Hue,Intensity; 
	CString s; 
	BYTE *lpTempPtr,*lpTempPtr1; 
	if(pDoc->m_Dib.BPP<16) 
		return; 
	Height=(int) pDoc->m_Dib.Height; 
	Width=(int) pDoc->m_Dib.Bpl; 
 	for (i=0;i<Height;i++) 
	{ 
		for(j=0;j<(int)Width/3;j++) 
		{ 
			lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+j*3; 
		    Blue=(int) *lpTempPtr; 
			Green=(int) *(lpTempPtr+1); 
			Red=(int) *(lpTempPtr+2); 
			rgbToHSV(Red,Green,Blue,&Hue,&Saturation,&Intensity); 
			if((Hue>240)||(Hue<200)) 
			{ 
				*lpTempPtr=0; 
				*(lpTempPtr+1)=0; 
				*(lpTempPtr+2)=0; 
			} 
		} 
	} 
	pDoc->m_BM=true; 
	Invalidate();	 
}