Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. #include "MultiMarker.h"
  2. #include "highgui.h"
  3. #include "stdafx.h"
  4. #include "createmarker.h"
  5. #include <iostream>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9. using namespace alvar;
  10.  
  11. using namespace System;
  12. using namespace System::IO;
  13.  
  14. struct State {
  15. IplImage *img;
  16. stringstream filename;
  17. double minx, miny, maxx, maxy; // top-left and bottom-right in pixel units
  18. MultiMarker multi_marker;
  19.  
  20. // General options
  21. bool prompt;
  22. double units; // how many pixels per one unit
  23. double marker_side_len; // marker side len in current units
  24. int marker_type; // 0:MarkerData, 1:ArToolkit
  25. double posx, posy; // The position of marker center in the given units
  26. int content_res;
  27. double margin_res;
  28.  
  29. // MarkerData specific options
  30. MarkerData::MarkerContentType marker_data_content_type;
  31. bool marker_data_force_strong_hamming;
  32.  
  33. State()
  34. : img(0),
  35. prompt(false),
  36. units(96.0/2.54), // cm assuming 96 dpi
  37. marker_side_len(9.0), // 9 cm
  38. marker_type(0),
  39. posx(0), posy(0),
  40. content_res(0), // 0 uses default
  41. margin_res(0.0), // 0.0 uses default (can be n*0.5)
  42. marker_data_content_type(MarkerData::MARKER_CONTENT_TYPE_NUMBER),
  43. marker_data_force_strong_hamming(false)
  44. {}
  45. ~State() {
  46. if (img) cvReleaseImage(&img);
  47. }
  48. void AddMarker(const char *id) {
  49. if (marker_type == 0) {
  50. MarkerData md(marker_side_len, content_res, margin_res);
  51. int side_len = int(marker_side_len*units+0.5);
  52. if (img == 0) {
  53. img = cvCreateImage(cvSize(side_len, side_len), IPL_DEPTH_8U, 1);
  54. filename.str("");
  55. filename<<"MarkerData";
  56. minx = (posx*units) - (marker_side_len*units/2.0);
  57. miny = (posy*units) - (marker_side_len*units/2.0);
  58. maxx = (posx*units) + (marker_side_len*units/2.0);
  59. maxy = (posy*units) + (marker_side_len*units/2.0);
  60. } else {
  61. double new_minx = (posx*units) - (marker_side_len*units/2.0);
  62. double new_miny = (posy*units) - (marker_side_len*units/2.0);
  63. double new_maxx = (posx*units) + (marker_side_len*units/2.0);
  64. double new_maxy = (posy*units) + (marker_side_len*units/2.0);
  65. if (minx < new_minx) new_minx = minx;
  66. if (miny < new_miny) new_miny = miny;
  67. if (maxx > new_maxx) new_maxx = maxx;
  68. if (maxy > new_maxy) new_maxy = maxy;
  69. IplImage *new_img = cvCreateImage(cvSize(int(new_maxx-new_minx+0.5), int(new_maxy-new_miny+0.5)), IPL_DEPTH_8U, 1);
  70. cvSet(new_img, cvScalar(255));
  71. CvRect roi = cvRect(int(minx-new_minx+0.5), int(miny-new_miny+0.5), img->width, img->height);
  72. cvSetImageROI(new_img, roi);
  73. cvCopy(img, new_img);
  74. cvReleaseImage(&img);
  75. img = new_img;
  76. roi.x = int((posx*units) - (marker_side_len*units/2.0) - new_minx + 0.5);
  77. roi.y = int((posy*units) - (marker_side_len*units/2.0) - new_miny + 0.5);
  78. roi.width = int(marker_side_len*units+0.5); roi.height = int(marker_side_len*units+0.5);
  79. cvSetImageROI(img, roi);
  80. minx = new_minx; miny = new_miny;
  81. maxx = new_maxx; maxy = new_maxy;
  82. }
  83. if (marker_data_content_type == MarkerData::MARKER_CONTENT_TYPE_NUMBER) {
  84. int idi = atoi(id);
  85. md.SetContent(marker_data_content_type, idi, 0);
  86. if (filename.str().length()<64) filename<<"_"<<idi;
  87.  
  88. Pose pose;
  89. pose.Reset();
  90. pose.SetTranslation(posx, -posy, 0);
  91. multi_marker.PointCloudAdd(idi, marker_side_len, pose);
  92. } else {
  93. md.SetContent(marker_data_content_type, 0, id);
  94. const char *p = id;
  95. int counter=0;
  96. filename<<"_";
  97. while(*p) {
  98. if (!isalnum(*p)) filename<<"_";
  99. else filename<<(char)tolower(*p);
  100. p++; counter++;
  101. if (counter > 8) break;
  102. }
  103. }
  104. md.ScaleMarkerToImage(img);
  105. cvResetImageROI(img);
  106. }
  107. else if (marker_type == 1) {
  108. // Create and save MarkerArtoolkit marker (Note, that this doesn't support multi markers)
  109. MarkerArtoolkit md(marker_side_len, content_res, margin_res);
  110. int side_len = int(marker_side_len*units+0.5);
  111. if (img != 0) cvReleaseImage(&img);
  112. img = cvCreateImage(cvSize(side_len, side_len), IPL_DEPTH_8U, 1);
  113. filename.str("");
  114. filename<<"MarkerArtoolkit";
  115. md.SetContent(atoi(id));
  116. filename<<"_"<<atoi(id);
  117. md.ScaleMarkerToImage(img);
  118. }
  119. }
  120. void Save() {
  121. if (img) {
  122. std::stringstream filenamexml;
  123. filenamexml<<filename.str()<<".xml";
  124. filename<<".png";
  125. std::cout<<"Saving: "<<filename.str()<<std::endl;
  126. cvSaveImage(filename.str().c_str(), img);
  127. if (multi_marker.Size() > 1) {
  128. std::cout<<"Saving: "<<filenamexml.str()<<std::endl;
  129. multi_marker.Save(filenamexml.str().c_str(), alvar::FILE_FORMAT_XML);
  130. }
  131. }
  132. }
  133. } st;
  134.  
  135.  
  136. namespace createmarker {
  137. int createMarkers::createOneMarker(char *VCID_in)
  138. {
  139. try {
  140.  
  141. // String-type marker -> arbitrary-size based on length of the string
  142. // this makes huge markers
  143. // VCID-string should be encoded somehow to make it shorter
  144. // as VCID is actually hex, it contains only chars 0-0, a-f
  145.  
  146. std::string VCIDhex = VCID_in;
  147. ofstream filelist("list.txt", ios::app);
  148. if(filelist)
  149. {
  150. filelist << VCIDhex << " ";
  151. filelist.close();
  152. }
  153. else
  154. cerr << "Error on the opening of list.txt." << endl;
  155.  
  156. //La conversion est'elle bien réalisée ?
  157. int A=0;
  158. wchar_t TableTemp[32];
  159. char TableBin[113];
  160. while (A<28)
  161. {
  162. TableTemp[A]=VCIDhex[A];
  163. A++;
  164. }
  165. A=0;
  166. while (A<28)
  167. {
  168. if (TableTemp[A] == 0)
  169. {
  170. TableBin[4*A+0]= 0;
  171. TableBin[4*A+1]= 0;
  172. TableBin[4*A+2]= 0;
  173. TableBin[4*A+3]= 0;
  174. }
  175. if (TableTemp[A] == 1)
  176. {
  177. TableBin[4*A+0]= 0;
  178. TableBin[4*A+1]= 0;
  179. TableBin[4*A+2]= 0;
  180. TableBin[4*A+3]= 1;
  181. }
  182. if (TableTemp[A] == 2)
  183. {
  184. TableBin[4*A+0]= 0;
  185. TableBin[4*A+1]= 0;
  186. TableBin[4*A+2]= 1;
  187. TableBin[4*A+3]= 0;
  188. }
  189. if (TableTemp[A] == 3)
  190. {
  191. TableBin[4*A+0]= 0;
  192. TableBin[4*A+1]= 0;
  193. TableBin[4*A+2]= 1;
  194. TableBin[4*A+3]= 1;
  195. }
  196. if (TableTemp[A] == 4)
  197. {
  198. TableBin[4*A+0]= 0;
  199. TableBin[4*A+1]= 1;
  200. TableBin[4*A+2]= 0;
  201. TableBin[4*A+3]= 0;
  202. }
  203. if (TableTemp[A] == 5)
  204. {
  205. TableBin[4*A+0]= 0;
  206. TableBin[4*A+1]= 1;
  207. TableBin[4*A+2]= 0;
  208. TableBin[4*A+3]= 1;
  209. }
  210. if (TableTemp[A] == 6)
  211. {
  212. TableBin[4*A+0]= 0;
  213. TableBin[4*A+1]= 1;
  214. TableBin[4*A+2]= 1;
  215. TableBin[4*A+3]= 0;
  216. }
  217. if (TableTemp[A] == 7)
  218. {
  219. TableBin[4*A+0]= 0;
  220. TableBin[4*A+1]= 1;
  221. TableBin[4*A+2]= 1;
  222. TableBin[4*A+3]= 1;
  223. }
  224. if (TableTemp[A] == 8)
  225. {
  226. TableBin[4*A+0]= 1;
  227. TableBin[4*A+1]= 0;
  228. TableBin[4*A+2]= 0;
  229. TableBin[4*A+3]= 0;
  230. }
  231. if (TableTemp[A] == 9)
  232. {
  233. TableBin[4*A+0]= 1;
  234. TableBin[4*A+1]= 0;
  235. TableBin[4*A+2]= 0;
  236. TableBin[4*A+3]= 1;
  237. }
  238. if (TableTemp[A] == 'a')
  239. {
  240. TableBin[4*A+0]= 1;
  241. TableBin[4*A+1]= 0;
  242. TableBin[4*A+2]= 1;
  243. TableBin[4*A+3]= 0;
  244. }
  245. if (TableTemp[A] == 'b')
  246. {
  247. TableBin[4*A+0]= 1;
  248. TableBin[4*A+1]= 0;
  249. TableBin[4*A+2]= 1;
  250. TableBin[4*A+3]= 1;
  251. }
  252. if (TableTemp[A] == 'c')
  253. {
  254. TableBin[4*A+0]= 1;
  255. TableBin[4*A+1]= 1;
  256. TableBin[4*A+2]= 0;
  257. TableBin[4*A+3]= 0;
  258. }
  259. if (TableTemp[A] == 'd')
  260. {
  261. TableBin[4*A+0]= 1;
  262. TableBin[4*A+1]= 1;
  263. TableBin[4*A+2]= 0;
  264. TableBin[4*A+3]= 1;
  265. }
  266. if (TableTemp[A] == 'e')
  267. {
  268. TableBin[4*A+0]= 1;
  269. TableBin[4*A+1]= 1;
  270. TableBin[4*A+2]= 1;
  271. TableBin[4*A+3]= 0;
  272. }
  273. if (TableTemp[A] == 'f')
  274. {
  275. TableBin[4*A+0]= 1;
  276. TableBin[4*A+1]= 1;
  277. TableBin[4*A+2]= 1;
  278. TableBin[4*A+3]= 1;
  279. }
  280. if (TableTemp[A] == '-')
  281. {
  282. //TableBin[A]="-";
  283. }
  284. A++;
  285. }
  286. TableBin[113]='\0';
  287. std::string *VCIDbin = new string(TableBin);
  288. //////////////////////////////A REVOIR
  289.  
  290. const char *VCID_in2 = VCIDbin->c_str();
  291.  
  292.  
  293. /////////////////////////////////////////
  294. st.marker_data_content_type = MarkerData::MARKER_CONTENT_TYPE_STRING;
  295. st.AddMarker(VCID_in2);
  296. ofstream filelist2("list.txt", ios::app);
  297. if(filelist2)
  298. {
  299. filelist2 << VCID_in2 <<endl;
  300. filelist2.close();
  301. }
  302. else
  303. cerr << "Error on the opening of list.txt." << endl;
  304.  
  305. st.Save();
  306. return 1;
  307.  
  308. }
  309.  
  310.  
  311. catch (const std::exception & ) {
  312. // std::cout << "Exception: " << e.what() << endl;
  313. return -1;
  314. }
  315. catch (...) {
  316. // std::cout << "Exception: unknown" << std::endl;
  317. return -2;
  318. }
  319. }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement