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

📄 reconizedlg.cpp

📁 该程序是能够处理图象分割和识别等等内容,为朋友提供图象处理原代码.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ReconizeDlg.cpp : implementation file
//著者:王侯艳      2004,6,20

#include "stdafx.h"
#include "Reconize.h"
#include "ReconizeDlg.h"

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

BYTE Buffer[1000][1000][3];	// 24位真彩色图// 0--blue 1--green 2--red
BYTE Buffer1[1000][1000];	// 灰度图
BYTE BufferReg[1000][1000];	//二值图
BYTE pByte[3200000];	//内存镜像文件

int Flag=-1;

int index1,index2,index3,index4;	//分别是以下四个数组的上界标识.
int  PointLT[50000][3] ,PointRT[50000][3] ,PointRB[50000][3] ,PointLB[50000][3] ;

int indexRect;
int  Rect[50000][4];	//匹配的矩形记录,里面放的是PointLT等..中对应坐标的索引.

int  avgPointLT[50000][3] ,avgPointRT[50000][3] ,avgPointRB[50000][3] ,avgPointLB[50000][3] ;//依次对应匹配的平均矩形的左顶点,右顶点,右底点,左底点底坐标
int  avgindexRect; //匹配的平均矩形记录

int  HaveJumpRectLT[50000][3],HaveJumpRectRT[50000][3],HaveJumpRectRB[50000][3],HaveJumpRectLB[50000][3];//含有足够跳变的矩形坐标记录
int  JumpindexRect; //含有足够跳变的矩形数目
///////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReconizeDlg dialog

