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

📄 svm.cpp

📁 使用支持向量机(svm)方法进行图像的单视度量(single view metrology)的vc++(需要用到wxwindows2.4.2)和matlab源代码.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		long **temp = new long*[vpCount];
		for (int i=0;i<vpCount;i++) {
			temp[i] = new long[2];
			if (i<vpCount-1) {
				temp[i][0] = vanishingPoints[i][0];
				temp[i][1] = vanishingPoints[i][1];
			} else {
				temp[i][0] = x;
				temp[i][1] = y;
			}
		}

		for (int j=0;j<vpCount-1;j++) delete vanishingPoints[j];
		delete vanishingPoints;

		vanishingPoints = temp;
		if (vpCount==1) {
			selectedVP[0][0] = selectedVP[1][0] = vanishingPoints[0][0];
			selectedVP[0][1] = selectedVP[1][1] = vanishingPoints[0][1];
		}

		vpLoaded = true;

		((MainFrame*)(GetParent()->GetParent()))->SetStatusText(wxString::Format(_T("Vanishing Point (%d,%d) added"),x,y), 0);
	}

	// nevlista feltoltese
	wxString *vpArray = new wxString[vpCount];
	for (int i=0;i<vpCount;i++) vpArray[i] = wxString::Format(_T("%ld,%ld"),vanishingPoints[i][0],vanishingPoints[i][1]);

	// measure gomb aktivalasa ha lehetseges
	((MainPanel*)GetParent())->GetControlWindow()->EnableBtns();

	// az ablak letrehozasa
	wxBoxSizer *mainSizer = new wxBoxSizer(wxHORIZONTAL);

	radioBox1 = new wxRadioBox(this, RADIO_1, "VP 1:", wxDefaultPosition, wxDefaultSize, vpCount, vpArray, 1, wxRA_SPECIFY_COLS);
	radioBox2 = new wxRadioBox(this, RADIO_2, "VP 2:", wxDefaultPosition, wxDefaultSize, vpCount, vpArray, 1, wxRA_SPECIFY_COLS);
	mainSizer->Add(radioBox1, 1, wxEXPAND | wxALL, 5);
	mainSizer->Add(radioBox2, 1, wxEXPAND | wxALL, 5);

	// a sizer beallitasa
	SetSizer(mainSizer);

	int *w = new int;
	int *h = new int;
	GetSize(w,h);
//	logFile->Write(wxString::Format(_T("%d %d"),(*w)/10,(*h)/10));
	SetScrollbars(10, 10,(*w) / 10, (*h) / 10);

	delete w; delete h;
}

void RadioWindow::OnRadio1(wxCommandEvent& event) {
	int radioSel = event.GetSelection();

	selectedVP[0][0] = vanishingPoints[radioSel][0];
	selectedVP[0][1] = vanishingPoints[radioSel][1];
//	logFile->Write(wxString::Format(_T("1: (%d) %ld %ld\n"),radioSel,selectedVP[0][0],selectedVP[0][1]));

	// measure gomb aktivalasa ha lehetseges
	((MainPanel*)GetParent())->GetControlWindow()->EnableBtns();

	// rajzolo ablak frissitese ha van betoltve kep
	if (imageLoaded) ((MainPanel*)GetParent())->GetCanvas()->Refresh();
}

void RadioWindow::OnRadio2(wxCommandEvent& event) {
	int radioSel = event.GetSelection();

	selectedVP[1][0] = vanishingPoints[radioSel][0];
	selectedVP[1][1] = vanishingPoints[radioSel][1];
//	logFile->Write(wxString::Format(_T("2: (%d) %ld %ld\n"),radioSel,selectedVP[1][0],selectedVP[1][1]));

	// measure gomb aktivalasa ha lehetseges
	((MainPanel*)GetParent())->GetControlWindow()->EnableBtns();

	// rajzolo ablak frissitese ha van betoltve kep
	if (imageLoaded) ((MainPanel*)GetParent())->GetCanvas()->Refresh();
}

// ----------------------------------------------------------------------------
// ControlWindow
// ----------------------------------------------------------------------------

