?? 中國地圖著色dlg.cpp
字號:
// 中國地圖著色Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "中國地圖著色.h"
#include "中國地圖著色Dlg.h"
#include "fstream.h"
#include <time.h>
ofstream fout("result.txt");
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_size = 0;
m_MutateProbability = 0.0;
m_CrossProbability = 0.0;
m_GENERATION = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Text(pDX, IDC_EDIT2, m_size);
DDV_MinMaxUInt(pDX, m_size, 0, 1000);
DDX_Text(pDX, IDC_EDIT4, m_MutateProbability);
DDV_MinMaxDouble(pDX, m_MutateProbability, 0., 1.);
DDX_Text(pDX, IDC_EDIT3, m_CrossProbability);
DDV_MinMaxDouble(pDX, m_CrossProbability, 0., 1.);
DDX_Text(pDX, IDC_EDIT5, m_GENERATION);
DDV_MinMaxLong(pDX, m_GENERATION, 0, 100000);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
//const CPoint center[32]={(371,168),(173,179),(255,293),(253,325),(322,413),(280,352),(178,324),
// (94,297),(70,380),(122,362),(171,371),(225,382),(284,418),(289,443),
// (313,458),(301,469),(341,523),(374,540),(407,551),(268,471),(233,434),
// (177,438),(144,427),(74,438),(145,467),(209,475),(203,496),(193,526),
// (179,514),(114,509),(31,403),(88,532)};
char *province[32]={"新疆","西藏","青海","甘肅","內(nèi)蒙古","寧夏","四川","云南","廣西","貴州",
"重慶","陜西","山西","河北","北京","天津","遼寧","吉林","黑龍江","山東","河南","湖北",
"湖南","廣東","江西","安徽","江蘇","上海","浙江","福建","海南","臺灣"};
char *colorname[4]={"紅色","綠色","藍(lán)色","粉紅色"};
int center_x[32]={371,173,255,253,322,280,178,94,70,122,171,225,284,289,313,301,341,374,
407,268,233,177,144,74,145,209,203,193,179,114,31,88};
int center_y[32]={168,179,293,325,413,352,324,297,380,362,371,382,418,443,458,469,523,
540,551,471,434,438,427,438,467,475,496,526,514,509,403,532};
int color[32]={0};
CString info;
char NeighbourMatrix[32][32]={ //為對稱矩陣,這里只列出一半
100,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,100,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,100,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,100,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,100,1,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,100,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,100,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,100,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100};
BOOL CMyDlg::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
for(int i=0;i<32;i++) //填滿鄰居矩陣的另一半
for(int j=i;j<32;j++)
if(NeighbourMatrix[i][j]==1)
NeighbourMatrix[j][i]=1;
m_size=20;
m_CrossProbability=0.8;
m_MutateProbability=0.5;
m_GENERATION=20000;
UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::Open()
{
BYTE * pBuffer;
CFile bmp("地圖.bmp",CFile::modeRead);
bmp.Seek(54 ,CFile::begin);
pBuffer=new BYTE[624*512*3];
bmp.Read(pBuffer,624*512*3);
bmp.Close();
CClientDC dc(this);
int r,g,b;
int i,j;
for(i=0;i<512;i++)
{
for(j=0;j<624;j++)
{
b=*pBuffer;
g=*(pBuffer+1);
r=*(pBuffer+2);
pBuffer=pBuffer+3;
if((r+g+b)/3>50)
bmpTwo[i][j]=255;
else
bmpTwo[i][j]=0;
dc.SetPixel(j+11,10+512-i,RGB(bmpTwo[i][j],bmpTwo[i][j],bmpTwo[i][j]));
bmpTwo[i][j]=255-bmpTwo[i][j];
}
}
}
void CMyDlg::Thining()
{
int i=0,j;
int flag = 1;
BYTE **bmpThin;
bmpThin = new BYTE *[512];
for(i=0;i<512;i++)
bmpThin[i] = new BYTE[624];
for(i=0;i<512;i++)
for(j=0;j<624;j++)//初始化
bmpThin[i][j]= 0;
while(flag==1) //flag=0時(shí)迭代結(jié)束
{
flag=0;
for( i=2;i<512-2;i++)
{
for(j=2;j<624-2;j++)
{
if(bmpTwo[i-1][j-1]==0xff //模板a
&& bmpTwo[i-1][j+1]==0
&& bmpTwo[i][j-1]==0xff
&& bmpTwo[i][j-0]==0
&& bmpTwo[i][j+1]==0
&& bmpTwo[i][j+2]==0
&& bmpTwo[i+1][j-1]==0xff
&& bmpTwo[i+1][j+1]==0)
{
bmpThin[i][j]=0xff;
flag=1;
continue;
}
if(bmpTwo[i-1][j-0]==0 //模板b
&& bmpTwo[i-1][j+1]==0
&& bmpTwo[i][j-1]==0xff
&& bmpTwo[i][j-0]==0
&& bmpTwo[i][j+1]==0
&& bmpTwo[i][j+2]==0
&& bmpTwo[i+1][j-1]==0xff
&& bmpTwo[i+1][j-0]==0xff)
{
bmpThin[i][j]=0xff;
flag=1;
continue;
}
if(bmpTwo[i-1][j-1]==0xff //模板c
&& bmpTwo[i-1][j-0]==0xff
&& bmpTwo[i][j-1]==0xff
&& bmpTwo[i][j-0]==0
&& bmpTwo[i][j+1]==0
&& bmpTwo[i][j+2]==0
&& bmpTwo[i+1][j-0]==0
&& bmpTwo[i+1][j+1]==0)
{
bmpThin[i][j]=0xff;
flag=1;
continue;
}
if(bmpTwo[i-1][j-1]==0xff //模板d
&& bmpTwo[i-1][j-0]==0xff
&& bmpTwo[i-1][j+1]==0xff
&& bmpTwo[i][j-0]==0
&& bmpTwo[i+1][j-1]==0
&& bmpTwo[i+1][j-0]==0
&& bmpTwo[i+1][j+1]==0)
{
bmpThin[i][j]=0xff;
flag=1;
continue;
}
if(bmpTwo[i-1][j-1]==0 //模板e(cuò)
&& bmpTwo[i-1][j+1]==0xff
&& bmpTwo[i][j-1]==0
&& bmpTwo[i][j-0]==0
&& bmpTwo[i][j+1]==0xff
&& bmpTwo[i+1][j-1]==0
&& bmpTwo[i+1][j+1]==0xff)
{
bmpThin[i][j]=0xff;
flag=1;
continue;
}
if(bmpTwo[i-1][j-0]==0 //模板f
&& bmpTwo[i][j-1]==0
&& bmpTwo[i][j-0]==0
&& bmpTwo[i][j+1]==0xff
&& bmpTwo[i+1][j-0]==0xff
&& bmpTwo[i+1][j+1]==0xff)
{
bmpThin[i][j]=0xff;
flag=1;
continue;
}
if(bmpTwo[i-1][j-0]==0xff //模板g
&& bmpTwo[i-1][j+1]==0xff
&& bmpTwo[i][j-1]==0
&& bmpTwo[i][j-0]==0
&& bmpTwo[i][j+1]==0xff
&& bmpTwo[i+1][j-0]==0)
{
bmpThin[i][j]=0xff;
flag=1;
continue;
}
if(bmpTwo[i-2][j-0]==0 //模板h
&& bmpTwo[i-1][j-1]==0
&& bmpTwo[i-1][j-0]==0
&& bmpTwo[i-1][j+1]==0
&& bmpTwo[i][j-0]==0
&& bmpTwo[i+1][j-1]==0xff
&& bmpTwo[i+1][j-0]==0xff
&& bmpTwo[i+1][j+1]==0xff)
{
bmpThin[i][j]=0xff;
flag=1;
continue;
}
}
}
for(i=0;i<512;i++)
{
for(j=0;j<624;j++)
if(bmpThin[i][j]==0xff)
bmpTwo[i][j]=0xff;
}
}
for( i=2;i<512-2;i++) //第二次串行細(xì)化
{
for(j=2;j<624-2;j++)
{
//縮小后的模板a
if(bmpTwo[i-1][j-1]==0xff
&& bmpTwo[i-1][j+1]==0
&& bmpTwo[i][j-1]==0xff
&& bmpTwo[i][j-0]==0
&& bmpTwo[i][j+1]==0
&& bmpTwo[i+1][j-1]==0xff
&& bmpTwo[i+1][j+1]==0)
{
bmpTwo[i][j]=0xff;
flag=1;
continue;
}
//縮小后的模板h
if(bmpTwo[i-1][j-1]==0
&& bmpTwo[i-1][j-0]==0
&& bmpTwo[i-1][j+1]==0
&& bmpTwo[i][j-0]==0
&& bmpTwo[i+1][j-1]==0xff
&& bmpTwo[i+1][j-0]==0xff
&& bmpTwo[i+1][j+1]==0xff)
{
bmpTwo[i][+j]=0xff;
flag=1;
continue;
}
}
}
delete []bmpThin;
CClientDC dc(this);
// for(i=0;i<512;i++)
for(i=511;i>=0;i--)
{
for(j=0;j<624;j++)
{
if(bmpTwo[i][j]==0xff)
dc.SetPixel(j+11,512+10-i,RGB(255,255,255));
else
dc.SetPixel(j+11,512+10-i,RGB(0,0,0));
}
}
}
void CMyDlg::RegionGrow(BYTE *bwImage,int l)
{
static int nDx[]={-1,0,1,0};
static int nDy[]={ 0,1,0,-1};
// 定義堆棧的起點(diǎn)和終點(diǎn)
// 當(dāng)nStart=nEnd, 表示堆棧中只有一個(gè)點(diǎn)
long int nStart=0 ;
long int nEnd=0 ;
// p_bw=new BYTE(512*624);
int *GrowRegionx = new int [512*624];
int *GrowRegiony = new int [512*624];
// 當(dāng)前正在處理的象素
int nCurrx ;
int nCurry ;
// 循環(huán)控制變量
int k ;
// 圖象的橫縱坐標(biāo),用來對當(dāng)前象素的4鄰域進(jìn)行遍歷
int xx;
int yy;
CClientDC dc(this);
memset(p_bw,0,sizeof(BYTE)*319488);
nStart=0;
nEnd=0;
Sleep(500);
// 把種子點(diǎn)的坐標(biāo)壓入棧
GrowRegionx[nEnd] = center_y[l];
GrowRegiony[nEnd] = center_x[l];
while (nStart<=nEnd)
{
// 當(dāng)前種子點(diǎn)的坐標(biāo)
nCurrx = GrowRegionx[nStart];
nCurry = GrowRegiony[nStart];
// 對當(dāng)前點(diǎn)的4鄰域進(jìn)行遍歷
for (k=0; k<4; k++)
{
// 4鄰域象素的坐標(biāo)
xx = nCurrx+nDx[k];
yy = nCurry+nDy[k];
// 判斷象素(xx,yy) 是否在圖像內(nèi)部
if ( (xx < 624) && (xx>=0) && (yy<512) && (yy>=0)
&&p_bw[yy*624+xx]==0
&&bwImage[yy*624+xx] ==255)
{
// 堆棧的尾部指針后移一位
nEnd++;
// 象素(xx,yy) 壓入棧
GrowRegionx[nEnd] = xx;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -