graft_cv_api.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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.19";
  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. g_chessboard.set_image_saver(&g_pImStore);
  133. return 0;
  134. }
  135. GCV_API int cv_release()
  136. {
  137. if(g_pImStore){
  138. delete g_pImStore;
  139. g_pImStore = 0;
  140. }
  141. //DeleteCriticalSection(&g_cs);
  142. return 0;
  143. }
  144. //1
  145. int cv_init(char*conf)
  146. {
  147. //InitializeCriticalSection(&g_cs);
  148. CGCvConfig conf_contrainer = CGCvConfig();
  149. conf_contrainer.setConfParam(&g_cp);
  150. if(conf){
  151. //read configures
  152. ifstream ifs(conf);
  153. if(!ifs.good()){return 1;}
  154. ifs.close();
  155. memset(&g_cp,0,sizeof(ConfigParam));
  156. cv::FileStorage fs(conf, cv::FileStorage::READ);
  157. conf_contrainer.read(fs["conf_parameters"]);
  158. fs.release();
  159. g_conf_file = conf;
  160. }
  161. else{
  162. ifstream ifs(g_conf_file);
  163. if(!ifs.good()){return 1;}
  164. ifs.close();
  165. memset(&g_cp,0,sizeof(ConfigParam));
  166. //read configures
  167. cv::FileStorage fs(g_conf_file, cv::FileStorage::READ);
  168. conf_contrainer.read(fs["conf_parameters"]);
  169. fs.release();
  170. }
  171. string pinfo = get_cparam_info(&g_cp);
  172. g_logger.INFO(string("lib version: ")+string(g_version_str));
  173. g_logger.INFO(string("load parameters:\n")+pinfo);
  174. return 0;
  175. };
  176. //2
  177. void cv_set_param(ConfigParam&cp)
  178. {
  179. g_cp = cp;
  180. string pinfo = get_cparam_info(&g_cp);
  181. g_logger.INFO(string("set parameters:\n")+pinfo);
  182. };
  183. int cv_set_param_from_file(char*conf)
  184. {
  185. if (conf == 0) {
  186. return 1;
  187. }
  188. ConfigParam cp;
  189. CGCvConfig conf_contrainer = CGCvConfig();
  190. conf_contrainer.setConfParam(&g_cp);
  191. //read configures
  192. ifstream ifs(conf);
  193. if (!ifs.good()) { return 1; }
  194. ifs.close();
  195. memset(&g_cp, 0, sizeof(ConfigParam));
  196. cv::FileStorage fs(conf, cv::FileStorage::READ);
  197. conf_contrainer.read(fs["conf_parameters"]);
  198. fs.release();
  199. g_conf_file = conf;
  200. return 0;
  201. }
  202. //3
  203. void cv_save_param(char* conf_file/*=0*/)
  204. {
  205. //save configures
  206. CGCvConfig conf_contrainer = CGCvConfig();
  207. conf_contrainer.setConfParam(&g_cp);
  208. if(conf_file){
  209. cv::FileStorage fs(
  210. conf_file,
  211. cv::FileStorage::WRITE
  212. );
  213. fs<<"conf_parameters";
  214. fs<<conf_contrainer;
  215. fs.release();
  216. }
  217. else{
  218. cv::FileStorage fs(
  219. g_conf_file,
  220. cv::FileStorage::WRITE
  221. );
  222. fs<<"conf_parameters";
  223. fs<<conf_contrainer;
  224. fs.release();
  225. }
  226. }
  227. //4
  228. void cv_get_param(ConfigParam&cp)
  229. {
  230. cp = g_cp;
  231. };
  232. //5
  233. void get_version(char* buf)
  234. {
  235. strcpy_s(buf, strlen(g_version_str)+1,g_version_str);
  236. };
  237. //6
  238. void cv_get_conf_file(char*buff)
  239. {
  240. strcpy_s(buff, g_conf_file.size()+1, g_conf_file.c_str());
  241. };
  242. //7
  243. /*int rs_oa_init()
  244. {
  245. return g_oa.reset();
  246. };*/
  247. //8
  248. /*int rs_oa_append(
  249. ImgInfo* imginfo,
  250. PositionInfo& posinfo
  251. )
  252. {
  253. memset(&posinfo,0,sizeof(PositionInfo));
  254. try{
  255. g_oa.append(imginfo, posinfo);
  256. }
  257. catch(std::exception &err){
  258. g_logger.ERRORINFO(err.what());
  259. return 1;
  260. }
  261. catch(string& msg){
  262. g_logger.ERRORINFO(msg);
  263. return 1;
  264. }
  265. catch(...){
  266. g_logger.ERRORINFO("unknown error");
  267. return 1;
  268. }
  269. return 0;
  270. };*/
  271. //
  272. int sola_grab_point_rs(
  273. float* points,
  274. int pixel_size,
  275. int pt_size,
  276. PositionInfo& posinfo,
  277. const char* fn/*=0*/
  278. )
  279. {
  280. memset(&posinfo, 0, sizeof(PositionInfo));
  281. int dtype = 1;
  282. try {
  283. int rst = g_sola_grab_rs.load_data(points, pixel_size, pt_size, dtype, fn);
  284. if (rst <= 0) {
  285. g_logger.ERRORINFO("invalid points");
  286. return 1;
  287. }
  288. int oa = g_sola_grab_rs.grab_point_detect(posinfo);
  289. if (oa != 0) {
  290. g_logger.ERRORINFO("no points");
  291. return 1;
  292. }
  293. }
  294. catch (std::exception &err) {
  295. g_logger.ERRORINFO(err.what());
  296. return 1;
  297. }
  298. catch (string& msg) {
  299. g_logger.ERRORINFO(msg);
  300. return 1;
  301. }
  302. catch (...) {
  303. g_logger.ERRORINFO("unknown error");
  304. return 1;
  305. }
  306. return 0;
  307. }
  308. int sola_grab_point_sc(
  309. float* points,
  310. int pixel_size,
  311. int pt_size,
  312. PositionInfo& posinfo,
  313. const char* fn/*=0*/
  314. )
  315. {
  316. memset(&posinfo, 0, sizeof(PositionInfo));
  317. int dtype = 0;
  318. try {
  319. int rst = g_sola_grab_sc.load_data(points, pixel_size, pt_size, dtype, fn);
  320. if (rst <= 0) {
  321. g_logger.ERRORINFO("invalid points");
  322. return 1;
  323. }
  324. int oa = g_sola_grab_sc.grab_point_detect(posinfo);
  325. if (oa != 0) {
  326. g_logger.ERRORINFO("no points");
  327. return 1;
  328. }
  329. }
  330. catch (std::exception &err) {
  331. g_logger.ERRORINFO(err.what());
  332. return 1;
  333. }
  334. catch (string& msg) {
  335. g_logger.ERRORINFO(msg);
  336. return 1;
  337. }
  338. catch (...) {
  339. g_logger.ERRORINFO("unknown error");
  340. return 1;
  341. }
  342. return 0;
  343. }
  344. int chessboard_calibration(float* points, int pixel_size, int pt_size, PositionInfo& posinfo, const char* fn)
  345. {
  346. memset(&posinfo, 0, sizeof(PositionInfo));
  347. int dtype = 0;
  348. try {
  349. int rst = g_chessboard.load_data(points, pixel_size, pt_size, dtype, fn);
  350. if (rst <= 0) {
  351. g_logger.ERRORINFO("invalid points");
  352. return 1;
  353. }
  354. int oa = g_chessboard.cross_detect(posinfo);
  355. if (oa != 0) {
  356. g_logger.ERRORINFO("no points");
  357. return 1;
  358. }
  359. }
  360. catch (std::exception &err) {
  361. g_logger.ERRORINFO(err.what());
  362. return 1;
  363. }
  364. catch (string& msg) {
  365. g_logger.ERRORINFO(msg);
  366. return 1;
  367. }
  368. catch (...) {
  369. g_logger.ERRORINFO("unknown error");
  370. return 1;
  371. }
  372. return 0;
  373. }
  374. //9
  375. int rs_oa_get_result(
  376. ImgInfo* imginfo,
  377. PositionInfo& posinfo
  378. )
  379. {
  380. memset(&posinfo,0,sizeof(PositionInfo));
  381. try{
  382. double oa = g_oa.angle_recognize(imginfo, posinfo);
  383. }
  384. catch(std::exception &err){
  385. g_logger.ERRORINFO(err.what());
  386. return 1;
  387. }
  388. catch(string& msg){
  389. g_logger.ERRORINFO(msg);
  390. return 1;
  391. }
  392. catch(...){
  393. g_logger.ERRORINFO("unknown error");
  394. return 1;
  395. }
  396. return 0;
  397. }
  398. //10
  399. int rs_cut_point(
  400. ImgInfo* imginfo,
  401. PositionInfo& posinfo
  402. )
  403. {
  404. memset(&posinfo,0,sizeof(PositionInfo));
  405. try{
  406. g_rs_cp.up_point_detect(
  407. imginfo,
  408. cv::Mat(),
  409. posinfo,
  410. g_img_cache
  411. );
  412. }
  413. catch(std::exception &err){
  414. g_logger.ERRORINFO(err.what());
  415. return 1;
  416. }
  417. catch(string& msg){
  418. g_logger.ERRORINFO(msg);
  419. return 1;
  420. }
  421. catch(...){
  422. g_logger.ERRORINFO("unknown error");
  423. return 1;
  424. }
  425. return 0;
  426. }
  427. //11
  428. int rs_cut_point_reid(ImgInfo*imginfo , const char* pre_img_id, PositionInfo& posinfo)
  429. {
  430. memset(&posinfo,0,sizeof(PositionInfo));
  431. try{
  432. g_rs_cp_reid.cut_point_reid(
  433. imginfo,
  434. cv::Mat(),
  435. pre_img_id,
  436. posinfo,
  437. g_img_cache
  438. );
  439. }
  440. catch(std::exception &err){
  441. g_logger.ERRORINFO(err.what());
  442. return 1;
  443. }
  444. catch(string& msg){
  445. g_logger.ERRORINFO(msg);
  446. return 1;
  447. }
  448. catch(...){
  449. g_logger.ERRORINFO("unknown error");
  450. return 1;
  451. }
  452. return 0;
  453. }
  454. //
  455. int sola_cut_point_reid_rs(ImgInfo*imginfo, PositionInfo& posinfo)
  456. {
  457. memset(&posinfo, 0, sizeof(PositionInfo));
  458. int sola_type = 1;
  459. try {
  460. g_sola_reid_rs.cut_point_reid(imginfo, cv::Mat(), posinfo);
  461. }
  462. catch (std::exception &err) {
  463. g_logger.ERRORINFO(err.what());
  464. return 1;
  465. }
  466. catch (string& msg) {
  467. g_logger.ERRORINFO(msg);
  468. return 1;
  469. }
  470. catch (...) {
  471. g_logger.ERRORINFO("unknown error");
  472. return 1;
  473. }
  474. return 0;
  475. }
  476. int sola_cut_point_reid_sc(ImgInfo*imginfo, PositionInfo& posinfo)
  477. {
  478. memset(&posinfo, 0, sizeof(PositionInfo));
  479. int sola_type = 0;
  480. try {
  481. g_sola_reid_sc.cut_point_reid(imginfo, cv::Mat(), posinfo);
  482. }
  483. catch (std::exception &err) {
  484. g_logger.ERRORINFO(err.what());
  485. return 1;
  486. }
  487. catch (string& msg) {
  488. g_logger.ERRORINFO(msg);
  489. return 1;
  490. }
  491. catch (...) {
  492. g_logger.ERRORINFO("unknown error");
  493. return 1;
  494. }
  495. return 0;
  496. }
  497. //12
  498. int sc_cut_point(
  499. ImgInfo* imginfo,
  500. PositionInfo& posinfo
  501. )
  502. {
  503. memset(&posinfo,0,sizeof(PositionInfo));
  504. try{
  505. g_sc_cp.up_point_detect(
  506. imginfo,
  507. cv::Mat(),
  508. posinfo
  509. );
  510. }
  511. catch(std::exception &err){
  512. g_logger.ERRORINFO(err.what());
  513. return 1;
  514. }
  515. catch(string& msg){
  516. g_logger.ERRORINFO(msg);
  517. return 1;
  518. }
  519. catch(...){
  520. g_logger.ERRORINFO("unknown error");
  521. return 1;
  522. }
  523. return 0;
  524. }
  525. };