graft_cv_api.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. #include <string.h>
  2. #include <fstream>
  3. #include "graft_cv_api.h"
  4. #include "data_def.h"
  5. #include "data_def_api.h"
  6. #include "config.h"
  7. #include "optimal_angle.h"
  8. #include "cut_point_rs.h"
  9. #include "cut_point_sc.h"
  10. #include "logger.h"
  11. #include "utils.h"
  12. #include "imstorage_manager.h"
  13. #include "cut_point_rs_reid.h"
  14. #include "grab_point_rs.h"
  15. #include "chessboard_calibration.h"
  16. extern CRITICAL_SECTION g_cs;
  17. namespace graft_cv
  18. {
  19. char *g_version_str = "0.7.11";
  20. //configure
  21. string g_conf_file = "./gcv_conf.yml";
  22. ConfigParam g_cp;
  23. //logger
  24. ofstream g_logger_ofs;
  25. CGcvLogger g_logger = CGcvLogger(
  26. g_logger_ofs,
  27. CGcvLogger::file_and_terminal,
  28. CGcvLogger::debug,
  29. "./gcv.log");
  30. //image saver
  31. CImStoreManager* g_pImStore=0;
  32. //implement
  33. //COptimalAnglePart g_oa = COptimalAnglePart(g_cp,&g_logger);
  34. CCotyledonAngle g_oa = CCotyledonAngle(g_cp,&g_logger);
  35. CRootStockCutPoint g_rs_cp = CRootStockCutPoint(g_cp,&g_logger);
  36. CRootStockCutPointReid g_rs_cp_reid = CRootStockCutPointReid(g_cp,&g_logger);
  37. CScionCutPoint g_sc_cp = CScionCutPoint(g_cp,&g_logger);
  38. CRootStockGrabPoint g_sola_grab_rs(g_cp, &g_logger);
  39. CRootStockGrabPoint g_sola_grab_sc(g_cp, &g_logger);
  40. CSolaCutPointReid g_sola_reid_rs = CSolaCutPointReid(g_cp, 1, &g_logger);
  41. CSolaCutPointReid g_sola_reid_sc = CSolaCutPointReid(g_cp, 0, &g_logger);
  42. CChessboardCalibration g_chessboard = CChessboardCalibration(g_cp, &g_logger);
  43. //??¶àÏß³ÌÓÐÎÊÌâ
  44. map<string, cv::Mat> g_img_cache;
  45. //0 log path
  46. int cv_set_logpath(char*lpath)
  47. {
  48. try{
  49. string mp = g_logger.getPath();
  50. string np(lpath);
  51. if(mp==np){
  52. return 0;
  53. }
  54. g_logger_ofs.close();
  55. g_logger_ofs.open(lpath,ofstream::out | ofstream::app);
  56. string tmp = currTime() +" [WELCOME] ===========start logger===========\n";
  57. g_logger_ofs<<tmp;
  58. g_logger_ofs.flush();
  59. cout<<tmp<<endl;
  60. g_logger.setPath(string(lpath));
  61. return 0;
  62. }
  63. catch(...){
  64. g_logger.ERRORINFO("set log path failed");
  65. return 1;
  66. }
  67. }
  68. // 0-debug, 1-info, 2-warning, 3-error
  69. int cv_set_loglevel(int lev)
  70. {
  71. if(lev <0 || lev>3){
  72. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  73. return 1;
  74. }
  75. try{
  76. switch(lev){
  77. case 0:
  78. g_logger.setLevel(CGcvLogger::debug);
  79. break;
  80. case 1:
  81. g_logger.setLevel(CGcvLogger::info);
  82. break;
  83. case 2:
  84. g_logger.setLevel(CGcvLogger::warning);
  85. break;
  86. case 3:
  87. g_logger.setLevel(CGcvLogger::error);
  88. break;
  89. default:
  90. g_logger.ERRORINFO("log level error: should in [0,1,2,3] 0-debug, 1-info, 2-warning, 3-error");
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. catch(...){
  96. g_logger.ERRORINFO("set log level failed");
  97. return 1;
  98. }
  99. }
  100. int cv_init_image_saver()
  101. {
  102. if( g_cp.image_save){
  103. if(g_pImStore){
  104. string folder;
  105. g_pImStore->getStoreDir(folder);
  106. if(folder!=g_cp.image_depository){
  107. delete g_pImStore;
  108. g_pImStore = new CImStoreManager();
  109. g_pImStore->setStoreDir(g_cp.image_depository);
  110. g_pImStore->setStoreDays(g_cp.image_backup_days);
  111. }
  112. }
  113. else{
  114. g_pImStore = new CImStoreManager();
  115. g_pImStore->setStoreDir(g_cp.image_depository);
  116. g_pImStore->setStoreDays(g_cp.image_backup_days);
  117. }
  118. }
  119. else{
  120. if(g_pImStore){
  121. delete g_pImStore;
  122. g_pImStore=0;
  123. }
  124. }
  125. g_oa.set_image_saver(&g_pImStore);
  126. g_rs_cp.set_image_saver(&g_pImStore);
  127. g_sc_cp.set_image_saver(&g_pImStore);
  128. g_sola_grab_rs.set_image_saver(&g_pImStore);
  129. g_sola_grab_sc.set_image_saver(&g_pImStore);
  130. g_sola_reid_rs.set_image_saver(&g_pImStore);
  131. g_sola_reid_sc.set_image_saver(&g_pImStore);
  132. return 0;
  133. }
  134. GCV_API int cv_release()
  135. {
  136. if(g_pImStore){
  137. delete g_pImStore;
  138. g_pImStore = 0;
  139. }
  140. //DeleteCriticalSection(&g_cs);
  141. return 0;
  142. }
  143. //1
  144. int cv_init(char*conf)
  145. {
  146. //InitializeCriticalSection(&g_cs);
  147. CGCvConfig conf_contrainer = CGCvConfig();
  148. conf_contrainer.setConfParam(&g_cp);
  149. if(conf){
  150. //read configures
  151. ifstream ifs(conf);
  152. if(!ifs.good()){return 1;}
  153. ifs.close();
  154. memset(&g_cp,0,sizeof(ConfigParam));
  155. cv::FileStorage fs(conf, cv::FileStorage::READ);
  156. conf_contrainer.read(fs["conf_parameters"]);
  157. fs.release();
  158. g_conf_file = conf;
  159. }
  160. else{
  161. ifstream ifs(g_conf_file);
  162. if(!ifs.good()){return 1;}
  163. ifs.close();
  164. memset(&g_cp,0,sizeof(ConfigParam));
  165. //read configures
  166. cv::FileStorage fs(g_conf_file, cv::FileStorage::READ);
  167. conf_contrainer.read(fs["conf_parameters"]);
  168. fs.release();
  169. }
  170. string pinfo = get_cparam_info(&g_cp);
  171. g_logger.INFO(string("lib version: ")+string(g_version_str));
  172. g_logger.INFO(string("load parameters:\n")+pinfo);
  173. return 0;
  174. };
  175. //2
  176. void cv_set_param(ConfigParam&cp)
  177. {
  178. g_cp = cp;
  179. string pinfo = get_cparam_info(&g_cp);
  180. g_logger.INFO(string("set parameters:\n")+pinfo);
  181. };
  182. int cv_set_param_from_file(char*conf)
  183. {
  184. if (conf == 0) {
  185. return 1;
  186. }
  187. ConfigParam cp;
  188. CGCvConfig conf_contrainer = CGCvConfig();
  189. conf_contrainer.setConfParam(&g_cp);
  190. //read configures
  191. ifstream ifs(conf);
  192. if (!ifs.good()) { return 1; }
  193. ifs.close();
  194. memset(&g_cp, 0, sizeof(ConfigParam));
  195. cv::FileStorage fs(conf, cv::FileStorage::READ);
  196. conf_contrainer.read(fs["conf_parameters"]);
  197. fs.release();
  198. g_conf_file = conf;
  199. return 0;
  200. }
  201. //3
  202. void cv_save_param(char* conf_file/*=0*/)
  203. {
  204. //save configures
  205. CGCvConfig conf_contrainer = CGCvConfig();
  206. conf_contrainer.setConfParam(&g_cp);
  207. if(conf_file){
  208. cv::FileStorage fs(
  209. conf_file,
  210. cv::FileStorage::WRITE
  211. );
  212. fs<<"conf_parameters";
  213. fs<<conf_contrainer;
  214. fs.release();
  215. }
  216. else{
  217. cv::FileStorage fs(
  218. g_conf_file,
  219. cv::FileStorage::WRITE
  220. );
  221. fs<<"conf_parameters";
  222. fs<<conf_contrainer;
  223. fs.release();
  224. }
  225. }
  226. //4
  227. void cv_get_param(ConfigParam&cp)
  228. {
  229. cp = g_cp;
  230. };
  231. //5
  232. void get_version(char* buf)
  233. {
  234. strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
  235. };
  236. //6
  237. void cv_get_conf_file(char*buff)
  238. {
  239. strcpy_s(buff, g_conf_file.size()+1, g_conf_file.c_str());
  240. };
  241. //7
  242. /*int rs_oa_init()
  243. {
  244. return g_oa.reset();
  245. };*/
  246. //8
  247. /*int rs_oa_append(
  248. ImgInfo* imginfo,
  249. PositionInfo& posinfo
  250. )
  251. {
  252. memset(&posinfo,0,sizeof(PositionInfo));
  253. try{
  254. g_oa.append(imginfo, posinfo);
  255. }
  256. catch(std::exception &err){
  257. g_logger.ERRORINFO(err.what());
  258. return 1;
  259. }
  260. catch(string& msg){
  261. g_logger.ERRORINFO(msg);
  262. return 1;
  263. }
  264. catch(...){
  265. g_logger.ERRORINFO("unknown error");
  266. return 1;
  267. }
  268. return 0;
  269. };*/
  270. //
  271. int sola_grab_point_rs(
  272. float* points,
  273. int pixel_size,
  274. int pt_size,
  275. PositionInfo& posinfo,
  276. const char* fn/*=0*/
  277. )
  278. {
  279. memset(&posinfo, 0, sizeof(PositionInfo));
  280. int dtype = 1;
  281. try {
  282. int rst = g_sola_grab_rs.load_data(points, pixel_size, pt_size, dtype, fn);
  283. if (rst <= 0) {
  284. g_logger.ERRORINFO("invalid points");
  285. return 1;
  286. }
  287. int oa = g_sola_grab_rs.grab_point_detect(posinfo);
  288. if (oa != 0) {
  289. g_logger.ERRORINFO("no points");
  290. return 1;
  291. }
  292. }
  293. catch (std::exception &err) {
  294. g_logger.ERRORINFO(err.what());
  295. return 1;
  296. }
  297. catch (string& msg) {
  298. g_logger.ERRORINFO(msg);
  299. return 1;
  300. }
  301. catch (...) {
  302. g_logger.ERRORINFO("unknown error");
  303. return 1;
  304. }
  305. return 0;
  306. }
  307. int sola_grab_point_sc(
  308. float* points,
  309. int pixel_size,
  310. int pt_size,
  311. PositionInfo& posinfo,
  312. const char* fn/*=0*/
  313. )
  314. {
  315. memset(&posinfo, 0, sizeof(PositionInfo));
  316. int dtype = 0;
  317. try {
  318. int rst = g_sola_grab_sc.load_data(points, pixel_size, pt_size, dtype, fn);
  319. if (rst <= 0) {
  320. g_logger.ERRORINFO("invalid points");
  321. return 1;
  322. }
  323. int oa = g_sola_grab_sc.grab_point_detect(posinfo);
  324. if (oa != 0) {
  325. g_logger.ERRORINFO("no points");
  326. return 1;
  327. }
  328. }
  329. catch (std::exception &err) {
  330. g_logger.ERRORINFO(err.what());
  331. return 1;
  332. }
  333. catch (string& msg) {
  334. g_logger.ERRORINFO(msg);
  335. return 1;
  336. }
  337. catch (...) {
  338. g_logger.ERRORINFO("unknown error");
  339. return 1;
  340. }
  341. return 0;
  342. }
  343. int chessboard_calibration(float* points, int pixel_size, int pt_size, PositionInfo& posinfo, const char* fn)
  344. {
  345. memset(&posinfo, 0, sizeof(PositionInfo));
  346. int dtype = 0;
  347. try {
  348. int rst = g_chessboard.load_data(points, pixel_size, pt_size, dtype, fn);
  349. if (rst <= 0) {
  350. g_logger.ERRORINFO("invalid points");
  351. return 1;
  352. }
  353. int oa = g_chessboard.cross_detect(posinfo);
  354. if (oa != 0) {
  355. g_logger.ERRORINFO("no points");
  356. return 1;
  357. }
  358. }
  359. catch (std::exception &err) {
  360. g_logger.ERRORINFO(err.what());
  361. return 1;
  362. }
  363. catch (string& msg) {
  364. g_logger.ERRORINFO(msg);
  365. return 1;
  366. }
  367. catch (...) {
  368. g_logger.ERRORINFO("unknown error");
  369. return 1;
  370. }
  371. return 0;
  372. }
  373. //9
  374. int rs_oa_get_result(
  375. ImgInfo* imginfo,
  376. PositionInfo& posinfo
  377. )
  378. {
  379. memset(&posinfo,0,sizeof(PositionInfo));
  380. try{
  381. double oa = g_oa.angle_recognize(imginfo, posinfo);
  382. }
  383. catch(std::exception &err){
  384. g_logger.ERRORINFO(err.what());
  385. return 1;
  386. }
  387. catch(string& msg){
  388. g_logger.ERRORINFO(msg);
  389. return 1;
  390. }
  391. catch(...){
  392. g_logger.ERRORINFO("unknown error");
  393. return 1;
  394. }
  395. return 0;
  396. }
  397. //10
  398. int rs_cut_point(
  399. ImgInfo* imginfo,
  400. PositionInfo& posinfo
  401. )
  402. {
  403. memset(&posinfo,0,sizeof(PositionInfo));
  404. try{
  405. g_rs_cp.up_point_detect(
  406. imginfo,
  407. cv::Mat(),
  408. posinfo,
  409. g_img_cache
  410. );
  411. }
  412. catch(std::exception &err){
  413. g_logger.ERRORINFO(err.what());
  414. return 1;
  415. }
  416. catch(string& msg){
  417. g_logger.ERRORINFO(msg);
  418. return 1;
  419. }
  420. catch(...){
  421. g_logger.ERRORINFO("unknown error");
  422. return 1;
  423. }
  424. return 0;
  425. }
  426. //11
  427. int rs_cut_point_reid(ImgInfo*imginfo , const char* pre_img_id, PositionInfo& posinfo)
  428. {
  429. memset(&posinfo,0,sizeof(PositionInfo));
  430. try{
  431. g_rs_cp_reid.cut_point_reid(
  432. imginfo,
  433. cv::Mat(),
  434. pre_img_id,
  435. posinfo,
  436. g_img_cache
  437. );
  438. }
  439. catch(std::exception &err){
  440. g_logger.ERRORINFO(err.what());
  441. return 1;
  442. }
  443. catch(string& msg){
  444. g_logger.ERRORINFO(msg);
  445. return 1;
  446. }
  447. catch(...){
  448. g_logger.ERRORINFO("unknown error");
  449. return 1;
  450. }
  451. return 0;
  452. }
  453. //
  454. int sola_cut_point_reid_rs(ImgInfo*imginfo, PositionInfo& posinfo)
  455. {
  456. memset(&posinfo, 0, sizeof(PositionInfo));
  457. int sola_type = 1;
  458. try {
  459. g_sola_reid_rs.cut_point_reid(imginfo, cv::Mat(), posinfo);
  460. }
  461. catch (std::exception &err) {
  462. g_logger.ERRORINFO(err.what());
  463. return 1;
  464. }
  465. catch (string& msg) {
  466. g_logger.ERRORINFO(msg);
  467. return 1;
  468. }
  469. catch (...) {
  470. g_logger.ERRORINFO("unknown error");
  471. return 1;
  472. }
  473. return 0;
  474. }
  475. int sola_cut_point_reid_sc(ImgInfo*imginfo, PositionInfo& posinfo)
  476. {
  477. memset(&posinfo, 0, sizeof(PositionInfo));
  478. int sola_type = 0;
  479. try {
  480. g_sola_reid_sc.cut_point_reid(imginfo, cv::Mat(), posinfo);
  481. }
  482. catch (std::exception &err) {
  483. g_logger.ERRORINFO(err.what());
  484. return 1;
  485. }
  486. catch (string& msg) {
  487. g_logger.ERRORINFO(msg);
  488. return 1;
  489. }
  490. catch (...) {
  491. g_logger.ERRORINFO("unknown error");
  492. return 1;
  493. }
  494. return 0;
  495. }
  496. //12
  497. int sc_cut_point(
  498. ImgInfo* imginfo,
  499. PositionInfo& posinfo
  500. )
  501. {
  502. memset(&posinfo,0,sizeof(PositionInfo));
  503. try{
  504. g_sc_cp.up_point_detect(
  505. imginfo,
  506. cv::Mat(),
  507. posinfo
  508. );
  509. }
  510. catch(std::exception &err){
  511. g_logger.ERRORINFO(err.what());
  512. return 1;
  513. }
  514. catch(string& msg){
  515. g_logger.ERRORINFO(msg);
  516. return 1;
  517. }
  518. catch(...){
  519. g_logger.ERRORINFO("unknown error");
  520. return 1;
  521. }
  522. return 0;
  523. }
  524. };