PageRenderTime 162ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/vector0.2/vector.cpp

https://github.com/qiqidone/Vectorization
C++ | 321 lines | 248 code | 47 blank | 26 comment | 39 complexity | 6058c92bac538c3507dbff06b5327306 MD5 | raw file
  1. #include "vector.h"
  2. #include <iostream>
  3. /**************** class Vdata ******************/
  4. Vdata::Vdata()
  5. {
  6. m_source = NULL;
  7. m_gray = NULL;
  8. m_edge = NULL;
  9. }
  10. Vdata::~Vdata()
  11. {
  12. if(m_source != NULL)
  13. {
  14. cvReleaseImage( &m_source );
  15. m_source = NULL;
  16. }
  17. if(m_gray != NULL)
  18. {
  19. cvReleaseImage( &m_gray );
  20. m_gray = NULL;
  21. }
  22. if(m_edge != NULL)
  23. {
  24. cvReleaseImage( &m_edge );
  25. m_edge = NULL;
  26. }
  27. if(m_dst != NULL)
  28. {
  29. cvReleaseImage( &m_dst );
  30. m_dst = NULL;
  31. }
  32. }
  33. // 数据清空
  34. bool Vdata::initial()
  35. {
  36. if(m_source != NULL)
  37. {
  38. cvReleaseImage( &m_source );
  39. m_source = NULL;
  40. }
  41. if(m_gray != NULL)
  42. {
  43. cvReleaseImage( &m_gray );
  44. m_gray = NULL;
  45. }
  46. if(m_edge != NULL)
  47. {
  48. cvReleaseImage( &m_edge );
  49. m_edge = NULL;
  50. }
  51. if(m_dst != NULL)
  52. {
  53. cvReleaseImage( &m_dst );
  54. m_dst = NULL;
  55. }
  56. printf("%s", __FUNCTION__);
  57. return true;
  58. }
  59. bool Vdata::initial(const char* filename)
  60. {
  61. // 先清空
  62. initial();
  63. // source
  64. m_source = cvLoadImage(filename, 1);
  65. // gray
  66. m_gray = cvCreateImage(cvGetSize(m_source), 8, 1);
  67. cvCvtColor(m_source, m_gray, CV_BGR2GRAY);
  68. //edge
  69. m_edge = cvCreateImage(cvGetSize(m_source), 8, 1);
  70. cvCanny(m_gray, m_edge, 50, 150);
  71. //dst
  72. m_dst = cvCreateImage(cvGetSize(m_source), 8, 3);
  73. return true;
  74. }
  75. /**************** class Vdata end...... ******************/
  76. /**************** class Vgui ******************/
  77. Vgui::Vgui()
  78. {
  79. design();
  80. }
  81. Vgui::~Vgui()
  82. {
  83. }
  84. // 设计Gui
  85. bool Vgui::design()
  86. {
  87. fileButton = new QPushButton(tr("Open"));
  88. runButton = new QPushButton(tr("Run Fill"));
  89. meshButton = new QPushButton(tr("Mesh"));
  90. testButton = new QPushButton(tr("Test"));
  91. saveButton = new QPushButton(tr("Save"));
  92. // new完毕,开始布局
  93. QVBoxLayout* mainLayout = new QVBoxLayout();
  94. mainLayout->addWidget(fileButton);
  95. mainLayout->addWidget(runButton);
  96. mainLayout->addWidget(meshButton);
  97. mainLayout->addWidget(testButton);
  98. mainLayout->addWidget(saveButton);
  99. // main Layout
  100. setLayout(mainLayout);
  101. setWindowTitle(tr("Vectorize "));
  102. // conncet ?
  103. return true;
  104. }
  105. void Vgui::run()
  106. {
  107. show();
  108. }
  109. /**************** class Vgui end...... ******************/
  110. /*************** class Vshader ********************/
  111. /******************** 多线程 的设计 不会全部无响应 主窗口无响应************************/
  112. Vshader::Vshader()
  113. {
  114. source = NULL;
  115. edge = NULL;
  116. dst = NULL;
  117. }
  118. Vshader::~Vshader()
  119. {
  120. if(edge != NULL)
  121. {
  122. cvReleaseImage( &edge );
  123. edge = NULL;
  124. }
  125. if(source != NULL)
  126. {
  127. cvReleaseImage( &source );
  128. source = NULL;
  129. }
  130. if(dst != NULL)
  131. {
  132. cvReleaseImage( &dst );
  133. dst = NULL;
  134. }
  135. }
  136. void Vshader::run(int mode)
  137. {
  138. switch(mode)
  139. {
  140. case 1:
  141. shader.trapBall(source, dst);
  142. break;
  143. case 2:
  144. common::delaunay( edge, dst, source );
  145. break;
  146. case 3:
  147. shader.mesh( source, dst );
  148. break;
  149. case 4:
  150. common::test( );
  151. break;
  152. default:
  153. cout << "Mode Error" << endl;
  154. break;
  155. }
  156. exec();
  157. }
  158. bool Vshader::initial(IplImage* img, IplImage* _edge)
  159. {
  160. if(img == NULL || _edge == NULL)
  161. return false;
  162. if(source != NULL)
  163. {
  164. cvReleaseImage(&source);
  165. source = NULL;
  166. }
  167. source = cvCreateImage(cvGetSize(img), 8, 3);
  168. cvCopy(img, source);
  169. if(edge != NULL)
  170. {
  171. cvReleaseImage(&edge);
  172. edge = NULL;
  173. }
  174. edge = cvCreateImage(cvGetSize(_edge), 8, 1);
  175. cvCopy(_edge, edge);
  176. if(dst != NULL)
  177. {
  178. cvReleaseImage( &dst );
  179. dst = NULL;
  180. }
  181. dst = cvCreateImage(cvGetSize(img), 8, 3);
  182. return shader.initial(edge);
  183. }
  184. /*************** class Vshader end.......********************/
  185. // ==
  186. // || Vectorize 2.0 ~ 《设计模式》+ 《Thinking in C++ 第二卷》 +《从小工到专家》
  187. // ||
  188. /**************** class Vectorize . ******************/
  189. Vectorize::Vectorize()
  190. {
  191. vconncet();
  192. m_gui.run();
  193. }
  194. Vectorize::~Vectorize()
  195. {
  196. }
  197. bool Vectorize::vconncet()
  198. {
  199. connect(m_gui.fileButton, SIGNAL(clicked()), this, SLOT(open()));
  200. connect(m_gui.runButton, SIGNAL(clicked()), this, SLOT(run()));
  201. connect(m_gui.meshButton, SIGNAL(clicked()), this, SLOT(delaunay()));
  202. connect(m_gui.testButton, SIGNAL(clicked()), this, SLOT(test()));
  203. connect(m_gui.saveButton, SIGNAL(clicked()), this, SLOT(save()));
  204. return true;
  205. }
  206. bool Vectorize::open()
  207. {
  208. QString fileName = QFileDialog::getOpenFileName(&m_gui, tr("Open Image File"), NULL, tr("Images (*.jpg *.png *.bmp)"));
  209. if(!fileName.isEmpty())
  210. {
  211. /* 将QSTRING 类型转换为char* */
  212. QByteArray temp = fileName.toLocal8Bit();
  213. const char* c_str = temp.data();
  214. if( m_data.initial(c_str) )
  215. {
  216. if( !m_shader.initial(m_data.m_source, m_data.m_edge) )
  217. {
  218. QMessageBox::about(NULL, "Error", " Shader Initial Error");
  219. }
  220. }
  221. else
  222. {
  223. QMessageBox::about(NULL, "Error", "Vdata initial error");
  224. }
  225. // assert 断言:用于绝不可能的事,异常用于异常情况
  226. }
  227. cvShowImage("img", m_data.m_source);
  228. // open 的同时会导出一份obj
  229. common::export_to_obj(m_data.m_gray);
  230. return true;
  231. }
  232. bool Vectorize::run()
  233. {
  234. if( ! m_shader.initial(m_data.m_source, m_data.m_edge) )
  235. {
  236. QMessageBox::about(NULL, "Error", "No data? ");
  237. return false;
  238. }
  239. m_shader.run(1);
  240. m_shader.quit();
  241. return true;
  242. }
  243. bool
  244. Vectorize::delaunay()
  245. {
  246. m_shader.run(2);
  247. m_shader.quit();
  248. return true;
  249. }
  250. bool Vectorize::test()
  251. {
  252. m_shader.run(4);
  253. m_shader.quit();
  254. return true;
  255. }
  256. bool Vectorize::save()
  257. {
  258. if(m_data.m_dst == NULL)
  259. {
  260. QMessageBox::about(NULL, "Error", "No data? ");
  261. return false;
  262. }
  263. QString fileName = QFileDialog::getSaveFileName(&m_gui, tr("Save Image File"), NULL, tr("Images (*.jpg *.png *.bmp)"));
  264. if(!fileName.isEmpty())
  265. {
  266. /* 将QSTRING 类型转换为char* */
  267. QByteArray temp = fileName.toLocal8Bit();
  268. const char* c_str = temp.data();
  269. cvSaveImage(c_str, m_data.m_dst);
  270. return true;
  271. }
  272. return false;
  273. }
  274. /**************** class Vectorize .end ...... ******************/