Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MultiMarker.h"
- #include "highgui.h"
- #include "stdafx.h"
- #include "createmarker.h"
- #include <iostream>
- #include <fstream>
- using namespace std;
- using namespace alvar;
- using namespace System;
- using namespace System::IO;
- struct State {
- IplImage *img;
- stringstream filename;
- double minx, miny, maxx, maxy; // top-left and bottom-right in pixel units
- MultiMarker multi_marker;
- // General options
- bool prompt;
- double units; // how many pixels per one unit
- double marker_side_len; // marker side len in current units
- int marker_type; // 0:MarkerData, 1:ArToolkit
- double posx, posy; // The position of marker center in the given units
- int content_res;
- double margin_res;
- // MarkerData specific options
- MarkerData::MarkerContentType marker_data_content_type;
- bool marker_data_force_strong_hamming;
- State()
- : img(0),
- prompt(false),
- units(96.0/2.54), // cm assuming 96 dpi
- marker_side_len(9.0), // 9 cm
- marker_type(0),
- posx(0), posy(0),
- content_res(0), // 0 uses default
- margin_res(0.0), // 0.0 uses default (can be n*0.5)
- marker_data_content_type(MarkerData::MARKER_CONTENT_TYPE_NUMBER),
- marker_data_force_strong_hamming(false)
- {}
- ~State() {
- if (img) cvReleaseImage(&img);
- }
- void AddMarker(const char *id) {
- if (marker_type == 0) {
- MarkerData md(marker_side_len, content_res, margin_res);
- int side_len = int(marker_side_len*units+0.5);
- if (img == 0) {
- img = cvCreateImage(cvSize(side_len, side_len), IPL_DEPTH_8U, 1);
- filename.str("");
- filename<<"MarkerData";
- minx = (posx*units) - (marker_side_len*units/2.0);
- miny = (posy*units) - (marker_side_len*units/2.0);
- maxx = (posx*units) + (marker_side_len*units/2.0);
- maxy = (posy*units) + (marker_side_len*units/2.0);
- } else {
- double new_minx = (posx*units) - (marker_side_len*units/2.0);
- double new_miny = (posy*units) - (marker_side_len*units/2.0);
- double new_maxx = (posx*units) + (marker_side_len*units/2.0);
- double new_maxy = (posy*units) + (marker_side_len*units/2.0);
- if (minx < new_minx) new_minx = minx;
- if (miny < new_miny) new_miny = miny;
- if (maxx > new_maxx) new_maxx = maxx;
- if (maxy > new_maxy) new_maxy = maxy;
- IplImage *new_img = cvCreateImage(cvSize(int(new_maxx-new_minx+0.5), int(new_maxy-new_miny+0.5)), IPL_DEPTH_8U, 1);
- cvSet(new_img, cvScalar(255));
- CvRect roi = cvRect(int(minx-new_minx+0.5), int(miny-new_miny+0.5), img->width, img->height);
- cvSetImageROI(new_img, roi);
- cvCopy(img, new_img);
- cvReleaseImage(&img);
- img = new_img;
- roi.x = int((posx*units) - (marker_side_len*units/2.0) - new_minx + 0.5);
- roi.y = int((posy*units) - (marker_side_len*units/2.0) - new_miny + 0.5);
- roi.width = int(marker_side_len*units+0.5); roi.height = int(marker_side_len*units+0.5);
- cvSetImageROI(img, roi);
- minx = new_minx; miny = new_miny;
- maxx = new_maxx; maxy = new_maxy;
- }
- if (marker_data_content_type == MarkerData::MARKER_CONTENT_TYPE_NUMBER) {
- int idi = atoi(id);
- md.SetContent(marker_data_content_type, idi, 0);
- if (filename.str().length()<64) filename<<"_"<<idi;
- Pose pose;
- pose.Reset();
- pose.SetTranslation(posx, -posy, 0);
- multi_marker.PointCloudAdd(idi, marker_side_len, pose);
- } else {
- md.SetContent(marker_data_content_type, 0, id);
- const char *p = id;
- int counter=0;
- filename<<"_";
- while(*p) {
- if (!isalnum(*p)) filename<<"_";
- else filename<<(char)tolower(*p);
- p++; counter++;
- if (counter > 8) break;
- }
- }
- md.ScaleMarkerToImage(img);
- cvResetImageROI(img);
- }
- else if (marker_type == 1) {
- // Create and save MarkerArtoolkit marker (Note, that this doesn't support multi markers)
- MarkerArtoolkit md(marker_side_len, content_res, margin_res);
- int side_len = int(marker_side_len*units+0.5);
- if (img != 0) cvReleaseImage(&img);
- img = cvCreateImage(cvSize(side_len, side_len), IPL_DEPTH_8U, 1);
- filename.str("");
- filename<<"MarkerArtoolkit";
- md.SetContent(atoi(id));
- filename<<"_"<<atoi(id);
- md.ScaleMarkerToImage(img);
- }
- }
- void Save() {
- if (img) {
- std::stringstream filenamexml;
- filenamexml<<filename.str()<<".xml";
- filename<<".png";
- std::cout<<"Saving: "<<filename.str()<<std::endl;
- cvSaveImage(filename.str().c_str(), img);
- if (multi_marker.Size() > 1) {
- std::cout<<"Saving: "<<filenamexml.str()<<std::endl;
- multi_marker.Save(filenamexml.str().c_str(), alvar::FILE_FORMAT_XML);
- }
- }
- }
- } st;
- namespace createmarker {
- int createMarkers::createOneMarker(char *VCID_in)
- {
- try {
- // String-type marker -> arbitrary-size based on length of the string
- // this makes huge markers
- // VCID-string should be encoded somehow to make it shorter
- // as VCID is actually hex, it contains only chars 0-0, a-f
- std::string VCIDhex = VCID_in;
- ofstream filelist("list.txt", ios::app);
- if(filelist)
- {
- filelist << VCIDhex << " ";
- filelist.close();
- }
- else
- cerr << "Error on the opening of list.txt." << endl;
- //La conversion est'elle bien réalisée ?
- int A=0;
- wchar_t TableTemp[32];
- char TableBin[113];
- while (A<28)
- {
- TableTemp[A]=VCIDhex[A];
- A++;
- }
- A=0;
- while (A<28)
- {
- if (TableTemp[A] == 0)
- {
- TableBin[4*A+0]= 0;
- TableBin[4*A+1]= 0;
- TableBin[4*A+2]= 0;
- TableBin[4*A+3]= 0;
- }
- if (TableTemp[A] == 1)
- {
- TableBin[4*A+0]= 0;
- TableBin[4*A+1]= 0;
- TableBin[4*A+2]= 0;
- TableBin[4*A+3]= 1;
- }
- if (TableTemp[A] == 2)
- {
- TableBin[4*A+0]= 0;
- TableBin[4*A+1]= 0;
- TableBin[4*A+2]= 1;
- TableBin[4*A+3]= 0;
- }
- if (TableTemp[A] == 3)
- {
- TableBin[4*A+0]= 0;
- TableBin[4*A+1]= 0;
- TableBin[4*A+2]= 1;
- TableBin[4*A+3]= 1;
- }
- if (TableTemp[A] == 4)
- {
- TableBin[4*A+0]= 0;
- TableBin[4*A+1]= 1;
- TableBin[4*A+2]= 0;
- TableBin[4*A+3]= 0;
- }
- if (TableTemp[A] == 5)
- {
- TableBin[4*A+0]= 0;
- TableBin[4*A+1]= 1;
- TableBin[4*A+2]= 0;
- TableBin[4*A+3]= 1;
- }
- if (TableTemp[A] == 6)
- {
- TableBin[4*A+0]= 0;
- TableBin[4*A+1]= 1;
- TableBin[4*A+2]= 1;
- TableBin[4*A+3]= 0;
- }
- if (TableTemp[A] == 7)
- {
- TableBin[4*A+0]= 0;
- TableBin[4*A+1]= 1;
- TableBin[4*A+2]= 1;
- TableBin[4*A+3]= 1;
- }
- if (TableTemp[A] == 8)
- {
- TableBin[4*A+0]= 1;
- TableBin[4*A+1]= 0;
- TableBin[4*A+2]= 0;
- TableBin[4*A+3]= 0;
- }
- if (TableTemp[A] == 9)
- {
- TableBin[4*A+0]= 1;
- TableBin[4*A+1]= 0;
- TableBin[4*A+2]= 0;
- TableBin[4*A+3]= 1;
- }
- if (TableTemp[A] == 'a')
- {
- TableBin[4*A+0]= 1;
- TableBin[4*A+1]= 0;
- TableBin[4*A+2]= 1;
- TableBin[4*A+3]= 0;
- }
- if (TableTemp[A] == 'b')
- {
- TableBin[4*A+0]= 1;
- TableBin[4*A+1]= 0;
- TableBin[4*A+2]= 1;
- TableBin[4*A+3]= 1;
- }
- if (TableTemp[A] == 'c')
- {
- TableBin[4*A+0]= 1;
- TableBin[4*A+1]= 1;
- TableBin[4*A+2]= 0;
- TableBin[4*A+3]= 0;
- }
- if (TableTemp[A] == 'd')
- {
- TableBin[4*A+0]= 1;
- TableBin[4*A+1]= 1;
- TableBin[4*A+2]= 0;
- TableBin[4*A+3]= 1;
- }
- if (TableTemp[A] == 'e')
- {
- TableBin[4*A+0]= 1;
- TableBin[4*A+1]= 1;
- TableBin[4*A+2]= 1;
- TableBin[4*A+3]= 0;
- }
- if (TableTemp[A] == 'f')
- {
- TableBin[4*A+0]= 1;
- TableBin[4*A+1]= 1;
- TableBin[4*A+2]= 1;
- TableBin[4*A+3]= 1;
- }
- if (TableTemp[A] == '-')
- {
- //TableBin[A]="-";
- }
- A++;
- }
- TableBin[113]='\0';
- std::string *VCIDbin = new string(TableBin);
- //////////////////////////////A REVOIR
- const char *VCID_in2 = VCIDbin->c_str();
- /////////////////////////////////////////
- st.marker_data_content_type = MarkerData::MARKER_CONTENT_TYPE_STRING;
- st.AddMarker(VCID_in2);
- ofstream filelist2("list.txt", ios::app);
- if(filelist2)
- {
- filelist2 << VCID_in2 <<endl;
- filelist2.close();
- }
- else
- cerr << "Error on the opening of list.txt." << endl;
- st.Save();
- return 1;
- }
- catch (const std::exception & ) {
- // std::cout << "Exception: " << e.what() << endl;
- return -1;
- }
- catch (...) {
- // std::cout << "Exception: unknown" << std::endl;
- return -2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement