optimized Mat to vtkImageData conversion

This commit is contained in:
Anatoly Baksheev 2014-01-11 22:00:28 +04:00
parent e21f2a81ff
commit cad9a786c4
3 changed files with 27 additions and 23 deletions

View File

@ -127,6 +127,7 @@
#include <vtkTensorGlyph.h> #include <vtkTensorGlyph.h>
#include <vtkImageAlgorithm.h> #include <vtkImageAlgorithm.h>
#include <vtkTransformFilter.h> #include <vtkTransformFilter.h>
#include <vtkStreamingDemandDrivenPipeline.h>
#if !defined(_WIN32) || defined(__CYGWIN__) #if !defined(_WIN32) || defined(__CYGWIN__)
# include <unistd.h> /* unlink */ # include <unistd.h> /* unlink */

View File

@ -44,15 +44,6 @@
#include "precomp.hpp" #include "precomp.hpp"
#include "vtkImageData.h"
#include "vtkImageProgressIterator.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
namespace cv { namespace viz namespace cv { namespace viz
{ {
vtkStandardNewMacro(vtkImageMatSource); vtkStandardNewMacro(vtkImageMatSource);
@ -93,48 +84,60 @@ void cv::viz::vtkImageMatSource::SetImage(InputArray _image)
this->ImageData->SetDimensions(image.cols, image.rows, 1); this->ImageData->SetDimensions(image.cols, image.rows, 1);
#if VTK_MAJOR_VERSION <= 5 #if VTK_MAJOR_VERSION <= 5
this->ImageData->SetNumberOfScalarComponents(std::min(3, image.channels())); this->ImageData->SetNumberOfScalarComponents(image.channels());
this->ImageData->SetScalarTypeToUnsignedChar(); this->ImageData->SetScalarTypeToUnsignedChar();
this->ImageData->AllocateScalars(); this->ImageData->AllocateScalars();
#else #else
this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, std::min(3, image.channels())); this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, image.channels());
#endif #endif
switch(image.channels()) switch(image.channels())
{ {
case 1: copyGrayImage(image, this->ImageData); case 1: copyGrayImage(image, this->ImageData); break;
case 3: copyRGBImage (image, this->ImageData); case 3: copyRGBImage (image, this->ImageData); break;
case 4: copyRGBAImage(image, this->ImageData); case 4: copyRGBAImage(image, this->ImageData); break;
} }
this->ImageData->Modified(); this->ImageData->Modified();
} }
void cv::viz::vtkImageMatSource::copyGrayImage(const Mat &source, vtkSmartPointer<vtkImageData> output) void cv::viz::vtkImageMatSource::copyGrayImage(const Mat &source, vtkSmartPointer<vtkImageData> output)
{ {
unsigned char* dptr = reinterpret_cast<unsigned char*>(output->GetScalarPointer());
size_t elem_step = output->GetIncrements()[1]/sizeof(unsigned char);
for (int y = 0; y < source.rows; ++y) for (int y = 0; y < source.rows; ++y)
{ {
unsigned char* drow = dptr + elem_step * y;
const unsigned char *srow = source.ptr<unsigned char>(y); const unsigned char *srow = source.ptr<unsigned char>(y);
for (int x = 0; x < source.cols; ++x) for (int x = 0; x < source.cols; ++x)
*reinterpret_cast<unsigned char*>(output->GetScalarPointer(x,y,0)) = *srow++; drow[x] = *srow++;
} }
} }
void cv::viz::vtkImageMatSource::copyRGBImage(const Mat &source, vtkSmartPointer<vtkImageData> output) void cv::viz::vtkImageMatSource::copyRGBImage(const Mat &source, vtkSmartPointer<vtkImageData> output)
{ {
Vec3b* dptr = reinterpret_cast<Vec3b*>(output->GetScalarPointer());
size_t elem_step = output->GetIncrements()[1]/sizeof(Vec3b);
for (int y = 0; y < source.rows; ++y) for (int y = 0; y < source.rows; ++y)
{ {
Vec3b* drow = dptr + elem_step * y;
const unsigned char *srow = source.ptr<unsigned char>(y); const unsigned char *srow = source.ptr<unsigned char>(y);
for (int x = 0; x < source.cols; ++x, srow += source.channels()) for (int x = 0; x < source.cols; ++x, srow += source.channels())
*reinterpret_cast<Vec3b*>(output->GetScalarPointer(x,y,0)) = Vec3b(srow[2], srow[1], srow[0]); drow[x] = Vec3b(srow[2], srow[1], srow[0]);
} }
} }
void cv::viz::vtkImageMatSource::copyRGBAImage(const Mat &source, vtkSmartPointer<vtkImageData> output) void cv::viz::vtkImageMatSource::copyRGBAImage(const Mat &source, vtkSmartPointer<vtkImageData> output)
{ {
Vec4b* dptr = reinterpret_cast<Vec4b*>(output->GetScalarPointer());
size_t elem_step = output->GetIncrements()[1]/sizeof(Vec4b);
for (int y = 0; y < source.rows; ++y) for (int y = 0; y < source.rows; ++y)
{ {
Vec4b* drow = dptr + elem_step * y;
const unsigned char *srow = source.ptr<unsigned char>(y); const unsigned char *srow = source.ptr<unsigned char>(y);
for (int x = 0; x < source.cols; ++x, srow += source.channels()) for (int x = 0; x < source.cols; ++x, srow += source.channels())
*reinterpret_cast<Vec4b*>(output->GetScalarPointer(x,y,0)) = Vec4b(srow[2], srow[1], srow[0], srow[3]); drow[x] = Vec4b(srow[2], srow[1], srow[0], srow[3]);
} }
} }