ControlWindow::ControlWindow(wxWindow *parent, const wxSize& size) : wxPanel(parent, -1, wxDefaultPosition, size, wxSTATIC_BORDER) {
	// az ablak letrehozasa
	wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);

	// vanishing point hozzaadas gombok
	wxBoxSizer *vpSizer = new wxBoxSizer(wxHORIZONTAL);
	btnStartAdd = new wxButton(this, BTN_STARTADD, "Add Vanishing Points");
	vpSizer->Add(btnStartAdd, 0, wxRIGHT, 3);
	btnStopAdd = new wxButton(this, BTN_STOPADD, "Stop Adding");
	vpSizer->Add(btnStopAdd, 1);

	mainSizer->Add(vpSizer, 0, wxALIGN_CENTER | wxTOP, 5);

	// elvalaszto vonal
	wxStaticLine *line0 = new wxStaticLine(this, -1, wxDefaultPosition, wxSize(200,-1), wxLI_HORIZONTAL);
	mainSizer->Add(line0, 0, wxTOP, 5);

	// referenciameret beviteli mezo
	wxBoxSizer *inputSizer = new wxBoxSizer(wxHORIZONTAL);
	wxStaticText *labelRefsize = new wxStaticText(this,-1,"Ref. object size: ");
	inputSizer->Add(labelRefsize, 0, wxTOP, 5);
	textRefsize = new wxTextCtrl(this, TEXT_REFSIZE);
	inputSizer->Add(textRefsize, 1);

	mainSizer->Add(inputSizer, 0, wxALIGN_CENTER | wxTOP, 5);

	// define ref object gomb
	btnDefref = new wxButton(this, BTN_DEFREF, "Define ref. object");
	mainSizer->Add(btnDefref, 0, wxALIGN_CENTER | wxTOP, 5);

	// elvalaszto vonal
	wxStaticLine *line1 = new wxStaticLine(this, -1, wxDefaultPosition, wxSize(200,-1), wxLI_HORIZONTAL);
	mainSizer->Add(line1, 0, wxTOP, 5);

	// measure gomb
	btnMeasure = new wxButton(this, BTN_MEASURE, "Measure");
	mainSizer->Add(btnMeasure, 0, wxALIGN_CENTER | wxALL, 5 );
	
	// elvalaszto vonal
	wxStaticLine *line2 = new wxStaticLine(this, -1, wxDefaultPosition, wxSize(200,-1), wxLI_HORIZONTAL);
	mainSizer->Add(line2, 0);

	// eredmeny kijelzo mezo
	wxBoxSizer *resultSizer = new wxBoxSizer(wxHORIZONTAL);
	wxStaticText *labelResult = new wxStaticText(this,-1,"Measurement: ");
	resultSizer->Add(labelResult, 0, wxTOP, 5);
	textResult = new wxTextCtrl(this, TEXT_RESULT, "", wxDefaultPosition, wxSize(110,-1), wxTE_RICH | wxTE_READONLY | wxTE_CENTRE);
	textResult->SetDefaultStyle(wxTextAttr(wxColour(192,0,0),wxColour(128,128,128),wxFont(10,wxDECORATIVE,wxNORMAL,wxBOLD)));
	textResult->WriteText("nothing");
	textResult->Disable();
	resultSizer->Add(textResult, 1);

	mainSizer->Add(resultSizer, 0, wxALIGN_CENTER | wxTOP, 10);

	// a sizer beallitasa
	SetSizer(mainSizer);

	EnableBtns();
}

void ControlWindow::OnTextRefsize(wxCommandEvent& event) {
	double val;
	
	if (textRefsize->GetValue().ToDouble(&val)) {
		refSize = (float)val;
	} else {
		refSize = -1.0;
	}

	// measure gomb aktivalasa ha lehetseges
	EnableBtns();
}

void ControlWindow::OnBtnStartAdd(wxCommandEvent& event) {
	addingVPs = true;
	EnableBtns();
}

void ControlWindow::OnBtnStopAdd(wxCommandEvent& event) {
	addingVPs = false;
	EnableBtns();
}

void ControlWindow::OnBtnDefref(wxCommandEvent& event) {
	// referencia objektum megadasa
	refObject[0][0] = mouseLine[0][0];
	refObject[0][1] = mouseLine[0][1];
	refObject[1][0] = mouseLine[1][0];
	refObject[1][1] = mouseLine[1][1];

	// statuszsor beallitasa
	char *str = new char[256];
	sprintf(str,"RefObj: (%d,%d)-(%d,%d) (%.2f)",refObject[0][0],refObject[0][1],refObject[1][0],refObject[1][1],refSize);
	((MainFrame*)(GetParent()->GetParent()))->SetStatusText("Reference object defined succesfully", 0);
	((MainFrame*)(GetParent()->GetParent()))->SetStatusText(str, 3);
	delete str;

	// rajzolo ablak frissitese
	((MainPanel*)GetParent())->GetCanvas()->Refresh();
	// measure gomb aktivalasa ha lehetseges
	EnableBtns();
}

