From cad9a786c4d64b9a4df0686e2f4cea345fea3f3c Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Sat, 11 Jan 2014 22:00:28 +0400 Subject: [PATCH] optimized Mat to vtkImageData conversion --- modules/viz/src/precomp.hpp | 1 + modules/viz/src/vtk/vtkImageMatSource.cpp | 37 ++++++++++++----------- modules/viz/test/tests_simple.cpp | 12 ++++---- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/modules/viz/src/precomp.hpp b/modules/viz/src/precomp.hpp index 98ab3ce63a..9c76d9dead 100644 --- a/modules/viz/src/precomp.hpp +++ b/modules/viz/src/precomp.hpp @@ -127,6 +127,7 @@ #include #include #include +#include #if !defined(_WIN32) || defined(__CYGWIN__) # include /* unlink */ diff --git a/modules/viz/src/vtk/vtkImageMatSource.cpp b/modules/viz/src/vtk/vtkImageMatSource.cpp index 2147039d71..54500cb4bc 100644 --- a/modules/viz/src/vtk/vtkImageMatSource.cpp +++ b/modules/viz/src/vtk/vtkImageMatSource.cpp @@ -44,15 +44,6 @@ #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 { vtkStandardNewMacro(vtkImageMatSource); @@ -93,48 +84,60 @@ void cv::viz::vtkImageMatSource::SetImage(InputArray _image) this->ImageData->SetDimensions(image.cols, image.rows, 1); #if VTK_MAJOR_VERSION <= 5 - this->ImageData->SetNumberOfScalarComponents(std::min(3, image.channels())); + this->ImageData->SetNumberOfScalarComponents(image.channels()); this->ImageData->SetScalarTypeToUnsignedChar(); this->ImageData->AllocateScalars(); #else - this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, std::min(3, image.channels())); + this->ImageData->AllocateScalars(VTK_UNSIGNED_CHAR, image.channels()); #endif switch(image.channels()) { - case 1: copyGrayImage(image, this->ImageData); - case 3: copyRGBImage (image, this->ImageData); - case 4: copyRGBAImage(image, this->ImageData); + case 1: copyGrayImage(image, this->ImageData); break; + case 3: copyRGBImage (image, this->ImageData); break; + case 4: copyRGBAImage(image, this->ImageData); break; } this->ImageData->Modified(); } void cv::viz::vtkImageMatSource::copyGrayImage(const Mat &source, vtkSmartPointer output) { + unsigned char* dptr = reinterpret_cast(output->GetScalarPointer()); + size_t elem_step = output->GetIncrements()[1]/sizeof(unsigned char); + for (int y = 0; y < source.rows; ++y) { + unsigned char* drow = dptr + elem_step * y; const unsigned char *srow = source.ptr(y); for (int x = 0; x < source.cols; ++x) - *reinterpret_cast(output->GetScalarPointer(x,y,0)) = *srow++; + drow[x] = *srow++; } } void cv::viz::vtkImageMatSource::copyRGBImage(const Mat &source, vtkSmartPointer output) { + Vec3b* dptr = reinterpret_cast(output->GetScalarPointer()); + size_t elem_step = output->GetIncrements()[1]/sizeof(Vec3b); + for (int y = 0; y < source.rows; ++y) { + Vec3b* drow = dptr + elem_step * y; const unsigned char *srow = source.ptr(y); for (int x = 0; x < source.cols; ++x, srow += source.channels()) - *reinterpret_cast(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 output) { + Vec4b* dptr = reinterpret_cast(output->GetScalarPointer()); + size_t elem_step = output->GetIncrements()[1]/sizeof(Vec4b); + for (int y = 0; y < source.rows; ++y) { + Vec4b* drow = dptr + elem_step * y; const unsigned char *srow = source.ptr(y); for (int x = 0; x < source.cols; ++x, srow += source.channels()) - *reinterpret_cast(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]); } } diff --git a/modules/viz/test/tests_simple.cpp b/modules/viz/test/tests_simple.cpp index ea8a0b0383..6bca255881 100644 --- a/modules/viz/test/tests_simple.cpp +++ b/modules/viz/test/tests_simple.cpp @@ -198,7 +198,7 @@ TEST(Viz, DISABLED_show_trajectory_reposition) 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); 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("pos1", WCameraPosition(0.75), 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("pos4", WCameraPosition(K, gray, 3, Color::indigo()), poses[1]); viz.spin(); @@ -229,9 +228,10 @@ TEST(Viz, show_overlay_image) Viz3d viz("show_overlay_image"); viz.showWidget("coos", WCoordinateSystem()); - viz.showWidget("cube", WCube(Vec3d::all(-0.5), Vec3d::all(0.5))); - viz.showWidget("img1", WImageOverlay(lena, Rect(Point(0, 400), Size_(viz.getWindowSize()) * 0.5))); - viz.showWidget("img2", WImageOverlay(gray, Rect(Point(640, 0), Size_(viz.getWindowSize()) * 0.5))); + viz.showWidget("cube", WCube()); + viz.showWidget("img1", WImageOverlay(lena, Rect(Point(0, 400), Size2d(viz.getWindowSize()) * 0.5))); + viz.showWidget("img2", WImageOverlay(gray, Rect(Point(640, 0), Size2d(viz.getWindowSize()) * 0.5))); + viz.spin(); int i = 0; while(!viz.wasStopped()) @@ -254,7 +254,7 @@ TEST(Viz, DISABLED_show_image_3d) Viz3d viz("show_image_3d"); 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("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()));