View File

@ -198,7 +198,7 @@ TEST(Viz, DISABLED_show_trajectory_reposition)
viz.spin(); viz.spin();
} }
TEST(Viz, show_camera_positions) TEST(Viz, DISABLED_show_camera_positions)
{ {
Matx33d K(1024.0, 0.0, 320.0, 0.0, 1024.0, 240.0, 0.0, 0.0, 1.0); Matx33d K(1024.0, 0.0, 320.0, 0.0, 1024.0, 240.0, 0.0, 0.0, 1.0);
Mat lena = imread(Path::combine(cvtest::TS::ptr()->get_data_path(), "lena.png")); Mat lena = imread(Path::combine(cvtest::TS::ptr()->get_data_path(), "lena.png"));
@ -216,7 +216,6 @@ TEST(Viz, show_camera_positions)
viz.showWidget("coos", WCoordinateSystem(1.5)); viz.showWidget("coos", WCoordinateSystem(1.5));
viz.showWidget("pos1", WCameraPosition(0.75), poses[0]); viz.showWidget("pos1", WCameraPosition(0.75), poses[0]);
viz.showWidget("pos2", WCameraPosition(Vec2d(0.78, 0.78), lena, 2.2, Color::green()), poses[0]); viz.showWidget("pos2", WCameraPosition(Vec2d(0.78, 0.78), lena, 2.2, Color::green()), poses[0]);
viz.showWidget("pos3", WCameraPosition(0.75), poses[1]); viz.showWidget("pos3", WCameraPosition(0.75), poses[1]);
viz.showWidget("pos4", WCameraPosition(K, gray, 3, Color::indigo()), poses[1]); viz.showWidget("pos4", WCameraPosition(K, gray, 3, Color::indigo()), poses[1]);
viz.spin(); viz.spin();
@ -229,9 +228,10 @@ TEST(Viz, show_overlay_image)
Viz3d viz("show_overlay_image"); Viz3d viz("show_overlay_image");
viz.showWidget("coos", WCoordinateSystem()); viz.showWidget("coos", WCoordinateSystem());
viz.showWidget("cube", WCube(Vec3d::all(-0.5), Vec3d::all(0.5))); viz.showWidget("cube", WCube());
viz.showWidget("img1", WImageOverlay(lena, Rect(Point(0, 400), Size_<double>(viz.getWindowSize()) * 0.5))); viz.showWidget("img1", WImageOverlay(lena, Rect(Point(0, 400), Size2d(viz.getWindowSize()) * 0.5)));
viz.showWidget("img2", WImageOverlay(gray, Rect(Point(640, 0), Size_<double>(viz.getWindowSize()) * 0.5))); viz.showWidget("img2", WImageOverlay(gray, Rect(Point(640, 0), Size2d(viz.getWindowSize()) * 0.5)));
viz.spin();
int i = 0; int i = 0;
while(!viz.wasStopped()) while(!viz.wasStopped())
@ -254,7 +254,7 @@ TEST(Viz, DISABLED_show_image_3d)
Viz3d viz("show_image_3d"); Viz3d viz("show_image_3d");
viz.showWidget("coos", WCoordinateSystem()); viz.showWidget("coos", WCoordinateSystem());
viz.showWidget("cube", WCube(Vec3d::all(-0.5), Vec3d::all(0.5))); viz.showWidget("cube", WCube());
viz.showWidget("arr0", WArrow(Vec3d(0.5, 0.0, 0.0), Vec3d(1.5, 0.0, 0.0), 0.009, Color::raspberry())); viz.showWidget("arr0", WArrow(Vec3d(0.5, 0.0, 0.0), Vec3d(1.5, 0.0, 0.0), 0.009, Color::raspberry()));
viz.showWidget("img0", WImage3D(lena, Size2d(1.0, 1.0)), Affine3d(Vec3d(0.0, CV_PI/2, 0.0), Vec3d(.5, 0.0, 0.0))); viz.showWidget("img0", WImage3D(lena, Size2d(1.0, 1.0)), Affine3d(Vec3d(0.0, CV_PI/2, 0.0), Vec3d(.5, 0.0, 0.0)));
viz.showWidget("arr1", WArrow(Vec3d(-0.5, -0.5, 0.0), Vec3d(0.2, 0.2, 0.0), 0.009, Color::raspberry())); viz.showWidget("arr1", WArrow(Vec3d(-0.5, -0.5, 0.0), Vec3d(0.2, 0.2, 0.0), 0.009, Color::raspberry()));