void ControlWindow::OnBtnMeasure(wxCommandEvent& event) {
	Measurement *measure = new Measurement(width,height,vanishingPoints[0][0],vanishingPoints[0][1],vanishingPoints[1][0],vanishingPoints[1][1]);

	measure->setRefData(refObject[0][0],refObject[0][1],refObject[1][0],refObject[1][1],refSize);
	measure->setMeasureCoordinates(mouseLine[0][0],mouseLine[0][1],mouseLine[1][0],mouseLine[1][1]);
	textResult->SetValue(wxString::Format(_T("%.2f"),measure->calculateSize()));

	delete measure;
}

void ControlWindow::EnableBtns() {
	bool measure = true, defref = true, startadd = true, stopadd = true;

	// ref. obj. meretenek beallitasa 1-re ha meg nincs beallitva (nem kell mindig beirni ha csak aranyokat akarunk)
	if (refSize == -1.0) textRefsize->SetValue("1.0");

	// kizaro okok
	if ((selectedVP[0][0] == selectedVP[1][0]) && (selectedVP[0][1] == selectedVP[1][1])) measure=false;
	if ((refObject[0][0] == -1) || (refObject[1][0] == -1) || (refObject[0][1] == -1) || (refObject[1][1] == -1)) measure=false;
	if (!vpLoaded) measure=false;
	if (addingVPs) measure=false;

	if (!imageLoaded) defref = false;
	if (refSize == -1.0) defref = false;
	if (addingVPs) defref=false;

	if (addingVPs) startadd=false;
	if (!imageLoaded) startadd=false;
	if (!addingVPs) stopadd=false;

	btnMeasure->Enable(measure);
	btnDefref->Enable(defref);
	btnStartAdd->Enable(startadd);
	btnStopAdd->Enable(stopadd);
}

wxButton* ControlWindow::GetBtnMeasure() {
	return btnMeasure;
}

wxButton* ControlWindow::GetBtnDefref() {
	return btnDefref;
}

// ----------------------------------------------------------------------------
// MainPanel
// ----------------------------------------------------------------------------

MainPanel::MainPanel(wxFrame *parent) : wxPanel(parent,-1) {
	// az ablak letrehozasa
	wxBoxSizer *mainSizer = new wxBoxSizer(wxHORIZONTAL);

	canvas = new BitmapCanvas(this,wxDefaultSize);
	mainSizer->Add(canvas, 1, wxEXPAND);

	wxBoxSizer *rightSizer = new wxBoxSizer(wxVERTICAL);

	radioWindow = new RadioWindow(this, wxSize(200,-1));
	rightSizer->Add(radioWindow, 1, wxEXPAND | wxALL, 5);

	controlWindow = new ControlWindow(this, wxSize(190,170));
	rightSizer->Add(controlWindow, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5);

	mainSizer->Add(rightSizer, 0, wxEXPAND);

	// a sizer beallitasa
	SetSizer(mainSizer);
}

BitmapCanvas* MainPanel::GetCanvas() {
	return canvas;
}

RadioWindow* MainPanel::GetRadioWindow() {
	return radioWindow;
}

ControlWindow* MainPanel::GetControlWindow() {
	return controlWindow;
}

// ----------------------------------------------------------------------------
// MainFrame
// ----------------------------------------------------------------------------

MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size)	: wxFrame((wxFrame *)NULL, -1, title, pos, size,wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION) {
	// inicializalas
	menuBar = NULL;
	fileMenu = infoMenu = NULL;

	// kezelo raallitasa a logfilera
//	logFile = new wxFile("log.txt",wxFile::write);

	// statuszsor letrehozasa
	CreateStatusBar(5);
	int ratios[] = {-4,-3,-3,-3,-1};
	SetStatusWidths(5,ratios);
	// menu letrehozasa
	MenuBuild(1);

	// ablak tartalmanak letrehozasa
	mainPanel = new MainPanel(this);
}

void MainFrame::OnOpenImage(wxCommandEvent& event) {
	// statuszsort beallito string
	char *str = new char[256];
	// az ablak letrehozasa
	wxFileDialog *dlg = new wxFileDialog(this, "Open image","","", "Image files|*.bmp;*.jpg;*.jpeg;*.gif;*.png;*.tiff;*.tif;*.xpm;*.pcx|All files|*.*",wxOPEN, wxDefaultPosition);

	// ha a megnyitas ablak megjelent
	if ( dlg->ShowModal() == wxID_OK ) {
		// handlerek betoltese
		wxInitAllImageHandlers();
		// kep betoltese
		image.LoadFile(dlg->GetPath());

		mainPanel->GetCanvas()->SetBitmap(wxBitmap(image));

		// meretet tartalmazo valtozok beallitasa
		width = image.GetWidth();
		height = image.GetHeight();
		// statussor beallitasa
		sprintf(str,"Image: %s (%dx%d)",dlg->GetFilename(),image.GetWidth(),image.GetHeight());
		SetStatusText("Image file opened succesfully", 0);
		SetStatusText(str, 1);
	}
	delete str;
	dlg->Destroy();
}

