You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
6.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <FL/Fl.H>
  2. #include <FL/fl_draw.H>
  3. #include "Fl_OpenCV.h"
  4. IplImage *fl_LoadImage(char*filename,int iscolor)
  5. {
  6. IplImage *cd,*image;
  7. if( (cd = cvLoadImage(filename, iscolor)) == 0 ){
  8. fl_alert("Image not opened !");
  9. return NULL;
  10. }
  11. if( cd->nChannels==3){
  12. // from showimage
  13. CvMat * mat, stub;
  14. int origin=0;
  15. origin = cd->origin;
  16. mat = cvGetMat(cd, &stub);
  17. cvConvertImage( mat, cd,(origin != 0 ? CV_CVTIMG_FLIP : 0) + CV_CVTIMG_SWAP_RB );
  18. // �maj withStep yanl�� hesaplan�yor (with > 600 gibi ) o y�zden resize ederek yap�yoruz !
  19. }
  20. if(cd->widthStep!=cd->width*cd->nChannels) {
  21. image = cvCreateImage(cvSize(cd->width,cd->height), IPL_DEPTH_8U, cd->nChannels);
  22. image->widthStep=image->width*cd->nChannels;
  23. cvResize( cd,image, CV_INTER_LINEAR );
  24. cvReleaseImage(&cd);
  25. return image;
  26. }
  27. else
  28. return cd;
  29. }
  30. bool fl_SaveImage(char*filename,IplImage *image)
  31. {
  32. if(image->nChannels==3){
  33. cvConvertImage( image,image,CV_CVTIMG_SWAP_RB );
  34. if((cvSaveImage(filename,image)) == 0 ){
  35. fl_alert("Image not saved");
  36. cvConvertImage( image,image,CV_CVTIMG_SWAP_RB );
  37. return false;
  38. }
  39. cvConvertImage( image,image,CV_CVTIMG_SWAP_RB );
  40. } else if((cvSaveImage(filename,image)) == 0 ){
  41. fl_alert("Image not saved");
  42. return false;
  43. }
  44. return true;
  45. }
  46. // Fl_OpenCv
  47. void Fl_OpenCV::draw(int xx, int yy, int ww, int hh)
  48. {
  49. if(!image) return;
  50. fl_push_clip(x(),y(),ww,hh);
  51. if(fit){
  52. if(fimage)
  53. cvReleaseImage(&fimage);
  54. fimage = cvCreateImage( cvSize( ww,hh),image->depth,image->nChannels);
  55. cvResize( image,fimage, CV_INTER_LINEAR );
  56. fl_draw_image((uchar *)fimage->imageData,xx, yy, fimage->width, fimage->height, fimage->nChannels , 0);
  57. }
  58. else
  59. fl_draw_image((uchar *)image->imageData,xx, yy, ww, hh, image->nChannels , 0);
  60. fl_pop_clip();
  61. }
  62. Fl_OpenCV::Fl_OpenCV(int X,int Y,int W,int H, char *filename):Fl_Widget(X,Y,W,H,0)
  63. {
  64. x(X);y(Y);w(W);h(H);
  65. image=NULL;
  66. fimage=NULL;
  67. fit=false;
  68. if(filename){
  69. Load(filename);
  70. }
  71. }
  72. Fl_OpenCV::Fl_OpenCV(int X, int Y):Fl_Widget(X,Y,1,1,0)
  73. {
  74. image=NULL;
  75. fimage=NULL;
  76. x(X);y(Y);
  77. }
  78. bool Fl_OpenCV::Load(char*filename,int iscolor)
  79. {
  80. if(image)
  81. cvReleaseImage(&image);
  82. if( (image = fl_LoadImage(filename, iscolor)) == 0 )
  83. return false;
  84. if(fit==false){
  85. w(image->width);
  86. h(image->height);
  87. resize(x(),y(),w(),h());
  88. }
  89. draw();
  90. return true;
  91. }
  92. bool Fl_OpenCV::Save(char*filename)
  93. {
  94. if((fl_SaveImage(filename,image)) == 0 ){
  95. fl_alert("Image not saved");
  96. return false;
  97. }
  98. else
  99. return true;
  100. }
  101. void Fl_OpenCV::SetImage(IplImage *nimage)
  102. {
  103. if(image)
  104. cvReleaseImage(&image);
  105. image=nimage;
  106. if(fit==false){
  107. w(image->width);
  108. h(image->height);
  109. resize(x(),y(),w(),h());
  110. }
  111. draw();
  112. }
  113. void Fl_OpenCV::FitWindows()
  114. {
  115. fit=true;
  116. }
  117. Fl_OpenCV::~Fl_OpenCV()
  118. {
  119. if(image)
  120. cvReleaseImage(&image);
  121. }
  122. // Video k�sm�
  123. bool Fl_Capture::CaptureFromFile( char* filename )
  124. {
  125. if(cap)
  126. cvReleaseCapture(&cap);
  127. cap= cvCaptureFromFile(filename);
  128. if(cap == NULL)
  129. {
  130. fl_alert("Video not opened");
  131. return false;
  132. }
  133. else
  134. return true;
  135. }
  136. bool Fl_Capture::CaptureFromCAM( int index )
  137. {
  138. cap= cvCaptureFromCAM(index );
  139. if(cap == NULL)
  140. {
  141. fl_alert("VideoCAM not opened");
  142. return false;
  143. }
  144. else
  145. return true;
  146. }
  147. int Fl_Capture::GrabFrame()
  148. {
  149. return cvGrabFrame(cap);
  150. }
  151. void Fl_Capture::RetrieveFrame()
  152. {
  153. frame=cvRetrieveFrame(cap);
  154. if(!frame) fl_alert("Frame not Retrieved");
  155. }
  156. void Fl_Capture::QueryFrame()
  157. {
  158. frame=cvRetrieveFrame(cap);
  159. if(!frame) fl_alert("Frame not Retrieved");
  160. }
  161. void Fl_Capture::ShowFrame()
  162. {
  163. }
  164. IplImage * Fl_Capture::GetFrame()
  165. {
  166. IplImage *image=NULL;
  167. GrabFrame();
  168. RetrieveFrame();
  169. if(frame){
  170. if(frame->nChannels==3){
  171. // from showimage
  172. CvMat * mat, stub;
  173. int origin=0;
  174. origin = frame->origin;
  175. mat = cvGetMat(frame, &stub);
  176. // cvConvertImage( mat, frame,(origin != 0 ? CV_CVTIMG_FLIP : 0) + CV_CVTIMG_SWAP_RB );
  177. cvConvertImage( mat, frame, CV_CVTIMG_SWAP_RB );
  178. // �maj withStep yanl�� hesaplan�yor (with > 600 gibi ) o y�zden resize ederek yap�yoruz !
  179. image = cvCreateImage(cvSize(frame->width,frame->height), IPL_DEPTH_8U, 3);
  180. image->widthStep=image->width*3;
  181. // cvResize( frame,image, CV_INTER_LINEAR );
  182. if( frame->origin == IPL_ORIGIN_TL )
  183. cvCopy( frame, image, 0 );
  184. else
  185. cvFlip( frame, image, 0 );
  186. }
  187. else{
  188. // image=cd;
  189. image = cvCreateImage(cvSize(frame->width,frame->height), IPL_DEPTH_8U, 1);
  190. image->widthStep=image->width*1;
  191. cvResize( frame,image, CV_INTER_LINEAR );
  192. }
  193. }
  194. return image;
  195. }
  196. double Fl_Capture::GetPositionMs()
  197. {
  198. return cvGetCaptureProperty(cap,CV_CAP_PROP_POS_MSEC);
  199. }
  200. double Fl_Capture::GetPositionFrame()
  201. {
  202. return cvGetCaptureProperty(cap,CV_CAP_PROP_POS_FRAMES);
  203. }
  204. double Fl_Capture::GetPositionPercent()
  205. {
  206. return (100*cvGetCaptureProperty(cap,CV_CAP_PROP_POS_AVI_RATIO));
  207. }
  208. double Fl_Capture::GetFrameWidth()
  209. {
  210. return cvGetCaptureProperty(cap,CV_CAP_PROP_FRAME_WIDTH );
  211. }
  212. double Fl_Capture::GetFrameHeight()
  213. {
  214. return cvGetCaptureProperty(cap,CV_CAP_PROP_FRAME_HEIGHT );
  215. }
  216. double Fl_Capture::GetFPS()
  217. {
  218. return cvGetCaptureProperty(cap,CV_CAP_PROP_FPS );
  219. }
  220. double Fl_Capture::GetCodec()
  221. {
  222. return cvGetCaptureProperty(cap,CV_CAP_PROP_FOURCC );
  223. }
  224. Fl_Capture::~Fl_Capture()
  225. {
  226. if(cap)
  227. cvReleaseCapture(&cap);
  228. }
  229. int Fl_Capture::SetPositionMs(double value)
  230. {
  231. return cvSetCaptureProperty( cap, CV_CAP_PROP_POS_MSEC, value );
  232. }
  233. int Fl_Capture::SetPositionFrame(double value)
  234. {
  235. return cvSetCaptureProperty( cap, CV_CAP_PROP_POS_FRAMES , value );
  236. }
  237. int Fl_Capture::SetPositionPercent(double value)
  238. {
  239. return cvSetCaptureProperty( cap, CV_CAP_PROP_POS_AVI_RATIO , value/100 );
  240. }
  241. int Fl_Capture::SetFrameWidth(double value)
  242. {
  243. // only for cameras
  244. return cvSetCaptureProperty( cap, CV_CAP_PROP_FRAME_WIDTH , value );
  245. }
  246. int Fl_Capture::SetFrameHeight(double value)
  247. {
  248. // only for cameras
  249. return cvSetCaptureProperty( cap, CV_CAP_PROP_FRAME_HEIGHT , value );
  250. }
  251. int Fl_Capture::SetFPS(double value)
  252. {
  253. // only for cameras
  254. return cvSetCaptureProperty( cap, CV_CAP_PROP_FPS , value );
  255. }
  256. int Fl_Capture::SetCodec(double value)
  257. {
  258. // only for cameras
  259. return cvSetCaptureProperty( cap, CV_CAP_PROP_FOURCC , value );
  260. }