Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include "vtkImageMatSource.h"
00046 #include <vtkImageData.h>
00047 #include <vtkInformation.h>
00048 #include <vtkInformationVector.h>
00049 #include <vtkStreamingDemandDrivenPipeline.h>
00050 #include <vtkObjectFactory.h>
00051
00052 namespace rtabmap {
00053 vtkStandardNewMacro(vtkImageMatSource);
00054 }
00055
00056 rtabmap::vtkImageMatSource::vtkImageMatSource()
00057 {
00058 this->SetNumberOfInputPorts(0);
00059 this->ImageData = vtkSmartPointer<vtkImageData>::New();
00060 }
00061
00062 int rtabmap::vtkImageMatSource::RequestInformation(vtkInformation *, vtkInformationVector**, vtkInformationVector *outputVector)
00063 {
00064 vtkInformation* outInfo = outputVector->GetInformationObject(0);
00065
00066 outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), this->ImageData->GetExtent(), 6);
00067 outInfo->Set(vtkDataObject::SPACING(), 1.0, 1.0, 1.0);
00068 outInfo->Set(vtkDataObject::ORIGIN(), 0.0, 0.0, 0.0);
00069
00070 vtkDataObject::SetPointDataActiveScalarInfo(outInfo, this->ImageData->GetScalarType(), this->ImageData->GetNumberOfScalarComponents());
00071 return 1;
00072 }
00073
00074 int rtabmap::vtkImageMatSource::RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector *outputVector)
00075 {
00076 vtkInformation *outInfo = outputVector->GetInformationObject(0);
00077
00078 vtkImageData *output = vtkImageData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()) );
00079 output->ShallowCopy(this->ImageData);
00080 return 1;
00081 }
00082
00083 void rtabmap::vtkImageMatSource::SetImage(cv::InputArray _image)
00084 {
00085 CV_Assert(_image.depth() == CV_8U && (_image.channels() == 1 || _image.channels() == 3 || _image.channels() == 4));
00086
00087 cv::Mat image = _image.getMat();
00088
00089 this->ImageData->SetDimensions(image.cols, image.rows, 1);
00090 #if VTK_MAJOR_VERSION <= 5
00091 this->ImageData->SetNumberOfScalarComponents(image.channels());
00092 this->ImageData->SetScalarTypeToUnsignedChar();
00093 this->ImageData->AllocateScalars();
00094 #else
00095 this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, image.channels());
00096 #endif
00097
00098 switch(image.channels())
00099 {
00100 case 1: copyGrayImage(image, this->ImageData); break;
00101 case 3: copyRGBImage (image, this->ImageData); break;
00102 case 4: copyRGBAImage(image, this->ImageData); break;
00103 }
00104 this->ImageData->Modified();
00105 }
00106
00107 void rtabmap::vtkImageMatSource::copyGrayImage(const cv::Mat &source, vtkSmartPointer<vtkImageData> output)
00108 {
00109 unsigned char* dptr = reinterpret_cast<unsigned char*>(output->GetScalarPointer());
00110 size_t elem_step = output->GetIncrements()[1]/sizeof(unsigned char);
00111
00112 for (int y = 0; y < source.rows; ++y)
00113 {
00114 unsigned char* drow = dptr + elem_step * y;
00115 const unsigned char *srow = source.ptr<unsigned char>(source.rows-(y+1));
00116 for (int x = 0; x < source.cols; ++x)
00117 drow[x] = *srow++;
00118 }
00119 }
00120
00121 void rtabmap::vtkImageMatSource::copyRGBImage(const cv::Mat &source, vtkSmartPointer<vtkImageData> output)
00122 {
00123 cv::Vec3b* dptr = reinterpret_cast<cv::Vec3b*>(output->GetScalarPointer());
00124 size_t elem_step = output->GetIncrements()[1]/sizeof(cv::Vec3b);
00125
00126 for (int y = 0; y < source.rows; ++y)
00127 {
00128 cv::Vec3b* drow = dptr + elem_step * y;
00129 const unsigned char *srow = source.ptr<unsigned char>(source.rows - (y + 1));
00130 for (int x = 0; x < source.cols; ++x, srow += source.channels())
00131 drow[x] = cv::Vec3b(srow[2], srow[1], srow[0]);
00132 }
00133 }
00134
00135 void rtabmap::vtkImageMatSource::copyRGBAImage(const cv::Mat &source, vtkSmartPointer<vtkImageData> output)
00136 {
00137 cv::Vec4b* dptr = reinterpret_cast<cv::Vec4b*>(output->GetScalarPointer());
00138 size_t elem_step = output->GetIncrements()[1]/sizeof(cv::Vec4b);
00139
00140 for (int y = 0; y < source.rows; ++y)
00141 {
00142 cv::Vec4b* drow = dptr + elem_step * y;
00143 const unsigned char *srow = source.ptr<unsigned char>(source.rows - (y + 1));
00144 for (int x = 0; x < source.cols; ++x, srow += source.channels())
00145 drow[x] = cv::Vec4b(srow[2], srow[1], srow[0], srow[3]);
00146 }
00147 }