Ajout de l'ensemble du workspace.
[GPU.git] / WCudaMSE / BilatTools_OpenCV / src / core / capture / cpp / Capture_A.cpp
1 #include <iostream>
2 #include "Capture_A.h"
3
4 using std::cerr;
5 using std::cout;
6 using std::endl;
7
8 //http://docs.opencv.org/doc/tutorials/highgui/video-input-psnr-ssim/video-input-psnr-ssim.html
9
10 /*----------------------------------------------------------------------*\
11 |* Declaration *|
12 \*---------------------------------------------------------------------*/
13
14 /*--------------------------------------*\
15 |* Public *|
16 \*-------------------------------------*/
17
18 /*--------------------------------------*\
19 |* Private *|
20 \*-------------------------------------*/
21
22 /*----------------------------------------------------------------------*\
23 |* Implementation *|
24 \*---------------------------------------------------------------------*/
25
26 /*--------------------------------------*\
27 |* Public *|
28 \*-------------------------------------*/
29
30 /*------------------*\
31 |* Constructeur *|
32 \*-----------------*/
33
34 /**
35 * Optimisation Cuda:
36 * Principe:
37 * Use pinned memory in host side
38 * Give this area to openCV
39 * Syntaxe:
40 * size_t sizeOctets=w*h*sizeof(uchar4);
41 * uchar4* ptrHostMemory;
42 * HANDLE_ERROR( cudaHostAlloc ((void**) &ptrHostMemory, sizeOctets,cudaHostAllocDefault ) );
43 *
44 * Usage:
45 * CaptureVideo captureur(pathToVideo, titre);
46 * Mat matImage=captureur.capturer(); // capture une image seulement ( à utiliser en boucle!)
47 * uchar4* image= CaptureVideo::castToUChar4(&matImage); // format cuda
48 */
49 Capture_A::Capture_A(VideoCapture* ptrCaptureStream, const string& title, uchar4* ptrHostMemory):chrono()
50 {
51 // cout << "[Capture_A] : Capture_A" << endl;
52
53 this->title = title;
54 this->compteurCapture = 0;
55 this->ptrCaptureStream = ptrCaptureStream;
56
57 this->w = (int) ptrCaptureStream->get(CV_CAP_PROP_FRAME_WIDTH);
58 this->h = (int) ptrCaptureStream->get(CV_CAP_PROP_FRAME_HEIGHT);
59
60 // chrono=Chronos();
61
62 if(ptrHostMemory!=NULL)
63 {
64 matCaptureSrc=Mat(w,h,CV_BGR2RGBA,ptrHostMemory); // Ecrase ancienne
65 }
66 }
67
68 Capture_A::~Capture_A(void)
69 {
70 if (ptrCaptureStream != NULL)
71 {
72 delete ptrCaptureStream;
73 ptrCaptureStream = NULL;
74 }
75 }
76
77 /*------------------*\
78 |* Methode *|
79 \*-----------------*/
80
81 Mat Capture_A::capturer(void)
82 {
83 //cout << "[Capture_A] : capturer" << endl;
84
85 compteurCapture++;
86
87 //(*ptrCaptureStream) >> matCaptureSrc; // plante en fin de video
88 readStream(this->ptrCaptureStream, &this->matCaptureSrc); // methode virtuelle : au lieu de ci-dessous, plus robuste
89
90 // debug
91 {
92 //printInfo();
93 }
94
95 // old
96 {
97 // Type image:
98 // CV_8U CV_ => BVRA et non RVBA => demande correction TODO Mieux?
99 // RGB2GRAY
100 // int nbChannel = 4; // 0 keep same chanel // 4 permet par exemple d'ajouter la couche alpha à rvb (pour une video ou webcam)
101 // cvtColor(matCaptureSrc, matCaptureDest, CV_8U, nbChannel);
102 }
103
104 // new
105 {
106 // http://siggiorn.com/wp-content/uploads/libraries/opencv-java/docs/sj/opencv/Constants.ColorConversion.html
107 //int colorConversion=CV_BGR2GRAY; // ok
108 //int colorConversion=CV_BGR2RGBA ; // ko
109 int colorConversion = CV_BGR2BGRA; // ok
110 cvtColor(matCaptureSrc, matCaptureDest, colorConversion);
111 }
112
113 return matCaptureDest; //castToUChar4(matCapture.data);
114 }
115
116 void Capture_A::printInfo(void)
117 {
118 cout << "[Capture_A] : Capture Info :" << endl;
119 cout << "\t(w,h) = (" << w << "," << h << ")" << endl;
120 cout << "\ttitle = " << title << endl;
121 cout << "\tnbChannel = " << matCaptureSrc.channels() << endl;
122 cout << "\ttype = " << matCaptureSrc.type() << endl;
123 cout << "\tisEmpty = " << matCaptureSrc.empty() << endl;
124 cout << "\tdtMS(source) = " << dtOriginalMS() << endl;
125
126 if (compteurCapture >= 2)
127 {
128 cout << "\tfps(capture) = " << fpsCapture() << endl;
129 }
130
131 cout << "\tchrono = " << chrono.timeFlight() << " (s)"<< endl;
132 cout << "\t#image = " << compteurCapture << endl;
133 }
134
135 int Capture_A::nbOctetImage()
136 {
137 return w * h * sizeof(uchar4);
138 }
139
140 int Capture_A::fpsCapture()
141 {
142 double timeS = chrono.timeFlight();
143 double fps = compteurCapture / timeS;
144
145 return (int) fps;
146 }
147
148 /*------------------*\
149 |* is *|
150 \*-----------------*/
151
152 bool Capture_A::isOpened(void)
153 {
154 return ptrCaptureStream->isOpened();
155 }
156
157 /*------------------*\
158 |* get *|
159 \*-----------------*/
160
161 VideoCapture* Capture_A::getVideoCapture()
162 {
163 return ptrCaptureStream;
164 }
165
166 int Capture_A::getW(void)
167 {
168 return w;
169 }
170
171 int Capture_A::getH(void)
172 {
173 return h;
174 }
175
176 int Capture_A::dtOriginalMS()
177 {
178 return 0;
179 }
180
181 long Capture_A::nbCapture()
182 {
183 return compteurCapture;
184 }
185
186 string Capture_A::getTitle()
187 {
188 return title;
189 }
190
191 Chronos Capture_A::getChronos()
192 {
193 return chrono;
194 }
195
196 /*--------------------------------------*\
197 |* Static *|
198 \*-------------------------------------*/
199
200 /**
201 * uchar = unsigned char
202 * uchar4 = 4 char sequentiel
203 */
204 uchar4* Capture_A::castToUChar4(uchar* ptrTab)
205 {
206 return (uchar4*) ptrTab;
207 }
208
209 /**
210 * uchar = unsigned char
211 * uchar4 = 4 char sequentiel
212 */
213 uchar4* Capture_A::castToUChar4(Mat* ptrMap)
214 {
215 return castToUChar4(ptrMap->data);
216 }
217
218 /*--------------------------------------*\
219 |* Private *|
220 \*-------------------------------------*/
221
222 // http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html#cv-videocapture-get
223 // TODO: fixme marche pas trop bien
224 // capture.set(CV_CAP_PROP_FRAME_WIDTH,1280);
225 // capture.set(CV_CAP_PROP_FRAME_HEIGHT ,720);
226 //capture.set(CV_CAP_PROP_EXPOSURE,0);
227 // int n = matCapture.rows;
228 // int m = matCapture.cols;
229 //
230 //uchar* ptr=matCapture.data;
231 // unsigned char* ptr4=(unsigned char*)ptr;
232 // cout<<"size uchar = "<<sizeof(uchar)<<endl;
233 // cout<<"size unsigned char = "<<sizeof(unsigned char)<<endl;
234 // cout<<"size uchar4 = "<<sizeof(uchar4)<<endl;
235 // cout<<"n= "<<n<<endl;
236 // cout<<"m= "<<m<<endl;
237 // uchar* ptrCopy=new uchar[n*m];
238 // uchar* ptrAgain=ptrCopy;
239 // for(int i=1;i<=n*m*4;i++)
240 // {
241 // *ptr++=123;
242 // }
243 //cout<<"i"<<i<<endl;
244 // *ptrCopy=*ptr4;
245 //// ptrCopy++;
246 // *ptr4=123;
247 // ptr4++;
248 //
249 //// //*ptrCopy=*ptr4;
250 //// ptrCopy++;
251 //// ptr4++;
252 ////
253 //// //*ptrCopy=*ptr4;
254 //// ptrCopy++;
255 //// ptr4++;
256 ////
257 //// //*ptrCopy=*ptr4;
258 ////
259 //// if(i<n*m)
260 //// {
261 //// ptrCopy++;
262 ////
263 //// ptr4++;
264 //// }
265 // }
266 //// for(int i=1;i<=n*m;i++)
267 //// {
268 //// //cout<<"i"<<i<<"\t";
269 //// assert(ptr[i-1]==ptrAgain[i-1]);
270 //// }
271 // CV_8U : [0,255]
272 // CV_16U : [0,65535]
273 // CV_32F : [0,1] in R
274 //cvtColor(matCapture, matCapture, CV_8U,0); // 0 keep same chanel
275 // cvtColor(matCapture, matCapture, CV_8U,4); // 0 keep same chanel // 4 permet par exemple d'ajouter la couche alpha à rvb (pour une video ou webcam)
276 /*----------------------------------------------------------------------*\
277 |* End *|
278 \*---------------------------------------------------------------------*/
279