CReconizeDlg::CReconizeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CReconizeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CReconizeDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CReconizeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReconizeDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CReconizeDlg, CDialog)
	//{{AFX_MSG_MAP(CReconizeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN_FILE, OnOpenFile)
	ON_BN_CLICKED(IDC_TO_GRAY, OnToGray)
	ON_BN_CLICKED(IDC_FILE_EXIT, OnFileExit)
	ON_BN_CLICKED(IDC_WHITE_BLACK, OnWhiteBlack)
	ON_BN_CLICKED(IDC_DRAW_EDGE, OnDrawEdge)
	ON_BN_CLICKED(IDC_DRAW_RECT, OnDrawRect)
	ON_BN_CLICKED(IDC_QUICK_RECONIZE, OnQuickReconize)
	ON_BN_CLICKED(IDC_GET_RECT, OnGetRect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReconizeDlg message handlers

BOOL CReconizeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CReconizeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CReconizeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CReconizeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CReconizeDlg::OnOpenFile() 
{
	// TODO: Add your control notification handler code here
	LPCTSTR lpszFilter="位图文件(*.BMP)|*.bmp|ALL FILES(*.*)|*.*";
	CFileDialog OpenDlg(TRUE,lpszFilter,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,lpszFilter,NULL);
	if(OpenDlg.DoModal()==IDOK)
	{
		InvalidateRect(NULL);
		FillMatrix(OpenDlg.GetPathName());
		DrawBMP();
	}

	Flag=0;
}

void CReconizeDlg::DrawBMP()//绘制bmp
{
	CClientDC dc(this);
	int i,j;
	for(i=0;i<m_Width;i++)  //rgb i=0;i<m_Width;i++
		for(j=0;j<m_Height;j++)
			dc.SetPixel(i+1,j+1,RGB(Buffer[i][j][2],Buffer[i][j][1],Buffer[i][j][0]) );	//why 0--blue 1--green 2--red
////////////////////////////////////////////////////////
}

void CReconizeDlg::FillMatrix(CString filename)	//将原始图形文件读入数组Buffer
{
////////////////////////////////////////////////////
	m_Width=0;
	m_Height=0;
	CFile fbmp;
	if (fbmp.Open(filename,CFile::modeRead)==FALSE)
	{
		MessageBox("文件打开出错");
		return;
	}

	char head1,head2;
	fbmp.Read(&head1,1);
	fbmp.Read(&head2,1);
	if(head1!='B' || head2!='M' )
	{
		MessageBox("找不到bmp文件");
		fbmp.Close();
		return;
	}
////////////////////////////////////////////////////
	DWORD filesize=fbmp.GetLength();
	fbmp.Seek(0,CFile::begin);
	fbmp.ReadHuge(pByte,filesize);
	fbmp.Close();

	BYTE *p;
	p=pByte+18;//18
	int tmpWidth,tmpHeight;

	tmpWidth= *p + (*(p+1)) *256 + (*(p+2)) *256*256 + (*(p+3)) *256*256*256 ; //256
	p=pByte+22;//22
	tmpHeight= *p + (*(p+1)) *256 + (*(p+2)) *256*256 + (*(p+3)) *256*256*256 ;//256 
	CString tmpStr1;
	MessageBox("胡建涛----2007--6--20");
	tmpStr1.Format("图象文件大小:宽 %d , 高 %d",tmpWidth,tmpHeight);
	MessageBox(tmpStr1);
	if(tmpWidth>1000 || tmpHeight >1000)
	{                                                                                                                                                                                                
		MessageBox("处理的bmp文件太大");
		return;
	}

	int especial=tmpWidth%4;	
	p=pByte+10;//10
	int bmpflg;                                                                              
	bmpflg= *p + (*(p+1)) *256 + (*(p+2)) *256*256 + (*(p+3)) *256*256*256 ;
	p=pByte+bmpflg;//bmpflg
	int i,j,k;
	for(i=tmpHeight-1;i>=0;i--)                                                                                                                                   
		for(j=0;j<tmpWidth;j++)
		{
			for(k=0;k<3;k++)
			{
				Buffer[j][i][k]=*p;//
				p++;
			}//图象数据
    if(j==tmpWidth-1)
	{                                                                                                                      
		p=p+especial;
	}
		}

	m_Width=tmpWidth;
	m_Height=tmpHeight;
}

void CReconizeDlg::GetGrayBmp()
{
	// TODO: Add your command handler code here
	//二值化。
	int i,j;
	// x=0.299*red+0.587*green+0.114*blue;
	for(i=0;i<m_Width;i++)
		for(j=0;j<m_Height;j++)
		   Buffer1[i][j]=(BYTE)( 0.114*Buffer[i][j][0]+0.587*Buffer[i][j][1]+0.299*Buffer[i][j][2] );
}

void CReconizeDlg::OnToGray() 
{
	// TODO: Add your control notification handler code here
	int i,j;
	GetGrayBmp();
	CClientDC dc(this);
	for(i=0;i<m_Width;i++)  //rgb
		for(j=0;j<m_Height;j++)
			dc.SetPixel(i+1,j+1,RGB(Buffer1[i][j],Buffer1[i][j],Buffer1[i][j]) );

    Flag=1;
}

void CReconizeDlg::OnFileExit() 
{
	// TODO: Add your control notification handler code here
	exit(0);
}
void CReconizeDlg::OnWhiteBlack() 
{
	// TODO: Add your control notification handler code here
	if(Flag!=1)
	{
		MessageBox("未灰度化","除错",MB_ICONSTOP|MB_OK);
		return;
	}
	
	DrawGetregion();
	EraseDot2();
	DrawRegion();

	Flag=2;
}
void CReconizeDlg::DrawGetregion() //计算出二值图
{
	// TODO: Add your command handler code here
	int i,j;
	int defvalue=15;//阈值点
	for(i=0;i<m_Width;i++)
		for(j=0;j<m_Height;j++)
			BufferReg[i][j]=0;

	for(i=0;i<m_Height;i++)
		for(j=0;j<m_Width-1;j++)
		{
			if( abs(Buffer1[j][i]-Buffer1[j+1][i])>defvalue )
				BufferReg[j][i]=255;
		}
	
	for(i=0;i<m_Width;i++)
		for(j=0;j<m_Height-1;j++)
		{
			if( abs(Buffer1[i][j]-Buffer1[i][j+1])>defvalue )
				BufferReg[i][j]=255;
		}
}

void CReconizeDlg::DrawRegion() //绘出二值图
{
	// TODO: Add your command handler code here
	int i,j;
	CClientDC dc(this);
	for(i=0;i<m_Width;i++)  //rgb
		for(j=0;j<m_Height;j++)
			dc.SetPixel(i+1,j+1,RGB( BufferReg[i][j],
										BufferReg[i][j],
										BufferReg[i][j] )  );
}///////////////////////////////////////////////////////////

void CReconizeDlg::EraseDot()
{ //m_Width,m_Height
  int i,j;
    for(i=1;i<m_Width-1;i++)
		for(j=1;j<m_Height-1;j++)
		{
		   if( (BufferReg[i][j]!=BufferReg[i-1][j-1]) && (BufferReg[i][j]!=BufferReg[i][j-1])
			  && (BufferReg[i][j]!=BufferReg[i+1][j-1]) && (BufferReg[i][j]!=BufferReg[i-1][j])
			  && (BufferReg[i][j]!=BufferReg[i+1][j]) && (BufferReg[i][j]!=BufferReg[i-1][j+1])
			  &&(BufferReg[i][j]!=BufferReg[i][j+1]) && (BufferReg[i][j]!=BufferReg[i+1][j+1])
			  )
			  BufferReg[i][j]=255-BufferReg[i][j];  
		}///////////////////////////////////////
}

void CReconizeDlg::EraseDot2()
{
	EraseDot();
	int i,j;
	for(i=4;i<m_Width-4;i++)
		for(j=4;j<m_Height-4;j++)
		{
			if(		BufferReg[i-1][j-1]==BufferReg[i][j-1] 
				&&  BufferReg[i-1][j-1]==BufferReg[i+1][j-1] 
				&&	BufferReg[i-1][j-1]==BufferReg[i+2][j-1]

				&&	BufferReg[i-1][j-1]==BufferReg[i-1][j]
				&&	BufferReg[i-1][j-1]==BufferReg[i+2][j]

				&&	BufferReg[i-1][j-1]==BufferReg[i-1][j+1]
				&&	BufferReg[i-1][j-1]==BufferReg[i+2][j+1]

				&&	BufferReg[i-1][j-1]==BufferReg[i-1][j+2]
				&&	BufferReg[i-1][j-1]==BufferReg[i][j+2]
				&&	BufferReg[i-1][j-1]==BufferReg[i+1][j+2]
				&&	BufferReg[i-1][j-1]==BufferReg[i+2][j+2]
			   )
			{
				BufferReg[i][j]=BufferReg[i-1][j-1];
				BufferReg[i+1][j]=BufferReg[i-1][j-1];
				BufferReg[i][j+1]=BufferReg[i-1][j-1];
				BufferReg[i+1][j+1]=BufferReg[i-1][j-1];
			}
		
		}
}

void CReconizeDlg::Getpts() 
{
	// TODO: Add your command handler code here
	int i,j,k,l;
	int flag1,flag2,flag3,flag4;
	int iflag;

	//初始化
	index1=0;    index2=0;    index3=0;    index4=0;    
	for(i=0;i<10000;i++)
		for(j=0;j<3;j++)
		{
			PointLT[i][j]=-1;
			PointRT[i][j]=-1;
			PointLB[i][j]=-1;
			PointRB[i][j]=-1;
		}

	int step=2;
	int longOffset=30;
	int shortOffset=2;//
	int whitepercent=48;
	///////////////////////////////////////////////////////////////////////
	//求左上角
	flag1=0;	flag2=0;	flag3=0;	flag4=0;
	iflag=0;	index1=0;
	for(i=longOffset;i<m_Width-longOffset;i++)
	{
		for(j=longOffset;j<m_Height-longOffset;j++)
		{
			for(k=1;k<=longOffset;k++)
				for(l=1;l<=shortOffset;l++)
				{
					if(BufferReg[i+k][j-l]==255)
						flag1+=1;
				}
			for(k=1;k<=longOffset;k++)
				for(l=1;l<=shortOffset;l++)
				{
					if(BufferReg[i+k][j+l]==255)
						flag2+=1;
				}
			for(k=1;k<=shortOffset;k++)
				for(l=1;l<=longOffset;l++)
				{
					if(BufferReg[i+k][j+l]==255)
						flag3+=1;
				}
			for(k=1;k<=shortOffset;k++)
				for(l=1;l<=longOffset;l++)
				{
					if(BufferReg[i-k][j+l]==255)
						flag4+=1;
				}
			if( (flag2>=whitepercent) && (flag3>=whitepercent) )
			{
				iflag=1;
				j+=step;
				PointLT[index1][0]=i;
				PointLT[index1][1]=j;
				PointLT[index1][2]=1;
				index1++;
			}
			/////////////////////////////////////////////////
			flag1=0;	flag2=0;	flag3=0;	flag4=0;
			//////////////////////////////////////////////////
		}
		if(iflag==1)
		{
			i+=step;
			iflag=0;
		}
	}

	////////////////////////////////////////////////////////////
	//求右上角

⌨️ 快捷键说明

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