⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mssb03view.cpp

📁 &#61548 1、有9个工件图像
💻 CPP
字号:
// MSSB03View.cpp : implementation of the CMSSB03View class
//

#include "stdafx.h"
#include "MSSB03.h"

#include "MSSB03Doc.h"
#include "MSSB03View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMSSB03View

IMPLEMENT_DYNCREATE(CMSSB03View, CFormView)

BEGIN_MESSAGE_MAP(CMSSB03View, CFormView)
	//{{AFX_MSG_MAP(CMSSB03View)
	ON_BN_CLICKED(IDC_BUTTON_bianyuan, OnBUTTONbianyuan)
	ON_BN_CLICKED(IDC_BUTTON_tu2ma, OnBUTTONtu2ma)
	ON_BN_CLICKED(IDC_BUTTON_ma2tu, OnBUTTONma2tu)
	ON_BN_CLICKED(IDC_BUTTON_tezhengtiqu, OnBUTTONtezhengtiqu)
	ON_BN_CLICKED(IDC_BUTTON_gongjian, OnBUTTONgongjian)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMSSB03View construction/destruction

CMSSB03View::CMSSB03View()
	: CFormView(CMSSB03View::IDD)
{
	//{{AFX_DATA_INIT(CMSSB03View)
	m_girth = 0.0f;
	m_area = 0.0f;
	m_dens = 0.0f;
	m_cg = _T("");
	m_gray = 0.0f;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CMSSB03View::~CMSSB03View()
{
}

void CMSSB03View::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMSSB03View)
	DDX_Text(pDX, IDC_EDIT2, m_girth);
	DDX_Text(pDX, IDC_EDIT3, m_area);
	DDX_Text(pDX, IDC_EDIT4, m_dens);
	DDX_Text(pDX, IDC_EDIT6, m_cg);
	DDX_Text(pDX, IDC_EDIT5, m_gray);
	//}}AFX_DATA_MAP
}

BOOL CMSSB03View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CMSSB03View::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CMSSB03View printing

BOOL CMSSB03View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMSSB03View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMSSB03View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CMSSB03View::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CMSSB03View diagnostics

#ifdef _DEBUG
void CMSSB03View::AssertValid() const
{
	CFormView::AssertValid();
}

