From 4b85c849cabf7edeef69590e840b8f987786cb2a Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Mon, 15 Jul 2013 16:13:33 +0200 Subject: [PATCH] fix rgb and bgr incompatibility --- modules/viz/src/cloud_widgets.cpp | 4 ++-- modules/viz/src/types.cpp | 2 +- modules/viz/src/viz3d_impl.cpp | 6 +++--- modules/viz/src/viz3d_impl.hpp | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/modules/viz/src/cloud_widgets.cpp b/modules/viz/src/cloud_widgets.cpp index 18be023..dbf93f4 100644 --- a/modules/viz/src/cloud_widgets.cpp +++ b/modules/viz/src/cloud_widgets.cpp @@ -116,7 +116,7 @@ cv::viz::CloudWidget::CloudWidget(InputArray _cloud, InputArray _colors) // Filter colors Vec3b* colors_data = new Vec3b[nr_points]; - NanFilter::copy(colors, colors_data, cloud); + NanFilter::copyColor(colors, colors_data, cloud); vtkSmartPointer scalars = vtkSmartPointer::New (); scalars->SetNumberOfComponents (3); @@ -384,7 +384,7 @@ cv::viz::MeshWidget::MeshWidget(const Mesh3d &mesh) { Vec3b * colors_data = 0; colors_data = new Vec3b[nr_points]; - NanFilter::copy(mesh.colors, colors_data, mesh.cloud); + NanFilter::copyColor(mesh.colors, colors_data, mesh.cloud); scalars = vtkSmartPointer::New (); scalars->SetNumberOfComponents (3); diff --git a/modules/viz/src/types.cpp b/modules/viz/src/types.cpp index 7cb25a0..871c2db 100644 --- a/modules/viz/src/types.cpp +++ b/modules/viz/src/types.cpp @@ -111,7 +111,7 @@ struct cv::viz::Mesh3d::loadMeshImpl Vec3b point_color; poly_colors->GetTupleValue (i, point_color.val); - //RGB or BGR? should we swap channels???? + std::swap(point_color[0], point_color[2]); // RGB -> BGR mesh_colors[i] = point_color; } } diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index 0220d9c..ca4b75b 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -847,7 +847,7 @@ bool cv::viz::Viz3d::VizImpl::addModelFromPLYFile (const std::string &filename, return (true); } -bool cv::viz::Viz3d::VizImpl::addPolylineFromPolygonMesh (const Mesh3d& mesh, const std::string &id) +bool cv::viz::Viz3d::VizImpl::addPolylineFromPolygonMesh (const Mesh3d& /*mesh*/, const std::string &/*id*/) { // CV_Assert(mesh.cloud.rows == 1 && mesh.cloud.type() == CV_32FC3); // @@ -1017,7 +1017,7 @@ void cv::viz::Viz3d::VizImpl::setWindowName (const std::string &name) void cv::viz::Viz3d::VizImpl::setWindowPosition (int x, int y) { window_->SetPosition (x, y); } void cv::viz::Viz3d::VizImpl::setWindowSize (int xw, int yw) { window_->SetSize (xw, yw); } -bool cv::viz::Viz3d::VizImpl::addPolygonMesh (const Mesh3d& mesh, const Mat& mask, const std::string &id) +bool cv::viz::Viz3d::VizImpl::addPolygonMesh (const Mesh3d& /*mesh*/, const Mat& /*mask*/, const std::string &/*id*/) { // CV_Assert(mesh.cloud.type() == CV_32FC3 && mesh.cloud.rows == 1 && !mesh.polygons.empty ()); // CV_Assert(mesh.colors.empty() || (!mesh.colors.empty() && mesh.colors.size() == mesh.cloud.size() && mesh.colors.type() == CV_8UC3)); @@ -1205,7 +1205,7 @@ return true; } -bool cv::viz::Viz3d::VizImpl::updatePolygonMesh (const Mesh3d& mesh, const cv::Mat& mask, const std::string &id) +bool cv::viz::Viz3d::VizImpl::updatePolygonMesh (const Mesh3d& /*mesh*/, const cv::Mat& /*mask*/, const std::string &/*id*/) { // CV_Assert(mesh.cloud.type() == CV_32FC3 && mesh.cloud.rows == 1 && !mesh.polygons.empty ()); // CV_Assert(mesh.colors.empty() || (!mesh.colors.empty() && mesh.colors.size() == mesh.cloud.size() && mesh.colors.type() == CV_8UC3)); diff --git a/modules/viz/src/viz3d_impl.hpp b/modules/viz/src/viz3d_impl.hpp index f3f245c..04b816d 100644 --- a/modules/viz/src/viz3d_impl.hpp +++ b/modules/viz/src/viz3d_impl.hpp @@ -327,6 +327,31 @@ namespace cv } return output; } + + static _Out* copyColor(const Mat& source, _Out* output, const Mat& nan_mask) + { + CV_Assert(DataDepth<_Tp>::value == source.depth() && source.size() == nan_mask.size()); + CV_Assert(nan_mask.channels() == 3 || nan_mask.channels() == 4); + CV_DbgAssert(DataDepth<_Msk>::value == nan_mask.depth()); + + int s_chs = source.channels(); + int m_chs = nan_mask.channels(); + + for(int y = 0; y < source.rows; ++y) + { + const _Tp* srow = source.ptr<_Tp>(y); + const _Msk* mrow = nan_mask.ptr<_Msk>(y); + + for(int x = 0; x < source.cols; ++x, srow += s_chs, mrow += m_chs) + if (!isNan(mrow[0]) && !isNan(mrow[1]) && !isNan(mrow[2])) + { + *output = _Out(srow); + std::swap((*output)[0], (*output)[2]); // BGR -> RGB + ++output; + } + } + return output; + } }; template @@ -339,6 +364,17 @@ namespace cv return table[nan_mask.depth() - 5](source, output, nan_mask); } + + template + static inline Vec<_Tp, 3>* copyColor(const Mat& source, Vec<_Tp, 3>* output, const Mat& nan_mask) + { + CV_Assert(nan_mask.depth() == CV_32F || nan_mask.depth() == CV_64F); + + typedef Vec<_Tp, 3>* (*copy_func)(const Mat&, Vec<_Tp, 3>*, const Mat&); + const static copy_func table[2] = { &NanFilter::Impl<_Tp, float>::copyColor, &NanFilter::Impl<_Tp, double>::copyColor }; + + return table[nan_mask.depth() - 5](source, output, nan_mask); + } }; struct ApplyAffine -- 2.7.4