PageRenderTime 149ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/libsrc/opencv/opencv_glue.c

https://gitlab.com/oytunistrator/QuIP
C | 320 lines | 245 code | 48 blank | 27 comment | 45 complexity | d65149fe48aaf382fee7b803ee852041 MD5 | raw file
  1. /* glue to link opencv subroutines with vt... */
  2. #include "quip_config.h"
  3. #ifdef HAVE_OPENCV
  4. #include "quip_prot.h"
  5. #include "data_obj.h"
  6. #include "opencv_glue.h"
  7. /* OpenCV_Image */
  8. ITEM_INTERFACE_DECLARATIONS(OpenCV_Image,ocvi)
  9. /* OpenCV_MemStorage */
  10. ITEM_INTERFACE_DECLARATIONS(OpenCV_MemStorage,ocv_mem)
  11. /* OpenCV_Scanner */
  12. ITEM_INTERFACE_DECLARATIONS(OpenCV_Scanner,ocv_scanner)
  13. /* OpenCV_Seq */
  14. ITEM_INTERFACE_DECLARATIONS(OpenCV_Seq,ocv_seq)
  15. /* OpenCV_ classifier cascade */
  16. ITEM_INTERFACE_DECLARATIONS(OpenCV_Cascade,ocv_ccasc)
  17. static OpenCV_Image *make_new_ocvi(QSP_ARG_DECL const char * obj_name);
  18. static OpenCV_MemStorage *make_new_ocv_mem(QSP_ARG_DECL const char * obj_name);
  19. static OpenCV_Scanner *make_new_ocv_scanner(QSP_ARG_DECL const char * obj_name);
  20. static OpenCV_Seq *make_new_ocv_seq(QSP_ARG_DECL const char * obj_name);
  21. static Data_Obj * creat_dp_for_ocvi(QSP_ARG_DECL OpenCV_Image *ocvi_p );
  22. static OpenCV_Image *make_new_ocvi(QSP_ARG_DECL const char * obj_name)
  23. {
  24. OpenCV_Image *ocvi_p;
  25. ocvi_p = ocvi_of(QSP_ARG obj_name);
  26. if( ocvi_p != NO_OPENCV_IMAGE ){
  27. sprintf(ERROR_STRING,"OpenCV image %s already exists!?",obj_name);
  28. WARN(ERROR_STRING);
  29. return(NO_OPENCV_IMAGE);
  30. }
  31. ocvi_p = new_ocvi(QSP_ARG obj_name);
  32. if( ocvi_p == NO_OPENCV_IMAGE ){
  33. sprintf(ERROR_STRING,"Error creating OpenCV image %s",obj_name);
  34. WARN(ERROR_STRING);
  35. return(NO_OPENCV_IMAGE);
  36. }
  37. ocvi_p->ocv_dp = NO_OBJ;
  38. return(ocvi_p);
  39. }
  40. static OpenCV_MemStorage *make_new_ocv_mem(QSP_ARG_DECL const char * obj_name)
  41. {
  42. OpenCV_MemStorage *ocv_mem_p;
  43. ocv_mem_p = ocv_mem_of(QSP_ARG obj_name);
  44. if( ocv_mem_p != NO_OPENCV_MEM ){
  45. sprintf(ERROR_STRING,"OpenCV mem storage %s already exists!?",obj_name);
  46. WARN(ERROR_STRING);
  47. return(NO_OPENCV_MEM);
  48. }
  49. ocv_mem_p = new_ocv_mem(QSP_ARG obj_name);
  50. if( ocv_mem_p == NO_OPENCV_MEM ){
  51. sprintf(ERROR_STRING,"Error creating OpenCV mem storage %s",obj_name);
  52. WARN(ERROR_STRING);
  53. return(NO_OPENCV_MEM);
  54. }
  55. ocv_mem_p->ocv_dp = NO_OBJ;
  56. return(ocv_mem_p);
  57. }
  58. static OpenCV_Scanner *make_new_ocv_scanner(QSP_ARG_DECL const char * obj_name)
  59. {
  60. OpenCV_Scanner *ocv_scanner_p;
  61. ocv_scanner_p = ocv_scanner_of(QSP_ARG obj_name);
  62. if( ocv_scanner_p != NO_OPENCV_SCANNER ){
  63. sprintf(ERROR_STRING,"OpenCV scanner %s already exists!?",obj_name);
  64. WARN(ERROR_STRING);
  65. return(NO_OPENCV_SCANNER);
  66. }
  67. ocv_scanner_p = new_ocv_scanner(QSP_ARG obj_name);
  68. if( ocv_scanner_p == NO_OPENCV_SCANNER ){
  69. sprintf(ERROR_STRING,"Error creating OpenCV scanner %s",obj_name);
  70. WARN(ERROR_STRING);
  71. return(NO_OPENCV_SCANNER);
  72. }
  73. ocv_scanner_p->ocv_dp = NO_OBJ;
  74. ocv_scanner_p->ocv_mem = cvCreateMemStorage(0);
  75. return(ocv_scanner_p);
  76. }
  77. static OpenCV_Seq *make_new_ocv_seq(QSP_ARG_DECL const char * obj_name)
  78. {
  79. OpenCV_Seq *ocv_seq_p;
  80. ocv_seq_p = ocv_seq_of(QSP_ARG obj_name);
  81. if( ocv_seq_p != NO_OPENCV_SEQ ){
  82. sprintf(ERROR_STRING,"OpenCV seq %s already exists!?",obj_name);
  83. WARN(ERROR_STRING);
  84. return(NO_OPENCV_SEQ);
  85. }
  86. ocv_seq_p = new_ocv_seq(QSP_ARG obj_name);
  87. if( ocv_seq_p == NO_OPENCV_SEQ ){
  88. sprintf(ERROR_STRING,"Error creating OpenCV seq %s",obj_name);
  89. WARN(ERROR_STRING);
  90. return(NO_OPENCV_SEQ);
  91. }
  92. ocv_seq_p->ocv_dp = NO_OBJ;
  93. ocv_seq_p->ocv_seq = NULL;
  94. return(ocv_seq_p);
  95. }
  96. //#ifdef FOOBAR
  97. OpenCV_Image * load_ocv_image(QSP_ARG_DECL const char * obj_name, const char * filename )
  98. {
  99. OpenCV_Image *ocvi_p;
  100. ocvi_p = make_new_ocvi(QSP_ARG obj_name);
  101. if( ocvi_p == NO_OPENCV_IMAGE ) return(ocvi_p);
  102. if( (ocvi_p->ocv_image = cvLoadImage( filename, CV_LOAD_IMAGE_COLOR)) == 0 ){
  103. sprintf(ERROR_STRING,"Error opening file %s!?",filename);
  104. WARN(ERROR_STRING);
  105. /* delete new struct here */
  106. del_ocvi(QSP_ARG ocvi_p);
  107. // free name here?
  108. return(NO_OPENCV_IMAGE);
  109. }
  110. return(ocvi_p);
  111. }
  112. //#endif /* FOOBAR */
  113. void save_ocv_image( OpenCV_Image *ocvi_p , const char* filename)
  114. {
  115. // Save the image to the given filename.
  116. /* check for success??? */
  117. /* Why the third arg? difference versions of lib? */
  118. #if CV_MAJOR_VERSION >= 2
  119. cvSaveImage(filename, ocvi_p->ocv_image , 0 );
  120. #else
  121. cvSaveImage(filename, ocvi_p->ocv_image /* , 0 */ );
  122. #endif
  123. }
  124. static Data_Obj * creat_dp_for_ocvi(QSP_ARG_DECL OpenCV_Image *ocvi_p )
  125. {
  126. IplImage* img;
  127. Dimension_Set dimset;
  128. Data_Obj *dp;
  129. prec_t p;
  130. img = ocvi_p->ocv_image;
  131. switch(img->depth) {
  132. case IPL_DEPTH_8U: p = PREC_UBY; break;
  133. case IPL_DEPTH_8S: p = PREC_BY; break;
  134. case IPL_DEPTH_16S: p = PREC_IN; break;
  135. case IPL_DEPTH_16U: p = PREC_UIN; break;
  136. case IPL_DEPTH_32S: p = PREC_DI; break;
  137. /* case IPL_DEPTH_32U: p = PREC_UDI; break; */
  138. case IPL_DEPTH_32F: p = PREC_SP; break;
  139. case IPL_DEPTH_64F: p = PREC_DP; break;
  140. default:
  141. sprintf(ERROR_STRING,"creat_dp_for_ocvi: unrecognized depth code %d!?",img->depth);
  142. WARN(ERROR_STRING);
  143. return(NO_OBJ);
  144. break;
  145. }
  146. if( img->dataOrder == 0 ){
  147. dimset.ds_dimension[0] = img->nChannels;
  148. dimset.ds_dimension[1] = img->width; // Image width.
  149. dimset.ds_dimension[2] = img->height; // Image height.
  150. dimset.ds_dimension[3] = 1;
  151. } else {
  152. dimset.ds_dimension[0] = 1;
  153. dimset.ds_dimension[1] = img->width; // Image width.
  154. dimset.ds_dimension[2] = img->height; // Image height.
  155. dimset.ds_dimension[3] = img->nChannels;
  156. sprintf(ERROR_STRING,"OpenCV image components are not interleaved!? (dataOrder = %d)",img->dataOrder);
  157. advise(ERROR_STRING);
  158. }
  159. dimset.ds_dimension[4] = 1;
  160. dp = _make_dp(QSP_ARG ocvi_p->ocv_name,&dimset,PREC_FOR_CODE(p));
  161. if( dp == NO_OBJ ) return(dp);
  162. SET_OBJ_DATA_PTR(dp, img->imageData);
  163. SET_OBJ_UNALIGNED_PTR(dp, img->imageDataOrigin);
  164. return(dp);
  165. }
  166. OpenCV_Image * create_ocv_image(QSP_ARG_DECL const char *obj_name,
  167. long width,long height,int bit_depth_code,int n_channels)
  168. {
  169. OpenCV_Image *ocvi_p;
  170. ocvi_p = make_new_ocvi(QSP_ARG obj_name);
  171. if( ocvi_p == NO_OPENCV_IMAGE ) return(ocvi_p);
  172. if( (ocvi_p->ocv_image = cvCreateImage(cvSize(width,height), bit_depth_code, n_channels)) == 0 ){
  173. sprintf(ERROR_STRING,"create_ocv_image: error creating ocv image for %s...",obj_name);
  174. WARN(ERROR_STRING);
  175. del_ocvi(QSP_ARG ocvi_p);
  176. // release name here??
  177. return(NO_OPENCV_IMAGE);
  178. }
  179. /*
  180. * This is where we allocate a data_obj struct, and then fill in
  181. * all the fields, and set the data ptr accordingly...
  182. * (as done in make_frame_obj in ../newmeteor/mcapt.c)
  183. */
  184. if( (ocvi_p->ocv_dp = creat_dp_for_ocvi(QSP_ARG ocvi_p)) == NO_OBJ ){
  185. sprintf(ERROR_STRING,"Error creating QuIP image for OpenCV image %s",ocvi_p->ocv_name);
  186. WARN(ERROR_STRING);
  187. }
  188. return(ocvi_p);
  189. }
  190. OpenCV_MemStorage * create_ocv_mem(QSP_ARG_DECL const char *obj_name)
  191. {
  192. OpenCV_MemStorage *ocv_mem_p;
  193. ocv_mem_p = make_new_ocv_mem(QSP_ARG obj_name);
  194. if( ocv_mem_p == NO_OPENCV_MEM ) return(ocv_mem_p);
  195. if( (ocv_mem_p->ocv_mem = cvCreateMemStorage(0)) == 0 ){
  196. sprintf(ERROR_STRING,"create_ocv_mem: error creating ocv mem storage for %s...",obj_name);
  197. WARN(ERROR_STRING);
  198. del_ocv_mem(QSP_ARG ocv_mem_p);
  199. // release name here???
  200. return(NO_OPENCV_MEM);
  201. }
  202. return(ocv_mem_p);
  203. }
  204. OpenCV_Scanner * create_ocv_scanner(QSP_ARG_DECL const char *obj_name)
  205. {
  206. OpenCV_Scanner *ocv_scanner_p;
  207. ocv_scanner_p = make_new_ocv_scanner(QSP_ARG obj_name);
  208. if( ocv_scanner_p == NO_OPENCV_SCANNER ) return(ocv_scanner_p);
  209. return(ocv_scanner_p);
  210. }
  211. OpenCV_Seq * create_ocv_seq(QSP_ARG_DECL const char *obj_name)
  212. {
  213. OpenCV_Seq *ocv_seq_p;
  214. ocv_seq_p = make_new_ocv_seq(QSP_ARG obj_name);
  215. if( ocv_seq_p == NO_OPENCV_SEQ ) return(ocv_seq_p);
  216. ocv_seq_p->ocv_seq = NULL;
  217. return(ocv_seq_p);
  218. }
  219. OpenCV_Image *creat_ocvi_from_dp(QSP_ARG_DECL Data_Obj *dp)
  220. {
  221. OpenCV_Image* ocvi_p = make_new_ocvi(QSP_ARG OBJ_NAME(dp));
  222. if( ocvi_p == NO_OPENCV_IMAGE ) {
  223. return(ocvi_p);
  224. }
  225. ocvi_p->ocv_image = (IplImage *)getbuf(sizeof(*ocvi_p->ocv_image));
  226. /* don't need to test here because getbuf kill whole program if failure... */
  227. /* Now copy over the fields... */
  228. IplImage* img = ocvi_p->ocv_image;
  229. /*img->nSize =
  230. dp->dt_nelts * ELEMENT_SIZE(dp);*/ /* in bytes... */
  231. img->nSize = sizeof(*img);
  232. img->ID = 0;
  233. img->nChannels = OBJ_COMPS(dp);
  234. int precision;
  235. int bpp; // Bytes per pixel.
  236. switch (OBJ_PREC(dp)) {
  237. case PREC_UBY: precision = IPL_DEPTH_8U; bpp = 8; break;
  238. case PREC_BY: precision = IPL_DEPTH_8S; bpp = 8; break;
  239. case PREC_IN: precision = IPL_DEPTH_16S; bpp = 16; break;
  240. case PREC_UIN: precision = IPL_DEPTH_16U; bpp = 16; break;
  241. case PREC_DI: precision = IPL_DEPTH_32S; bpp = 32; break;
  242. case PREC_SP: precision = IPL_DEPTH_32F; bpp = 32; break;
  243. case PREC_DP: precision = IPL_DEPTH_64F; bpp = 64; break;
  244. default:
  245. sprintf(ERROR_STRING,"creat_dp_for_ocvi: unrecognized depth code %d!?",img->depth);
  246. WARN(ERROR_STRING);
  247. return(NO_OPENCV_IMAGE);
  248. break;
  249. }
  250. img->depth = precision;
  251. img->widthStep = OBJ_COLS(dp)*OBJ_COMPS(dp);
  252. if (OBJ_FRAMES(dp) != 1) {
  253. img->dataOrder = 1; // Separate color channels.
  254. /* img->widthStep = bpp*img->width; */
  255. img->imageSize = img->height * img->widthStep * OBJ_COMPS(dp);
  256. } else {
  257. img->dataOrder = 0; // Interleaved color channels.
  258. /* img->widthStep = bpp*img->width*OBJ_COMPS(dp); */
  259. img->imageSize = OBJ_ROWS(dp) * img->widthStep; // Image size in bytes.
  260. }
  261. img->origin = 0; // 0 for upper-left origin.
  262. img->width = OBJ_COLS(dp); // Image width.
  263. img->height = OBJ_ROWS(dp); // Image height.
  264. img->roi = NULL;
  265. img->maskROI = NULL;
  266. img->imageId = NULL;
  267. img->tileInfo = NULL;
  268. img->imageData = (char *)OBJ_DATA_PTR(dp);
  269. img->imageDataOrigin = (char *)OBJ_UNALIGNED_PTR(dp);
  270. return(ocvi_p);
  271. }
  272. #endif /* HAVE_OPENCV */