split vtk polydata through a plane to two polydata - vtk

My idea is simple:
given a vtk polydata and a vtk plane, get two diferent polydata. This polydatas are the division of the original polydata.

Here's one half of a sphere. Flip the plane over (negate the normal) to get the other half.
#include "vtkActor.h"
#include "vtkClipPolyData.h"
#include "vtkPlane.h"
#include "vtkInteractorObserver.h"
#include "vtkInteractorStyleSwitch.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkSphereSource.h"
using namespace std;
int main(int argc, char ** argv) {
vtkSmartPointer<vtkSphereSource> sphere =
vtkSmartPointer<vtkSphereSource>::New();
vtkSmartPointer<vtkClipPolyData> clip =
vtkSmartPointer<vtkClipPolyData>::New();
clip->SetValue(0);
clip->GenerateClippedOutputOn();
clip->SetInputConnection(sphere->GetOutputPort());
vtkSmartPointer<vtkPlane> plane =
vtkSmartPointer<vtkPlane>::New();
//plane->SetNormal(-1.0, 0.0, 0.0);
plane->SetNormal(1.0, 0.0, 0.0);
clip->SetClipFunction (plane);
vtkSmartPointer<vtkPolyDataMapper> polyDataMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
polyDataMapper->SetInputConnection(clip->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(polyDataMapper);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(800,600);
renderWindow->SetWindowName("VTK");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderWindow->AddRenderer(renderer);
vtkInteractorStyleSwitch * styleSwitch
= vtkInteractorStyleSwitch::SafeDownCast(
renderWindowInteractor->GetInteractorStyle());
if (styleSwitch)
styleSwitch->SetCurrentStyleToTrackballCamera();
renderWindow->Render();
renderWindowInteractor->Start();
}
CMakeLists.txt :
cmake_minimum_required(VERSION 2.6)
project(V)
set(VTK_DIR "VTK_DIR-NOTFOUND"
CACHE PATH "location of VTK libraries" )
set(CMAKE_BUILD_TYPE debug)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(clip clip.cxx)
target_link_libraries(clip ${VTK_LIBRARIES})

Related

Recognize type of noise

What is kind of this noise?
Link :Image
How To remove it? I apply median and fastNlMeansDenoisingColored() functions but no improvement.
Sample programm:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat img,gray,DENO, im_gray, img_bw, img_final;
img = imread("C:/Users/Opencv/Downloads/1.bmp",1);
cvtColor(img,im_gray,CV_RGB2GRAY);
adaptiveThreshold(im_gray, img_bw, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, 105, 1);
dilate(img_bw, img_final, Mat(), Point(-1, -1), 2, 1, 1);
imshow("afa",img_final);
waitkay(0);
return 0;
}
Visual Studio 2012,C++, OpenCV 3.0.0
Regards
I write these codes :
ressu3 = zeros(m,n);
for i=2:m-1
for j=2:n-1
ressu3(i,j)=1/55* median ([I(i-1,j-1),I(i-1,j+1),I(i-1,j),I(i+1,j- 1),I(i+1,j+1),I(i+1,j)]);
end
end
The codes is writen in matlab(for test).
Is it directional median filter?
Try below code for removing Noise,
cv::Mat mainMat1 = cv::imread("input_image_path");
cv::Mat dstMat1, medianMat1, finalDest1;
bilateralFilter(mainMat1, dstMat1, 19, 19 * 2, 19 / 2);
addWeighted(mainMat1, 0.40, dstMat1, 0.60, 0, finalDest1, -1);
medianBlur(mainMat1, medianMat1, 17);
cv::Mat dstination1;
cv::fastNlMeansDenoisingColored(mainMat1,dstination1,10,10,7,21);
Hope This will help.

Transparency on one face of cube OpenScreenGraph

I have imported object (Cube) from 3dsMax in my OSG project in VisualStudio. But I can't find out how to make transparent only one face of this imported cube. this is my code:
#include <osgViewer/Viewer>
#include <iostream>
#include <osg/Group>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/StateSet>
#include <osg/StateAttribute>
#include <osg/CullFace>
#include <osg/Point>
#include <osg/Light>
#include <osg/LightSource>
#include <osg/BlendFunc>
#include <osg/Material>
#include <osg/PolygonMode>
int main(int argc, char** argv)
{
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("cube.3ds"); //Importing model
osg::StateSet* state2 = model->getOrCreateStateSet(); //Creating material
osg::ref_ptr<osg::Material> mat2 = new osg::Material;
mat2->setAlpha(osg::Material::FRONT_AND_BACK, 0.1); //Making alpha channel
state2->setAttributeAndModes( mat2.get() ,
osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
osg::BlendFunc* bf = new //Blending
osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
osg::BlendFunc::ONE_MINUS_DST_COLOR );
state2->setAttributeAndModes(bf);
root->addChild(model.get());
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
viewer.setUpViewOnSingleScreen(0);
return viewer.run();
}
This is my source with just imported file. I've tried implement transparency with multiple passes but have no success.
Is there any method how could i make it ?
The code in the question does make the model transparent. For example, with the cessna model from the OSG data package:
Adding two more models, a box and a sphere, where the box also has blending:
We see that blending is working. If you add another model but it isn't displayed, then probably the plane is being rendered before the other models. If the plane happens to be in front of the other models, even if the plane is transparent, you won't see them; as they don't pass the depth test.
Adding
cessna->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );
cessna->getStateSet()->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
forces the cessna to be rendered after the opaque models.
Also, note that if you provide a blending function, then you don't need to call
cessna->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );
Let's see another scene, where the box is behind the plane and we haven't set the rendering hint:
And now with the rendering hint active:

How to calculate the intersection of a line segment and circle with CGAL

I've been banging my head against a wall trying to understand how to use CGAL's Circular Kernel to calculate the intersection(s) between a line segment (Line_Arc_2) and a Circle (Circle_2). Unfortunately there isn't much in the way of example code for the Circular Kernel, and I'm not finding the reference manual much help.
Here is code that I thought would work, but right now it won't even compile (Mac OS 10.9 using the latest system compiler):
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2)>::type Res;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<Res> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}
I get an error on the result_of line: "error: no type named 'result_type' in...", and a second error that "no viable overloaded '='" is available for the intersection line.
Also, since this would probably be the follow up question once this is working: how do I actually get at the intersection points that are put in the vector? CGAL's documentation suggests to me "result" should contain pairs of a Circular_arc_point_2 and an unsigned int representing its multiplicity. Is this what I will actually get in this case? More generally, does anyone know a good tutorial for using the Circular Kernel and Spherical Kernel intersection routines?
Thanks!
So it seems that result_of doesn't work here, despite being suggested in the CGAL reference manual for the CircularKernel's intersection function.
Here is a different version that seems to work and can properly handle the output:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/iterator.h>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> IsectOutput;
using namespace std;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1.0,0.0), Pt2(0.0,1.0), Pt2(-1.0, 0.0));
LineArc2 l = LineArc2( Pt2(0.0,-2.0), Pt2(0.0,2.0) );
std::vector<IsectOutput> output;
typedef CGAL::Dispatch_output_iterator< CGAL::cpp11::tuple<IsectOutput>,
CGAL::cpp0x::tuple< std::back_insert_iterator<std::vector<IsectOutput> > > > Dispatcher;
Dispatcher disp = CGAL::dispatch_output<IsectOutput>( std::back_inserter(output) );
CGAL::intersection(l, c, disp);
cout << output.size() << endl;
for( const auto& v : output ){
cout << "Point: (" << CGAL::to_double( v.first.x() ) << ", " << CGAL::to_double( v.first.y() ) << "), Mult: "
<< v.second << std::endl;
}
return 0;
}
result_of is working but the operator you are asking for does not exist, you are missing the output iterator.
However, I agree the doc is misleading. I'll try to fix it.
The following code is working fine:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef boost::variant<std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> > InterRes;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2,std::back_insert_iterator<std::vector<InterRes> >)>::type Res;
int main(){
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<InterRes> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}

show first frame in the video using open cv?