void CMSSB03View::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CMSSB03Doc* CMSSB03View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMSSB03Doc)));
	return (CMSSB03Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMSSB03View message handlers

void CMSSB03View::OnBUTTONbianyuan() 
{
	// TODO: Add your control notification handler code here
	CClientDC dc(this);
	CRect clientRect;
	GetClientRect(&clientRect);
	OnPrepareDC(&dc);
	dc.DPtoLP(&clientRect);
	CMSSB03Doc * pDoc = GetDocument();
	COLORREF c;
	BYTE r, g, b;
	double grayValue;
	int x, y, z;
	for(x = 0; x<pDoc->fX; x++)
	{
		for(y = 0; y<pDoc->fY; y++)
		{
			c = dc.GetPixel(x,y);
			r = GetRValue(c);
			g = GetGValue(c);
			b = GetBValue(c);
			grayValue = (r+g+b)/3;
			if(grayValue>=100)
			{
				//dc.SetPixel(x+(int)pDoc->fX,y,RGB(255, 255, 255));
				color[x][y]=255;
			}
			else
			{
				//dc.SetPixel(x+(int)pDoc->fX,y,RGB(0, 0, 0));
				color[x][y]=0;
			}
		}
	}
	N = 0;
	for(x = 0; x<pDoc->fX; x++)
	{
		for(y = 0; y<pDoc->fY; y++)
		{
//拉普拉斯算子
			if(x==0||y==0||x==(pDoc->fX-1)||y==(pDoc->fY-1))
				//dc.SetPixel(x+pDoc->fX,y,RGB(color[x][y], color[x][y], color[x][y]));
			    dc.SetPixel(x,y,RGB(color[x][y], color[x][y], color[x][y]));
			else
			{
				z = 4*color[x][y]-color[x-1][y]-color[x][y-1]-color[x+1][y]-color[x][y+1];
				if(z>=0)
				{
					//dc.SetPixel(x+pDoc->fX,y,RGB(255, 255, 255));
					dc.SetPixel(x,y,RGB(255, 255, 255));
				}
				else
				{
					//dc.SetPixel(x+pDoc->fX,y,RGB(0, 0, 0));
					dc.SetPixel(x,y,RGB(0, 0, 0));
					H[N][0] = x;
					H[N][1] = y-1;
//					fprintf(fp, "(%d,%d), ", x,y);
					N++;
				}
			}
		}
	}
	N--;
	CString s;
	//s.Format("%d,%d",H[0][0],H[0][1]);
	//MessageBox(s);
}

void CMSSB03View::OnBUTTONtu2ma() 
{
	// TODO: Add your control notification handler code here
	CClientDC dc(this);
	CRect clientRect;
	GetClientRect(&clientRect);
	OnPrepareDC(&dc);
	dc.DPtoLP(&clientRect);
	CMSSB03Doc * pDoc = GetDocument();
	CPen myPen;
	CRect rectClient(5, 220, 770, 900); 
	CBrush brushBkColor; 
	brushBkColor.CreateSolidBrush(RGB(255,255,255)); 
	//dc.DPtoLP(rectClient); 
	dc.FillRect(rectClient, &brushBkColor); 
	int i, j, k, l, m, n, p, firstpoint[2], secondpoint[2], oldpoint[2400][2];
	CString s;
	i = 0;
	j = 0;
	Q = 0;
	start[Q][0] = H[0][0];
	start[Q][1] = H[0][1];
	start[Q][2] = 0;
	firstpoint[0] = H[0][0];
	firstpoint[1] = H[0][1];
	secondpoint[0] = H[0][0];
	secondpoint[1] = H[0][1];
	oldpoint[0][0] = H[0][0];
	oldpoint[0][1] = H[0][1];
	//s.Format("%d,%d,%d,%d,%d,%d,%d,%d",start[Q][0],start[Q][1],start[Q][2],firstpoint[0],firstpoint[1],secondpoint[0],secondpoint[1],oldpoint[0][0],oldpoint[0][1]);
	//MessageBox(s);
	m = 1;
	k = 1;
	dc.SetTextColor(RGB(0,0,150));
	s.Format("原图像的链码为:");
	dc.TextOut(225,25,s);
	//m_chain ="";
	while(i<=N)
	{
	    p = abs(firstpoint[1]-secondpoint[1])+abs(firstpoint[0]-secondpoint[0]);
		//s.Format("%d",i);
		//MessageBox(s);
		if(p>5)
		{
			Q++;
			start[Q][0] = secondpoint[0];
			start[Q][1] = secondpoint[1];
			start[Q][2] = i;
			
		}
		firstpoint[1] = secondpoint[1];
		firstpoint[0] = secondpoint[0];
		k = 0;
		j = 0;
		for(j=0; j<=N; j++)
			{
				n=0;
				if(m==10)
				{
					oldpoint[0][0]=100;
					oldpoint[0][1]=100;
				}
				for(l=0; l<m; l++)
				{
					if(H[j][0]==oldpoint[l][0]&&H[j][1]==oldpoint[l][1])
					{
						n=1;
						break;
					}
				}
				if(n==1)
					continue;
				else
				{
					secondpoint[0]=H[j][0];
					secondpoint[1]=H[j][1];
					if(secondpoint[1]==firstpoint[1]&&secondpoint[0]==(firstpoint[0]+1))
					{
						chain[i]=0;
						k=1;
					}
					else if(secondpoint[1]==(firstpoint[1]-1)&&secondpoint[0]==firstpoint[0])
					{
						chain[i]=2;
						k=1;
					}
					else if(secondpoint[1]==firstpoint[1]&&secondpoint[0]==(firstpoint[0]-1))
					{
						chain[i]=4;
						k=1;
					}
					else if(secondpoint[1]==(firstpoint[1]+1)&&secondpoint[0]==firstpoint[0])
					{
						chain[i]=6;
						k=1;
					}
					else if(secondpoint[1]==(firstpoint[1]-1)&&secondpoint[0]==(firstpoint[0]+1))
					{
							chain[i]=1;
							k=1;
					}
					else if(secondpoint[1]==(firstpoint[1]-1)&&secondpoint[0]==(firstpoint[0]-1))
					{
							chain[i]=3;
							k=1;
					}
					else if(secondpoint[1]==(firstpoint[1]+1)&&secondpoint[0]==(firstpoint[0]-1))
					{
							chain[i]=5;
							k=1;
					}
					else if(secondpoint[1]==(firstpoint[1]+1)&&secondpoint[0]==(firstpoint[0]+1))
					{
							chain[i]=7;
							k=1;
					}
					else if(secondpoint[1]==firstpoint[1]&&secondpoint[0]==firstpoint[0])
					{
						oldpoint[m][0]=secondpoint[0];
						oldpoint[m][1]=secondpoint[1];
						m++;
					}
				}
				if(k==1)
				{
					myPen.CreatePen(PS_DASH,0, RGB(255, 0, 0));
					CPen* pOldPen=dc.SelectObject(&myPen);
					pOldPen=dc.SelectObject(&myPen);
					//dc.MoveTo(pDoc->fX+firstpoint[0], firstpoint[1]);
					//dc.LineTo(pDoc->fX+secondpoint[0], secondpoint[1]);
					dc.SelectObject(pOldPen);
					myPen.DeleteObject();
					s.Format("%d,",chain[i]);
					//m_chain = s;
					dc.TextOut(132+(i%32)*20,225+15*(int)(i/32),s);
					oldpoint[m][0]=secondpoint[0];
					oldpoint[m][1]=secondpoint[1];
					//UpdateData(false);
					m++;
					i++;
					break;
				}
			}
			if(i>(N-10)&&k==0)
				break;
			//s.Format("%d,%d,%d,%d,%d,%d,%d,%d",start[Q][0],start[Q][1],start[Q][2],firstpoint[0],firstpoint[1],secondpoint[0],secondpoint[1],oldpoint[0][0],oldpoint[0][1]);
	        //MessageBox(s);
		}
	M = i-1;
	//f2 =TRUE;
	A = 0;
	L = 0;
	for(i = 0; i<pDoc->fX; i++)
		for(j = 0; j<pDoc->fY; j++)
			if(color[i][j]==0)
				A++;
	for(i = 0; i<=M; i++)
	{
		if(chain[i]==0||chain[i]==2||chain[i]==4||chain[i]==6)
			L += 1;
		else
			L += sqrt(2);
	}
	C = L*L/(4*3.1415926*A);
	int x, y, num[256], sum;
	COLORREF c;
	BYTE r, g, b;
	double m00, m10, m01;
	int grayValue;
	sum = (pDoc->fX)*(pDoc->fY);
	//s.Format("sum=%d",sum);
	//MessageBox(s);
	M1 = 0;
	m00 = 0; 
	m01 = 0; 
	m10 = 0;
	for(i = 0; i<256; i++)
		num[i] = 0;
	for(x = 0; x<pDoc->fX; x++)
	{
		for(y = 0; y<pDoc->fY; y++)
		{
			c = dc.GetPixel(x,y);
			r = GetRValue(c);
			g = GetGValue(c);
			b = GetBValue(c);
			grayValue = (int)(r+g+b)/3;
			m00 += grayValue;
			m10 += x*grayValue;
			m01 += y*grayValue;
			num[grayValue]++;
		}
	}
	X0 = m10/m00;
	Y0 = m01/m00;
	for(i = 0; i<256; i++)
		M1 += (i*num[i])/sum;
}

void CMSSB03View::OnBUTTONma2tu() 
{
	// TODO: Add your control notification handler code here
	CMSSB03Doc * pDoc = GetDocument();
    CClientDC dc(this);
	CRect clientRect;
	GetClientRect(&clientRect);
	OnPrepareDC(&dc);
	dc.DPtoLP(&clientRect);
	CPen myPen;
	CRect rectClient(5, 220, 770, 900);
	CRect rectClient1(pDoc->fX, 0, 2*(pDoc->fX), pDoc->fY); 
	CBrush brushBkColor; 
	CBrush brushBkColor1; 
	brushBkColor.CreateSolidBrush(RGB(255,255,255)); 
	brushBkColor1.CreateSolidBrush(RGB(236,233,216)); 
	//dc.DPtoLP(rectClient); 
	dc.FillRect(rectClient, &brushBkColor); 
	dc.FillRect(rectClient1, &brushBkColor1); 
	int firstpoint[2], secondpoint[2], i, j, k;
	CString s;
	secondpoint[0]=H[0][0];
	secondpoint[1]=H[0][1];
	dc.SetTextColor(RGB(0,0,150));
	s.Format("原图像的链码为:");
	dc.TextOut(10,225,s);
	for(i=0; i<=M; i++)
	{
		firstpoint[0]=secondpoint[0];
		firstpoint[1]=secondpoint[1];
		for(j=0; j<=Q; j++)
		{
			if(i==start[j][2])
			{
				firstpoint[0]=start[j][0];
				firstpoint[1]=start[j][1];
				secondpoint[0]=start[j][0];
				secondpoint[1]=start[j][1];
			}
		}
		if(chain[i]==0)
		{
			secondpoint[0]=secondpoint[0]+1;
		}
		else if(chain[i]==1)
		{
			secondpoint[0]=secondpoint[0]+1;
			secondpoint[1]=secondpoint[1]-1;
		}
		else if(chain[i]==2)
		{
			secondpoint[0]=secondpoint[0];
			secondpoint[1]=secondpoint[1]-1;
		}
		else if(chain[i]==3)
		{
			secondpoint[0]=secondpoint[0]-1;
			secondpoint[1]=secondpoint[1]-1;
		}
		else if(chain[i]==4)
		{
			secondpoint[0]=secondpoint[0]-1;
			secondpoint[1]=secondpoint[1];
		}
		else if(chain[i]==5)
		{
			secondpoint[0]=secondpoint[0]-1;
			secondpoint[1]=secondpoint[1]+1;
		}
		else if(chain[i]==6)
		{
			secondpoint[0]=secondpoint[0];
			secondpoint[1]=secondpoint[1]+1;
		}
		else if(chain[i]==7)
		{
			secondpoint[0]=secondpoint[0]+1;
			secondpoint[1]=secondpoint[1]+1;
		}
		myPen.CreatePen(PS_DASH, 1, RGB(255,0,0));
		CPen* pOldPen=dc.SelectObject(&myPen);
		pOldPen=dc.SelectObject(&myPen);
		dc.MoveTo((pDoc->fX)+firstpoint[0]+30, firstpoint[1]);
		dc.LineTo((pDoc->fX)+secondpoint[0]+30, secondpoint[1]);
		dc.SelectObject(pOldPen);
		myPen.DeleteObject();
		s.Format("%d,",chain[i]);
		dc.TextOut(132+(i%32)*20,225+15*(int)(i/32),s);
	}
}

void CMSSB03View::OnBUTTONtezhengtiqu() 
{
	// TODO: Add your control notification handler code here
	CClientDC dc(this);
	int i, j;
	CString s;
	j = 0;
	for(i=0; i<CHR; i++)
	{
		if(C-series[i][2]<0.01&&C-series[i][2]>-0.01)
		{
			j=1;
			break;
		}
	}
	/*if(j==1)
	{
		s.Format("第%d类工件特征", i+1);
		//dc.SetTextColor(RGB(255,0,0));
		//dc.TextOut(210, 210, s);
		MessageBox(s);
		m_girth = series[i][0];
		m_area = series[i][1];
		m_dens = series[i][2];
		m_gray = series[i][3];
        s.Format("(%f, %f)", series[i][4], series[i][5]);
		m_cg = s;
	}*/
	if(j==1)
	{
		s.Format("该工件的数据已储存!");
		MessageBox(s);
	}
	else
	{
		series[CHR][0]=L;
		series[CHR][1]=A;
		series[CHR][2]=C;
		series[CHR][3]=M1;
		series[CHR][4]=X0;
		series[CHR][5]=Y0;
		CHR++;
		//s.Format("第%d类工件特征", CHR);
		//dc.SetTextColor(RGB(255,0,0));
		//dc.TextOut(210, 210, s);
		//MessageBox(s);
		m_girth = L;
        m_area = A;
		m_dens = C;
		m_gray = M1;
		s.Format("(%f, %f)", X0,Y0);
		m_cg = s;
	}
	UpdateData(false);
}

void CMSSB03View::OnBUTTONgongjian() 
{
	// TODO: Add your control notification handler code here
	CString s;
	int i, j;
	j=0;
	for(i=0; i<CHR; i++)
	{
		if(C-series[i][2]<0.01&&C-series[i][2]>-0.01)
		{
			j=1;
			break;
		}
	}
	if(j==1)
	{
		//s.Format("该工件属于第%d类工件,区域周长L:%f,区域面积A:%d,密集度:%f,灰度:%f,灰度重心:(%f, %f)", (i+1),series[i][0],series[i][1],series[i][2],series[i][3],series[i][4], series[i][5]);
		s.Format("该工件属于第%d类,区域周长L:%f,区域面积A:%d,密集度:%f,灰度:%f,灰度重心:(%f, %f)", (i+1),L,A,C,M1,X0,Y0);
		MessageBox(s);
		/*dc.SetTextColor(RGB(255,0,0));
		dc.TextOut(10, fY+10, s); 
		s.Format("   第%d类工件的区域周长L:  L=%f;                      当前工件的区域周长L:  L=%f", (i+1), series[i][0], L);
		dc.SetTextColor(RGB(0,255,0)); 
		dc.TextOut(10, fY+40, s);
		s.Format("   第%d类工件的区域面积A:  A=%d;                            当前工件的区域面积A:  A=%d;", (i+1), (int)series[i][1], A);
		dc.SetTextColor(RGB(0,0,255));
		dc.TextOut(10, fY+70, s);
		s.Format("   第%d类工件的区域形状参数(密集度):  C=%f;           当前工件的区域形状参数(密集度):  C=%f", (i+1), series[i][2], C);
		dc.SetTextColor(RGB(10,150,100));
		dc.TextOut(10, fY+100, s);
		s.Format("   第%d类工件的灰度均值M1:  M1=%f;                    当前工件的灰度均值M1:  M1=%f", (i+1), series[i][3], M1);
		dc.SetTextColor(RGB(50,100,180));
		dc.TextOut(10, fY+130, s);
		s.Format("   第%d类工件的灰度重心坐标:  (X0, Y0)=(%f, %f);      当前工件的灰度重心坐标:  (X0, Y0)=(%f, %f)", (i+1), series[i][4], series[i][5], X0, Y0);
		dc.SetTextColor(RGB(200,100,80));
		dc.TextOut(10, fY+160, s);*/
	}
	else
	{
		s.Format("该工件数据尚未储存,请先提取特征!");
		//dc.SetTextColor(RGB(255,0,0));
		//dc.TextOut(10, fY+10, s);
		MessageBox(s);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -