cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
svlSampleImageCustom.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Balazs Vagvolgyi
7  Created on: 2010
8 
9  (C) Copyright 2006-2010 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 
20 */
21 
22 #ifndef _svlSampleImageCustom_h
23 #define _svlSampleImageCustom_h
24 
31 
32 // Always include last!
34 
35 
36 template <class _ValueType, unsigned int _DataChannels, unsigned int _VideoChannels>
37 class /*CISST_EXPORT*/ svlSampleImageCustom : public svlSampleImage
38 {
39  CMN_DECLARE_SERVICES_EXPORT(CMN_DYNAMIC_CREATION, CMN_LOG_ALLOW_DEFAULT);
40 
41 public:
42 
44  // Constructors //
46 
49  OwnData(true)
50  {
51  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
52  OwnImage[vch] = new vctDynamicMatrix<_ValueType>;
53  Image[vch].SetRef(*(OwnImage[vch]));
54 #if CISST_SVL_HAS_OPENCV
55  int ocvdepth = GetOCVDepth();
56  if (ocvdepth >= 0) OCVImageHeader[vch] = cvCreateImageHeader(cvSize(0, 0), ocvdepth, _DataChannels);
57  else OCVImageHeader[vch] = 0;
58 #endif // CISST_SVL_HAS_OPENCV
59  }
60  }
61 
62  svlSampleImageCustom(bool owndata) :
64  OwnData(owndata)
65  {
66  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
67  if (OwnData) {
68  OwnImage[vch] = new vctDynamicMatrix<_ValueType>;
69  Image[vch].SetRef(*(OwnImage[vch]));
70  }
71  else {
72  OwnImage[vch] = 0;
73  }
74 #if CISST_SVL_HAS_OPENCV
75  int ocvdepth = GetOCVDepth();
76  if (ocvdepth >= 0) OCVImageHeader[vch] = cvCreateImageHeader(cvSize(0, 0), ocvdepth, _DataChannels);
77  else OCVImageHeader[vch] = 0;
78 #endif // CISST_SVL_HAS_OPENCV
79  }
80  }
81 
83  svlSampleImage(other),
84  OwnData(true)
85  {
86  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
87  OwnImage[vch] = new vctDynamicMatrix<_ValueType>;
88  Image[vch].SetRef(*(OwnImage[vch]));
89 #if CISST_SVL_HAS_OPENCV
90  int ocvdepth = GetOCVDepth();
91  if (ocvdepth >= 0) OCVImageHeader[vch] = cvCreateImageHeader(cvSize(0, 0), ocvdepth, _DataChannels);
92  else OCVImageHeader[vch] = 0;
93 #endif // CISST_SVL_HAS_OPENCV
94  }
95  CopyOf(other);
96  }
97 
98 
100  // Destructor //
102 
104  {
105  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
106  if (OwnImage[vch]) delete OwnImage[vch];
107 #if CISST_SVL_HAS_OPENCV
108  if (OCVImageHeader[vch]) cvReleaseImageHeader(&(OCVImageHeader[vch]));
109 #endif // CISST_SVL_HAS_OPENCV
110  }
111  }
112 
114  // Operators //
116 
118  {
119  CopyOf(other);
120  return *this;
121  }
122 
123 
125  // Inherited from svlSample //
127 
129  {
131  }
132 
134  {
135  if (IsTypeUInt8<_ValueType>(static_cast<_ValueType>(0))) {
136  if (_DataChannels == 1) {
137  if (_VideoChannels == 1) return svlTypeImageMono8;
138  if (_VideoChannels == 2) return svlTypeImageMono8Stereo;
139  }
140  if (_DataChannels == 3) {
141  if (_VideoChannels == 1) return svlTypeImageRGB;
142  if (_VideoChannels == 2) return svlTypeImageRGBStereo;
143  }
144  if (_DataChannels == 4) {
145  if (_VideoChannels == 1) return svlTypeImageRGBA;
146  if (_VideoChannels == 2) return svlTypeImageRGBAStereo;
147  }
148  }
149  if (IsTypeUInt16<_ValueType>(static_cast<_ValueType>(0))) {
150  if (_DataChannels == 1) {
151  if (_VideoChannels == 1) return svlTypeImageMono16;
152  if (_VideoChannels == 2) return svlTypeImageMono16Stereo;
153  }
154  }
155  if (IsTypeUInt32<_ValueType>(static_cast<_ValueType>(0))) {
156  if (_DataChannels == 1) {
157  if (_VideoChannels == 1) return svlTypeImageMono32;
158  if (_VideoChannels == 2) return svlTypeImageMono32Stereo;
159  }
160  }
161  if (IsTypeFloat<_ValueType>(static_cast<_ValueType>(0))) {
162  if (_DataChannels == 3 && _VideoChannels == 1) return svlTypeImage3DMap;
163  }
164  return svlTypeInvalid;
165  }
166 
167  int SetSize(const svlSample* sample)
168  {
169  const svlSampleImage* sampleimage = dynamic_cast<const svlSampleImage*>(sample);
170  if (sampleimage) {
171  unsigned int samplevideochannels = sampleimage->GetVideoChannels();
172  for (unsigned int vch = 0; vch < _VideoChannels && vch < samplevideochannels; vch ++) {
173  SetSize(vch, sampleimage->GetWidth(vch), sampleimage->GetHeight(vch));
174  }
175  return SVL_OK;
176  }
177  else {
178  const svlSampleMatrix* samplematrix = dynamic_cast<const svlSampleMatrix*>(sample);
179  if (samplematrix) {
180  SetSize(samplematrix->GetCols(), samplematrix->GetRows());
181  return SVL_OK;
182  }
183  }
184  return SVL_FAIL;
185  }
186 
187  int SetSize(const svlSample& sample)
188  {
189  const svlSampleImage* sampleimage = dynamic_cast<const svlSampleImage*>(&sample);
190  if (sampleimage) {
191  unsigned int samplevideochannels = sampleimage->GetVideoChannels();
192  for (unsigned int vch = 0; vch < _VideoChannels && vch < samplevideochannels; vch ++) {
193  SetSize(vch, sampleimage->GetWidth(vch), sampleimage->GetHeight(vch));
194  }
195  return SVL_OK;
196  }
197  else {
198  const svlSampleMatrix* samplematrix = dynamic_cast<const svlSampleMatrix*>(&sample);
199  if (samplematrix) {
200  SetSize(samplematrix->GetCols(), samplematrix->GetRows());
201  return SVL_OK;
202  }
203  }
204  return SVL_FAIL;
205  }
206 
207  int CopyOf(const svlSample* sample)
208  {
209  if (!sample) return SVL_FAIL;
210  if (sample->GetType() != GetType() || SetSize(sample) != SVL_OK) return SVL_FAIL;
211 
212  const svlSampleImage* sampleimage = dynamic_cast<const svlSampleImage*>(sample);
213  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
214  memcpy(GetUCharPointer(vch), sampleimage->GetUCharPointer(vch), GetDataSize(vch));
215  }
216  SetTimestamp(sample->GetTimestamp());
217 
218  return SVL_OK;
219  }
220 
221  int CopyOf(const svlSample& sample)
222  {
223  if (sample.GetType() != GetType() || SetSize(sample) != SVL_OK) return SVL_FAIL;
224 
225  const svlSampleImage* sampleimage = dynamic_cast<const svlSampleImage*>(&sample);
226  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
227  memcpy(GetUCharPointer(vch), sampleimage->GetUCharPointer(vch), GetDataSize(vch));
228  }
229  SetTimestamp(sample.GetTimestamp());
230 
231  return SVL_OK;
232  }
233 
234  bool IsInitialized() const
235  {
236  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
237  if (Image[vch].width() < _DataChannels ||
238  Image[vch].height() < 1) return false;
239  }
240  return true;
241  }
242 
243  unsigned char* GetUCharPointer()
244  {
245  return reinterpret_cast<unsigned char*>(GetPointer());
246  }
247 
248  const unsigned char* GetUCharPointer() const
249  {
250  return reinterpret_cast<const unsigned char*>(GetPointer());
251  }
252 
253  unsigned int GetDataSize() const
254  {
255  return GetDataSize(0);
256  }
257 
258  virtual void SerializeRaw(std::ostream & outputStream) const
259  {
260  mtsGenericObject::SerializeRaw(outputStream);
261 
262  std::string codec;
263  int compression;
264  GetEncoder(codec, compression);
265  cmnSerializeRaw(outputStream, static_cast<int>(GetType()));
266  cmnSerializeRaw(outputStream, GetTimestamp());
267  cmnSerializeRaw(outputStream, codec);
268 
269  int hasdata = 1;
270  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
271  if (GetWidth(vch) < 1 || GetHeight(vch) < 1) hasdata = 0;
272  }
273  cmnSerializeRaw(outputStream, hasdata);
274 
275  if (hasdata == 1) {
276  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
277  if (svlImageIO::Write(*this, vch, codec, outputStream, compression) != SVL_OK) {
278 // Exeptions are not handled yet.
279 // TO DO: Restore code once exception handling is corrected.
280 // cmnThrow("svlSampleImageCustom::SerializeRaw(): Error occured with svlImageIO::Write");
281  }
282  }
283  }
284  }
285 
286  virtual void DeSerializeRaw(std::istream & inputStream)
287  {
289 
290  int type = -1;
291  double timestamp;
292  std::string codec;
293  int hasdata;
294  cmnDeSerializeRaw(inputStream, type);
295  if (type != GetType()) {
296  CMN_LOG_CLASS_RUN_ERROR << "Deserialized sample type mismatch " << std::endl;
297  return;
298  }
299  cmnDeSerializeRaw(inputStream, timestamp);
300  SetTimestamp(timestamp);
301  cmnDeSerializeRaw(inputStream, codec);
302  cmnDeSerializeRaw(inputStream, hasdata);
303 
304  if (hasdata == 1) {
305  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
306  if (svlImageIO::Read(*this, vch, codec, inputStream, false) != SVL_OK) {
307 // Exeptions are not handled yet.
308 // TO DO: Restore code once exception handling is corrected.
309 // cmnThrow("svlSampleImageCustom::DeSerializeRaw(): Error occured with svlImageIO::Read");
310  }
311  }
312  }
313  }
314 
316  // Inherited from svlSampleImage //
318 
319 #if CISST_SVL_HAS_OPENCV
320  IplImage* IplImageRef(const unsigned int videochannel = 0) const
321 #else // CISST_SVL_HAS_OPENCV
322  IplImage* IplImageRef(const unsigned int CMN_UNUSED(videochannel) = 0) const
323 #endif // CISST_SVL_HAS_OPENCV
324  {
325 #if CISST_SVL_HAS_OPENCV
326  if (videochannel < _VideoChannels) return OCVImageHeader[videochannel];
327  else return 0;
328 #else // CISST_SVL_HAS_OPENCV
329  CMN_LOG_CLASS_INIT_ERROR << "Class svlSampleImageCustom: IplImageRef() called while OpenCV is disabled" << std::endl;
330  return 0;
331 #endif // CISST_SVL_HAS_OPENCV
332  }
333 
334 #if CISST_SVL_HAS_OPENCV
335  cv::Mat CvMatRef(const unsigned int videochannel = 0) const
336 #else // CISST_SVL_HAS_OPENCV
337  cv::Mat CvMatRef(const unsigned int CMN_UNUSED(videochannel) = 0) const
338 #endif // CISST_SVL_HAS_OPENCV
339  {
340 #if CISST_SVL_HAS_OPENCV
341  if (videochannel < _VideoChannels) return cv::Mat(OCVImageHeader[videochannel]);
342  else return cv::Mat();
343 #else // CISST_SVL_HAS_OPENCV
344  CMN_LOG_CLASS_INIT_ERROR << "Class svlSampleImageCustom: CvMatRef() called while OpenCV is disabled" << std::endl;
345  return 0;
346 #endif // CISST_SVL_HAS_OPENCV
347  }
348 
349  unsigned char* GetUCharPointer(const unsigned int videochannel)
350  {
351  return reinterpret_cast<unsigned char*>(GetPointer(videochannel));
352  }
353 
354  const unsigned char* GetUCharPointer(const unsigned int videochannel) const
355  {
356  return reinterpret_cast<const unsigned char*>(GetPointer(videochannel));
357  }
358 
359  unsigned char* GetUCharPointer(const unsigned int videochannel, const unsigned int x, const unsigned int y)
360  {
361  return reinterpret_cast<unsigned char*>(GetPointer(videochannel, x, y));
362  }
363 
364  const unsigned char* GetUCharPointer(const unsigned int videochannel, const unsigned int x, const unsigned int y) const
365  {
366  return reinterpret_cast<const unsigned char*>(GetPointer(videochannel, x, y));
367  }
368 
369  void SetSize(const unsigned int width, const unsigned int height)
370  {
371  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
372  SetSize(vch, width, height);
373  }
374  }
375 
376  void SetSize(const unsigned int videochannel, const unsigned int width, const unsigned int height)
377  {
378  if (OwnData && videochannel < _VideoChannels) {
379  if (GetWidth (videochannel) == width &&
380  GetHeight(videochannel) == height) return;
381  OwnImage[videochannel]->SetSize(height, width * _DataChannels);
382  Image[videochannel].SetRef(*(OwnImage[videochannel]));
383 #if CISST_SVL_HAS_OPENCV
384  if (OCVImageHeader[videochannel]) {
385  cvInitImageHeader(OCVImageHeader[videochannel],
386  cvSize(width, height),
387  GetOCVDepth(),
388  _DataChannels);
389  cvSetData(OCVImageHeader[videochannel],
390  GetPointer(videochannel),
391  width * GetBPP());
392  }
393 #endif // CISST_SVL_HAS_OPENCV
394  }
395  }
396 
397 #if CISST_SVL_HAS_OPENCV
398  int SetSize(const IplImage* ipl_image, const unsigned int videochannel = 0)
399 #else // CISST_SVL_HAS_OPENCV
400  int SetSize(const IplImage* CMN_UNUSED(ipl_image), const unsigned int CMN_UNUSED(videochannel) = 0)
401 #endif // CISST_SVL_HAS_OPENCV
402  {
403 #if CISST_SVL_HAS_OPENCV
404  if (ipl_image && videochannel < _VideoChannels) {
405  SetSize(videochannel, ipl_image->width, ipl_image->height);
406  return SVL_OK;
407  }
408 #endif // CISST_SVL_HAS_OPENCV
409  return SVL_FAIL;
410  }
411 
412 #if CISST_SVL_HAS_OPENCV
413  int SetSize(const cv::Mat& cv_mat, const unsigned int videochannel = 0)
414 #else // CISST_SVL_HAS_OPENCV
415  int SetSize(const cv::Mat& CMN_UNUSED(cv_mat), const unsigned int CMN_UNUSED(videochannel) = 0)
416 #endif // CISST_SVL_HAS_OPENCV
417  {
418 #if CISST_SVL_HAS_OPENCV
419  if (videochannel < _VideoChannels) {
420  SetSize(videochannel, cv_mat.cols, cv_mat.rows);
421  return SVL_OK;
422  }
423 #endif // CISST_SVL_HAS_OPENCV
424  return SVL_FAIL;
425  }
426 
427 #if CISST_SVL_HAS_OPENCV
428  int CopyOf(const IplImage* ipl_image, const unsigned int videochannel = 0)
429 #else // CISST_SVL_HAS_OPENCV
430  int CopyOf(const IplImage* CMN_UNUSED(ipl_image), const unsigned int CMN_UNUSED(videochannel) = 0)
431 #endif // CISST_SVL_HAS_OPENCV
432  {
433 #if CISST_SVL_HAS_OPENCV
434  if (GetOCVImagePixelType(ipl_image) == GetPixelType()) {
435  if (SetSize(ipl_image, videochannel) != SVL_OK) return SVL_FAIL;
436  memcpy(GetUCharPointer(videochannel), ipl_image->imageData, GetDataSize(videochannel));
437  return SVL_OK;
438  }
439 #endif // CISST_SVL_HAS_OPENCV
440  return SVL_FAIL;
441  }
442 
443 #if CISST_SVL_HAS_OPENCV
444  int CopyOf(const cv::Mat& cv_mat, const unsigned int videochannel = 0)
445 #else // CISST_SVL_HAS_OPENCV
446  int CopyOf(const cv::Mat& CMN_UNUSED(cv_mat), const unsigned int CMN_UNUSED(videochannel) = 0)
447 #endif // CISST_SVL_HAS_OPENCV
448  {
449 #if CISST_SVL_HAS_OPENCV
450  if (GetOCVImagePixelType(cv_mat) == GetPixelType()) {
451  if (SetSize(cv_mat, videochannel) != SVL_OK) return SVL_FAIL;
452  memcpy(GetUCharPointer(videochannel), cv_mat.data, GetDataSize(videochannel));
453  return SVL_OK;
454  }
455 #endif // CISST_SVL_HAS_OPENCV
456  return SVL_FAIL;
457  }
458 
459  unsigned int GetVideoChannels() const
460  {
461  return _VideoChannels;
462  }
463 
464  unsigned int GetDataChannels() const
465  {
466  return _DataChannels;
467  }
468 
469  unsigned int GetBPP() const
470  {
471  return (sizeof(_ValueType) * _DataChannels);
472  }
473 
474  unsigned int GetWidth(const unsigned int videochannel = 0) const
475  {
476  if (videochannel < _VideoChannels) return static_cast<unsigned int>(Image[videochannel].width() / _DataChannels);
477  return 0;
478  }
479 
480  unsigned int GetHeight(const unsigned int videochannel = 0) const
481  {
482  if (videochannel < _VideoChannels) return static_cast<unsigned int>(Image[videochannel].height());
483  return 0;
484  }
485 
486  unsigned int GetRowStride(const unsigned int videochannel = 0) const
487  {
488  if (videochannel < _VideoChannels) return static_cast<unsigned int>(Image[videochannel].width());
489  return 0;
490  }
491 
492  unsigned int GetDataSize(const unsigned int videochannel) const
493  {
494  if (videochannel < _VideoChannels) {
495  return (GetBPP() * GetWidth(videochannel) * GetHeight(videochannel));
496  }
497  return 0;
498  }
499 
500  svlSampleImage* GetSubImage(const unsigned int top, const unsigned int height, const unsigned int videochannel = 0)
501  {
502  if (videochannel < _VideoChannels) {
503 
504  const unsigned int width = GetWidth(videochannel);
505 
506  // Create sub-matrix reference
507  vctDynamicMatrixRef<_ValueType> subref(Image[videochannel], top, 0, height, width * _DataChannels);
508 
509  // Create sub-sample image
511 
512  // Set sub-sample image to sub-matrix reference
513  subimage->SetMatrix(subref);
514 
515  return subimage;
516  }
517  return 0;
518  }
519 
520  svlSampleImage* GetSubImage(svlProcInfo* procInfo, const unsigned int videochannel = 0)
521  {
522  if (videochannel < _VideoChannels) {
523 
524  unsigned int height = GetHeight(videochannel);
525  unsigned int from, to;
526 
527  _GetParallelSubRange(procInfo, height, from, to);
528 
529  return GetSubImage(from, to - from, videochannel);
530  }
531  return 0;
532  }
533 
534 
536  // svlSampleImageCustom specific methods //
538 
539  template <class __ownerType>
540  int SetMatrix(vctDynamicMatrixBase<__ownerType, _ValueType>& matrix, unsigned int videochannel = 0)
541  {
542  if (!OwnData && videochannel < _VideoChannels) {
543  Image[videochannel].SetRef(matrix);
544 #if CISST_SVL_HAS_OPENCV
545  if (OCVImageHeader[videochannel]) {
546  cvInitImageHeader(OCVImageHeader[videochannel],
547  cvSize(GetWidth(videochannel), GetHeight(videochannel)),
548  GetOCVDepth(),
549  _DataChannels);
550  cvSetData(OCVImageHeader[videochannel],
551  GetPointer(videochannel),
552  GetWidth(videochannel) * _DataChannels);
553  }
554 #endif // CISST_SVL_HAS_OPENCV
555  return SVL_OK;
556  }
557  return SVL_FAIL;
558  }
559 
560  int SetMatrix(cv::Mat& matrix, unsigned int videochannel = 0)
561  {
562  if (!OwnData && videochannel < _VideoChannels) {
563 #if CISST_SVL_HAS_OPENCV
564  if (GetOCVImagePixelType(matrix) != GetPixelType()) {
565  return SVL_FAIL;
566  }
567  Image[videochannel].SetRef(matrix.rows, matrix.cols, matrix.cols * _DataChannels, _DataChannels, matrix.data);
568  if (OCVImageHeader[videochannel]) {
569  cvInitImageHeader(OCVImageHeader[videochannel],
570  cvSize(GetWidth(videochannel), GetHeight(videochannel)),
571  GetOCVDepth(),
572  _DataChannels);
573  cvSetData(OCVImageHeader[videochannel],
574  GetPointer(videochannel),
575  GetWidth(videochannel) * _DataChannels);
576  }
577  return SVL_OK;
578 #else // CISST_SVL_HAS_OPENCV
579  return SVL_FAIL;
580 #endif // CISST_SVL_HAS_OPENCV
581  }
582  return SVL_FAIL;
583  }
584 
585  vctDynamicMatrixRef<_ValueType> GetMatrixRef(const unsigned int videochannel = 0)
586  {
587  if (videochannel < _VideoChannels) return Image[videochannel];
588  else return InvalidMatrix;
589  }
590 
591  vctDynamicConstMatrixRef<_ValueType> GetMatrixRef(const unsigned int videochannel = 0) const
592  {
593  if (videochannel < _VideoChannels) return Image[videochannel];
594  else return InvalidMatrix;
595  }
596 
597  _ValueType* GetPointer(const unsigned int videochannel = 0)
598  {
599  if (videochannel < _VideoChannels) return Image[videochannel].Pointer();
600  return 0;
601  }
602 
603  const _ValueType* GetPointer(const unsigned int videochannel = 0) const
604  {
605  if (videochannel < _VideoChannels) return Image[videochannel].Pointer();
606  return 0;
607  }
608 
609  _ValueType* GetPointer(const unsigned int videochannel, const unsigned int x, const unsigned int y)
610  {
611  if (videochannel < _VideoChannels) {
612  return Image[videochannel].Pointer(y, x * _DataChannels);
613  }
614  return 0;
615  }
616 
617  const _ValueType* GetPointer(const unsigned int videochannel, const unsigned int x, const unsigned int y) const
618  {
619  if (videochannel < _VideoChannels) {
620  return Image[videochannel].Pointer(y, x * _DataChannels);
621  }
622  return 0;
623  }
624 
625 private:
626  bool OwnData;
627  vctDynamicMatrixRef<_ValueType> Image[_VideoChannels];
628  vctDynamicMatrix<_ValueType>* OwnImage[_VideoChannels];
629  vctDynamicMatrix<_ValueType> InvalidMatrix;
630 
631 #if CISST_SVL_HAS_OPENCV
632  IplImage* OCVImageHeader[_VideoChannels];
633 
634  int GetOCVDepth()
635  {
636  if (IsTypeUInt8 <_ValueType>(static_cast<_ValueType>(0))) return IPL_DEPTH_8U;
637  if (IsTypeUInt16<_ValueType>(static_cast<_ValueType>(0))) return IPL_DEPTH_16U;
638  if (IsTypeFloat <_ValueType>(static_cast<_ValueType>(0))) return IPL_DEPTH_32F;
639  return -1;
640  }
641 
642  svlPixelType GetOCVImagePixelType(const IplImage* ipl_image)
643  {
644  if (!ipl_image) return svlPixelUnknown;
645 
646  if (ipl_image->depth == IPL_DEPTH_8U) {
647  if (ipl_image->nChannels == 1) return svlPixelMono8;
648  if (ipl_image->nChannels == 3) return svlPixelRGB;
649  if (ipl_image->nChannels == 4) return svlPixelRGBA;
650  }
651  else if (ipl_image->depth == IPL_DEPTH_16U) {
652  if (ipl_image->nChannels == 1) return svlPixelMono16;
653  }
654  else if (ipl_image->depth == IPL_DEPTH_32F) {
655  if (ipl_image->nChannels == 1) return svlPixelMonoFloat;
656  if (ipl_image->nChannels == 3) return svlPixel3DFloat;
657  }
658 
659  return svlPixelUnknown;
660  }
661 
662  svlPixelType GetOCVImagePixelType(const cv::Mat cv_mat)
663  {
664  if (cv_mat.depth() == CV_8U) {
665  if (cv_mat.channels() == 1) return svlPixelMono8;
666  if (cv_mat.channels() == 3) return svlPixelRGB;
667  if (cv_mat.channels() == 4) return svlPixelRGBA;
668  }
669  else if (cv_mat.depth() == CV_16U) {
670  if (cv_mat.channels() == 1) return svlPixelMono16;
671  }
672  // There is no 32 bit unsigned integral type cv::Mat in OpenCV
673  else if (cv_mat.depth() == CV_32F) {
674  if (cv_mat.channels() == 1) return svlPixelMonoFloat;
675  if (cv_mat.channels() == 3) return svlPixel3DFloat;
676  }
677 
678  return svlPixelUnknown;
679  }
680 #endif // CISST_SVL_HAS_OPENCV
681 };
682 
683 #endif // _svlSampleImageCustom_h
684 
Definition: svlDefinitions.h:291
int SetSize(const svlSample &sample)
Definition: svlSampleImageCustom.h:187
_ValueType * GetPointer(const unsigned int videochannel=0)
Definition: svlSampleImageCustom.h:597
Definition: svlSampleMatrix.h:34
void SetSize(const unsigned int videochannel, const unsigned int width, const unsigned int height)
Definition: svlSampleImageCustom.h:376
unsigned int GetDataChannels() const
Definition: svlSampleImageCustom.h:464
int SetSize(const cv::Mat &CMN_UNUSED(cv_mat), const unsigned int CMN_UNUSED(videochannel)=0)
Definition: svlSampleImageCustom.h:415
vctDynamicMatrixRef< _ValueType > GetMatrixRef(const unsigned int videochannel=0)
Definition: svlSampleImageCustom.h:585
virtual unsigned int GetHeight(const unsigned int videochannel=0) const =0
Definition: svlDefinitions.h:207
unsigned int GetWidth(const unsigned int videochannel=0) const
Definition: svlSampleImageCustom.h:474
Definition: vctDynamicMatrixBase.h:42
svlStreamType
Definition: svlDefinitions.h:193
unsigned char * GetUCharPointer(const unsigned int videochannel)
Definition: svlSampleImageCustom.h:349
virtual void SerializeRaw(std::ostream &outputStream) const
Definition: svlSampleImageCustom.h:258
size_type width() const
Definition: vctDynamicConstMatrixBase.h:253
int SetMatrix(vctDynamicMatrixBase< __ownerType, _ValueType > &matrix, unsigned int videochannel=0)
Definition: svlSampleImageCustom.h:540
int CopyOf(const svlSample &sample)
Definition: svlSampleImageCustom.h:221
Definition: svlDefinitions.h:295
svlStreamType GetType() const
Definition: svlSampleImageCustom.h:133
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
unsigned int GetHeight(const unsigned int videochannel=0) const
Definition: svlSampleImageCustom.h:480
void SetSize(const unsigned int width, const unsigned int height)
Definition: svlSampleImageCustom.h:369
virtual void SerializeRaw(std::ostream &outputStream) const
virtual void DeSerializeRaw(std::istream &inputStream)
virtual svlStreamType GetType() const =0
int SetMatrix(cv::Mat &matrix, unsigned int videochannel=0)
Definition: svlSampleImageCustom.h:560
#define _GetParallelSubRange(_info, _count, _from, _to)
Definition: svlDefinitions.h:47
virtual svlPixelType GetPixelType() const
unsigned char * GetUCharPointer()
Definition: svlSampleImageCustom.h:243
svlSampleImage * GetSubImage(const unsigned int top, const unsigned int height, const unsigned int videochannel=0)
Definition: svlSampleImageCustom.h:500
svlSampleImageCustom(const svlSampleImageCustom< _ValueType, _DataChannels, _VideoChannels > &other)
Definition: svlSampleImageCustom.h:82
int CopyOf(const IplImage *CMN_UNUSED(ipl_image), const unsigned int CMN_UNUSED(videochannel)=0)
Definition: svlSampleImageCustom.h:430
unsigned char * GetUCharPointer(const unsigned int videochannel, const unsigned int x, const unsigned int y)
Definition: svlSampleImageCustom.h:359
unsigned int GetDataSize(const unsigned int videochannel) const
Definition: svlSampleImageCustom.h:492
const unsigned char * GetUCharPointer(const unsigned int videochannel) const
Definition: svlSampleImageCustom.h:354
virtual unsigned char * GetUCharPointer()=0
void IplImage
Definition: svlTypes.h:70
Definition: svlDefinitions.h:195
Definition: svlDefinitions.h:202
Definition: svlSampleImageCustom.h:37
Definition: svlDefinitions.h:198
svlSampleImageCustom()
Definition: svlSampleImageCustom.h:47
void SetRef(size_type rows, size_type cols, stride_type rowStride, stride_type colStride, pointer dataPointer)
Definition: vctDynamicMatrixRef.h:217
const _ValueType * GetPointer(const unsigned int videochannel=0) const
Definition: svlSampleImageCustom.h:603
const unsigned char * GetUCharPointer() const
Definition: svlSampleImageCustom.h:248
Definition: svlDefinitions.h:208
pointer Pointer(size_type rowIndex, size_type colIndex)
Definition: vctDynamicMatrixBase.h:143
Dynamic matrix referencing existing memory (const)
Definition: vctDynamicConstMatrixRef.h:79
virtual unsigned int GetRows() const =0
unsigned int GetVideoChannels() const
Definition: svlSampleImageCustom.h:459
virtual unsigned int GetVideoChannels() const =0
unsigned int GetBPP() const
Definition: svlSampleImageCustom.h:469
void cmnDeSerializeRaw(std::istream &inputStream, _elementType &data)
Definition: cmnDeSerializer.h:82
const unsigned char * GetUCharPointer(const unsigned int videochannel, const unsigned int x, const unsigned int y) const
Definition: svlSampleImageCustom.h:364
unsigned int GetRowStride(const unsigned int videochannel=0) const
Definition: svlSampleImageCustom.h:486
svlSampleImage * GetSubImage(svlProcInfo *procInfo, const unsigned int videochannel=0)
Definition: svlSampleImageCustom.h:520
void cmnSerializeRaw(std::ostream &outputStream, const _elementType &data)
Definition: cmnSerializer.h:78
Definition: svlDefinitions.h:293
Definition: svlSampleImage.h:35
Declaration of vctDynamicMatrixRef.
virtual void DeSerializeRaw(std::istream &inputStream)
Definition: svlSampleImageCustom.h:286
Macros to export the symbols of cisstStereoVision (in a Dll).
void SetSize(size_type rows, size_type cols, bool storageOrder)
Definition: vctDynamicMatrix.h:364
Definition: svlDefinitions.h:201
void GetEncoder(std::string &codec, int &parameter) const
Definition: svlDefinitions.h:199
_ValueType * GetPointer(const unsigned int videochannel, const unsigned int x, const unsigned int y)
Definition: svlSampleImageCustom.h:609
int CopyOf(const svlSample *sample)
Definition: svlSampleImageCustom.h:207
#define CMN_LOG_CLASS_RUN_ERROR
Definition: cmnLogger.h:117
std::string Mat
Definition: svlTypes.h:72
Definition: svlProcInfo.h:33
cv::Mat CvMatRef(const unsigned int CMN_UNUSED(videochannel)=0) const
Definition: svlSampleImageCustom.h:337
#define CMN_LOG_CLASS_INIT_ERROR
Definition: cmnLogger.h:113
svlSampleImageCustom(bool owndata)
Definition: svlSampleImageCustom.h:62
#define SVL_FAIL
Definition: svlDefinitions.h:124
bool IsInitialized() const
Definition: svlSampleImageCustom.h:234
~svlSampleImageCustom()
Definition: svlSampleImageCustom.h:103
IplImage * IplImageRef(const unsigned int CMN_UNUSED(videochannel)=0) const
Definition: svlSampleImageCustom.h:322
Definition: svlDefinitions.h:294
int CopyOf(const cv::Mat &CMN_UNUSED(cv_mat), const unsigned int CMN_UNUSED(videochannel)=0)
Definition: svlSampleImageCustom.h:446
Definition: svlDefinitions.h:288
void SetTimestamp(double ts)
int SetSize(const IplImage *CMN_UNUSED(ipl_image), const unsigned int CMN_UNUSED(videochannel)=0)
Definition: svlSampleImageCustom.h:400
const int CMN_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:331
Definition: svlDefinitions.h:204
Definition: svlDefinitions.h:289
Definition: svlDefinitions.h:206
int SetSize(const svlSample *sample)
Definition: svlSampleImageCustom.h:167
svlSampleImageCustom & operator=(const svlSampleImageCustom< _ValueType, _DataChannels, _VideoChannels > &other)
Definition: svlSampleImageCustom.h:117
svlSample * GetNewInstance() const
Definition: svlSampleImageCustom.h:128
Definition: svlDefinitions.h:203
Definition: svlDefinitions.h:292
unsigned int GetDataSize() const
Definition: svlSampleImageCustom.h:253
Definition: svlDefinitions.h:200
vctDynamicConstMatrixRef< _ValueType > GetMatrixRef(const unsigned int videochannel=0) const
Definition: svlSampleImageCustom.h:591
static int Read(svlSampleImage &image, const unsigned int videoch, const std::string &filename, bool noresize=false)
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76
svlPixelType
Definition: svlDefinitions.h:286
#define SVL_OK
Definition: svlDefinitions.h:123
Definition: svlDefinitions.h:205
virtual unsigned int GetWidth(const unsigned int videochannel=0) const =0
static int Write(const svlSampleImage &image, const unsigned int videoch, const std::string &filename, const int compression=-1)
Definition: svlSample.h:31
double GetTimestamp() const
size_type height() const
Definition: vctDynamicConstMatrixBase.h:248
const _ValueType * GetPointer(const unsigned int videochannel, const unsigned int x, const unsigned int y) const
Definition: svlSampleImageCustom.h:617
virtual unsigned int GetCols() const =0