void MainFrame::OnOpenVp(wxCommandEvent& event) {
	// statussort beallito string
	char *str = new char[256];

	// tallozo ablak letrehozasa
	wxFileDialog *dlg = new wxFileDialog(this, "Open Vanishing Points file","","", "Vanishing Points files|*.dat;*.vp|All files|*.*",wxOPEN, wxDefaultPosition);

	// ha a megnyitas ablak megjelent
	if ( dlg->ShowModal() == wxID_OK ) {
		mainPanel->GetRadioWindow()->CreateContent((dlg->GetPath()).GetData(),0,0);

		// statussor beallitasa
		sprintf(str,"VPFile: %s",dlg->GetFilename());
		SetStatusText("Vanishing Points file opened succesfully", 0);
		SetStatusText(str, 2);
	}
	delete str;
	dlg->Destroy();
}

void MainFrame::OnQuit(wxCommandEvent& event) {
	// logfile bezarasa
//	logFile->Close();
//	delete logFile;
	// foablak bezarasa
	Close(true);
}

void MainFrame::OnAbout(wxCommandEvent& event) {
	// nevjegy eloallitasa
	wxMessageBox( "Demonstration program for\n  Single View Metrology method.\n\n\tJuhasz Reka\n\tPapp Laszlo\n\tPinter Csaba\n\tSoponyai Gyorgy\n\nDate: 2005.11.26","Svm info", wxOK | wxICON_INFORMATION );
}

void MainFrame::MenuBuild(int open) {
	menuBar = new wxMenuBar();
	fileMenu = new wxMenu();
	fileMenu->Append(MENU_IMAGE_OPEN, "&Open Image...\tCtrl-O", "Open image file");
	fileMenu->Append(MENU_VPFILE_OPEN, "&Open VP File...\tCtrl-P", "Open file containing Vanishing Points");
	fileMenu->AppendSeparator();
	fileMenu->Append(MENU_FILE_QUIT, "&Quit\tAlt-F4", "Quit program");
	menuBar->Append(fileMenu, "&File");

	infoMenu = new wxMenu();
	infoMenu->Append(MENU_INFO_ABOUT, "&About", "About the program");
	menuBar->Append(infoMenu, "&Help");
	SetMenuBar(menuBar);
}

// ----------------------------------------------------------------------------
// VPFIleReader
// ----------------------------------------------------------------------------

VPFileReader::VPFileReader(const char* fileName) {
	// inicializalas
	vpFileName = (char *)fileName;

	// kezelo raallitasa a logfilera
	vpFile = new wxFile(fileName,wxFile::read);
}

VPFileReader::~VPFileReader() {
	vpFile->Close();
	delete vpFile;
}

void VPFileReader::ReadVPFile() {
	wxString str, strCp, temp;
	long tmpVp1, tmpVp2;

	// ha vannak betoltve vp-k, akkor azok torlese
	if (vanishingPoints) {
		for (int i=0;i<vpCount;i++) delete vanishingPoints[i];
		delete vanishingPoints;
	}

	// file beolvasasa stringbe
	vpFile->Read(str.GetWriteBuf(vpFile->Length()),vpFile->Length());
	strCp = str.Left(str.Length());

	// vanishing pointok szamanak meghatarozasa
	vpCount = 0;
	do {
		temp = str.BeforeFirst('\x0a');
		str = str.AfterFirst('\x0a');
		vpCount++;
	} while (temp.Length() > 0);
	vpCount--;

	// vanishing pointok kinyerese
	str = strCp.Left(strCp.Length());

	vanishingPoints = new long*[vpCount];
	for (int i=0;i<vpCount;i++) vanishingPoints[i] = new long[2];

	for (int j=0;j<vpCount;j++) {
		temp = str.BeforeFirst('\x0a');
		temp = temp.Left(temp.Length()-1);
		str = str.AfterFirst('\x0a');

		temp.BeforeFirst('\x20').ToLong(&tmpVp1,10);
		vanishingPoints[j][0] = tmpVp1;
		temp.AfterFirst('\x20').ToLong(&tmpVp2,10);
		vanishingPoints[j][1] = tmpVp2;
	}

	vpLoaded = true;
	selectedVP[0][0] = selectedVP[1][0] = vanishingPoints[0][0];
	selectedVP[0][1] = selectedVP[1][1] = vanishingPoints[0][1];
}

⌨️ 快捷键说明

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