I am working on stereo vision.Here i have two stereo videos.I wanted to do following things
1]extract the first frame from the video .
2] convert the frame to gray channel(so that i can apply SURF features)
3]display the first frame (getting error in the code given below)
4]store the video with small size(currently it is 1600*1200)
can anyone give suggestion how to do?
my code is given below(written in c++)
#include <stdio.h>
#include <iostream>
#include <vector>
#include <time.h>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include "opencv2\features2d\features2d.hpp"
#include "opencv2\nonfree\features2d.hpp"
#include "opencv2\nonfree\nonfree.hpp"
#include "opencv2\flann\flann.hpp"
#include "opencv2\contrib\contrib.hpp"
#include <opencv2\calib3d\calib3d.hpp>
#include <opencv2\gpu\gpumat.hpp>
#pragma comment (lib, "opencv_core243d")
#pragma comment (lib, "opencv_highgui243d")
#pragma comment (lib, "opencv_imgproc243d")
#pragma comment (lib, "opencv_features2d243d")
#pragma comment (lib, "opencv_nonfree243d")
#pragma comment (lib, "opencv_flann243d")
#pragma comment (lib, "opencv_contrib243d")
#pragma comment (lib, "opencv_calib3d243d")
#pragma comment (lib, "opencv_gpu243d")
using namespace std;
using namespace cv;
int main(int argc, char**argv)
{
clock_t start_time=clock();
long lframecount=0;
//load the videos
VideoCapture capture1(argv[1]);
VideoCapture capture2(argv[2]);
cout << argv[1] << endl;
cout << argv[2] << endl;
if(!capture1.isOpened()||!capture2.isOpened())
{
cout<<"cant load stereo video";
return -1;
}
Mat frame1,frame2;
capture1>>frame1;
capture2>>frame2;
int num_rows=frame1.rows;
int num_cols=frame1.cols;
std::cout<<"number of rows and colums"<<num_rows<<":"<<num_cols<<std::endl;
int output_rows=num_rows/4;
int output_cols=num_cols/4;
long framecount1= capture1.get(CV_CAP_PROP_FRAME_COUNT);
long framecount2= capture2.get(CV_CAP_PROP_FRAME_COUNT);
VideoWriter write;
write.open("E:\\Vipil\\open_cv_learning\\video\\new22.avi",CV_FOURCC('D','I','V','X'), 30,cv::Size(output_rows,output_cols) ,true);
if (!write.isOpened())
{
std::cout << "cant open output video";
return -1;
}
namedWindow("depthmap",cv::WINDOW_NORMAL);
cv::Point tex(570, 50);
cv::Mat image1;
cv::Mat image2;
for(long i=1;i<6;i++)
{
lframecount++;
int number=lframecount;
string frame;
std::ostringstream convert;
convert<<number;
frame = convert.str();
capture1>> frame1;
capture2>> frame2;
cv::cvtColor(frame1,image1,CV_RGB2GRAY);
cv::cvtColor(frame2,image2,CV_RGB2GRAY);
imshow("image1",frame1);
//cout<<lframecount<<"\n";
}
You will need to use waitKey(30) or some delay to display image correctly.
Also to resize your frame you can simply use OpenCV resize() function.
Here is the reference
http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html

VTK: Intersection between a plane and a polydata

Is there a simple way to compute whether a plane and a polydata-object intersect?
I want to know on which sides my polydata-object protude my bounding box.
I want to use VTK for this task.
Will the vtkIntersectionPolyDataFilter work for you? http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/IntersectionPolyDataFilter
I solved this using a vtkBoundingBox, which has the plane position and a second vtkBoundingBox which got the the bounds from my vtkPolyData.
You can use vtkCutter, like below:
#include <vtkSmartPointer.h>
#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkPlane.h>
#include <vtkCutter.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
int main(int, char *[])
{
vtkSmartPointer<vtkCubeSource> cube =
vtkSmartPointer<vtkCubeSource>::New();
cube->SetXLength(40);
cube->SetYLength(30);
cube->SetZLength(20);
vtkSmartPointer<vtkPolyDataMapper> cubeMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cubeMapper->SetInputConnection(cube->GetOutputPort());
// Create a plane to cut,here it cuts in the XZ direction (xz normal=(1,0,0);XY =(0,0,1),YZ =(0,1,0)
vtkSmartPointer<vtkPlane> plane =
vtkSmartPointer<vtkPlane>::New();
plane->SetOrigin(10,0,0);
plane->SetNormal(1,0,0);
// Create cutter
vtkSmartPointer<vtkCutter> cutter =
vtkSmartPointer<vtkCutter>::New();
cutter->SetCutFunction(plane);
cutter->SetInputConnection(cube->GetOutputPort());
cutter->Update();
vtkSmartPointer<vtkPolyDataMapper> cutterMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cutterMapper->SetInputConnection( cutter->GetOutputPort());
// Create plane actor
vtkSmartPointer<vtkActor> planeActor =
vtkSmartPointer<vtkActor>::New();
planeActor->GetProperty()->SetColor(1.0,1,0);
planeActor->GetProperty()->SetLineWidth(2);
planeActor->SetMapper(cutterMapper);
// Create cube actor
vtkSmartPointer<vtkActor> cubeActor =
vtkSmartPointer<vtkActor>::New();
cubeActor->GetProperty()->SetColor(0.5,1,0.5);
cubeActor->GetProperty()->SetOpacity(0.5);
cubeActor->SetMapper(cubeMapper);
// Create renderers and add actors of plane and cube
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(planeActor); //display the rectangle resulting from the cut
renderer->AddActor(cubeActor); //display the cube
// Add renderer to renderwindow and render
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(600, 600);
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
renderer->SetBackground(0,0,0);
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}

Resources