pull/2173/head
Anatoly Baksheev 11 years ago
parent f930f2f19d
commit a23f1447a8
  1. 8
      modules/viz/src/precomp.hpp
  2. 50
      modules/viz/src/shapes.cpp
  3. 4
      modules/viz/test/tests_simple.cpp

@ -183,10 +183,14 @@ namespace cv
return isNan(data[0]) || isNan(data[1]) || isNan(data[2]);
}
inline vtkSmartPointer<vtkActor> getActor(const Widget3D& widget)
{
return vtkActor::SafeDownCast(WidgetAccessor::getProp(widget));
}
inline vtkSmartPointer<vtkPolyData> getPolyData(const Widget3D& widget)
{
vtkSmartPointer<vtkProp> prop = WidgetAccessor::getProp(widget);
vtkSmartPointer<vtkMapper> mapper = vtkActor::SafeDownCast(prop)->GetMapper();
vtkSmartPointer<vtkMapper> mapper = getActor(widget)->GetMapper();
return vtkPolyData::SafeDownCast(mapper->GetInput());
}

@ -777,28 +777,34 @@ namespace cv { namespace viz { namespace
return extract_edges->GetOutput();
}
static vtkSmartPointer<vtkActor> projectImage(double fovy, double far_end_height, const Mat &image, double scale, const Color &color)
static vtkSmartPointer<vtkActor> projectImage(vtkSmartPointer<vtkPolyData> frustum, double far_end_height, const Mat &image, double scale, const Color &color)
{
float aspect_ratio = float(image.cols)/float(image.rows);
Mat color_image = image;
if (color_image.channels() == 1)
{
color_image.create(image.size(), CV_8UC3);
Vec3b *drow = color_image.ptr<Vec3b>();
for(int y = 0; y < image.rows; ++y)
{
const unsigned char *srow = image.ptr<unsigned char>(y);
const unsigned char *send = srow + image.cols;
for(;srow < send;)
*drow++ = Vec3b::all(*srow++);
}
}
double aspect_ratio = color_image.cols/(double)color_image.rows;
// Create the vtk image
vtkSmartPointer<vtkImageMatSource> source = vtkSmartPointer<vtkImageMatSource>::New();
source->SetImage(image);
source->SetImage(color_image);
source->Update();
vtkSmartPointer<vtkImageData> vtk_image = source->GetOutput();
// Adjust a pixel of the vtk_image
if(image.channels() == 1)
{
double gray = color[2] * 0.299 + color[1] * 0.578 + color[0] * 0.144;
vtk_image->SetScalarComponentFromDouble(0, 0, 0, 0, gray);
}
else
{
vtk_image->SetScalarComponentFromDouble(0, 0, 0, 0, color[2]);
vtk_image->SetScalarComponentFromDouble(0, 0, 0, 1, color[1]);
vtk_image->SetScalarComponentFromDouble(0, 0, 0, 2, color[0]);
}
vtk_image->SetScalarComponentFromDouble(0, 0, 0, 0, color[2]);
vtk_image->SetScalarComponentFromDouble(0, 0, 0, 1, color[1]);
vtk_image->SetScalarComponentFromDouble(0, 0, 0, 2, color[0]);
Vec3d plane_center(0.0, 0.0, scale);
@ -824,7 +830,7 @@ namespace cv { namespace viz { namespace
transform_filter->SetInputConnection(texture_plane->GetOutputPort());
transform_filter->SetTransform(transform);
vtkSmartPointer<vtkPolyData> frustum = createFrustum(aspect_ratio, fovy, scale);
// Frustum needs to be textured or else it can't be combined with image
vtkSmartPointer<vtkTextureMapToPlane> frustum_texture = vtkSmartPointer<vtkTextureMapToPlane>::New();
@ -901,14 +907,15 @@ cv::viz::WCameraPosition::WCameraPosition(const Vec2d &fov, double scale, const
cv::viz::WCameraPosition::WCameraPosition(const Matx33d &K, const Mat &image, double scale, const Color &color)
{
CV_Assert(!image.empty() && image.depth() == CV_8U);
double f_y = K(1,1), c_y = K(1,2);
// Assuming that this is an ideal camera (c_y and c_x are at the center of the image)
double fovy = 2.0 * atan2(c_y, f_y) * 180.0 / CV_PI;
double far_end_height = 2.00 * c_y * scale / f_y;
double aspect_ratio = image.cols/(double)image.rows;
vtkSmartPointer<vtkPolyData> frustum = CameraPositionUtils::createFrustum(aspect_ratio, fovy, scale);
vtkSmartPointer<vtkActor> actor = CameraPositionUtils::projectImage(fovy, far_end_height, image, scale, color);
vtkSmartPointer<vtkActor> actor = CameraPositionUtils::projectImage(frustum, far_end_height, image, scale, color);
WidgetAccessor::setProp(*this, actor);
}
@ -917,8 +924,11 @@ cv::viz::WCameraPosition::WCameraPosition(const Vec2d &fov, const Mat &image, do
CV_Assert(!image.empty() && image.depth() == CV_8U);
double fovy = fov[1] * 180.0 / CV_PI;
double far_end_height = 2.0 * scale * tan(fov[1] * 0.5);
double aspect_ratio = image.cols/(double)image.rows;
vtkSmartPointer<vtkPolyData> frustum = CameraPositionUtils::createFrustum(aspect_ratio, fovy, scale);
vtkSmartPointer<vtkActor> actor = CameraPositionUtils::projectImage(fovy, far_end_height, image, scale, color);
vtkSmartPointer<vtkActor> actor = CameraPositionUtils::projectImage(frustum, far_end_height, image, scale, color);
WidgetAccessor::setProp(*this, actor);
}

@ -174,7 +174,7 @@ TEST(Viz, DISABLED_show_trajectories)
viz.showWidget("sub1", WTrajectory(sub1, WTrajectory::PATH, 0.2, Color::brown()));
viz.showWidget("sub2", WTrajectory(sub2, WTrajectory::FRAMES, 0.2));
viz.showWidget("sub3", WTrajectory(sub3, WTrajectory::BOTH, 0.2, Color::green()));
viz.showWidget("sub4", WTrajectoryFrustums(sub4, K, 0.3));
viz.showWidget("sub4", WTrajectoryFrustums(sub4, K, 0.3, Color::yellow()));
viz.showWidget("sub5", WTrajectoryFrustums(sub5, Vec2d(0.78, 0.78), 0.15));
int i = 0;
@ -212,7 +212,7 @@ TEST(Viz, show_camera_positions)
}
Viz3d viz("show_camera_positions");
viz.showWidget("sphe", WSphere(Point3d(0,0,0), 1.0));
viz.showWidget("sphe", WSphere(Point3d(0,0,0), 1.0, 10, Color::orange_red()));
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]);

Loading…
Cancel
Save