<div dir="ltr"><div><div>Have you tried to use <font color="#006600">vtkMetaImageWriter</font><br></div>to write data for your code, so you can stop questioning if problem is with the way you write jpg?<br><br></div>Alex<br>
<div><div><br><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, May 9, 2013 at 11:35 AM, Marc Huber <span dir="ltr"><<a href="mailto:marchuber.hft@gmx.de" target="_blank">marchuber.hft@gmx.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF">
Hi,<br>
<br>
I wanted to cut a polydata object in several slices<br>
<br>
When I apply this example it works all fine:<br>
<a href="http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataContourToImageData" target="_blank">http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataContourToImageData</a><br>
<br>
But I have a polydata object and no sphere and I want to produce a
*.jpg file.<br>
My result is just a black image and I'm wondering what the problem
might be.<br>
Has anyone an idea?<br>
<br>
Here is my code (Original code commented out and mine in red):<br>
<br>
<font color="#006600">//ORIGINAL CODE<br>
//vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();<br>
// sphereSource->SetPhiResolution(30);<br>
// sphereSource->SetThetaResolution(30);<br>
// sphereSource->SetCenter(40, 40, 0);<br>
// sphereSource->SetRadius(20);</font><br>
<br>
<font color="#ff0000"> //MY CODE<br>
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New(); <br>
polydata->ShallowCopy(stlActor->GetMapper()->GetInputAsDataSet());</font><br>
<br>
vtkSmartPointer<vtkCutter> circleCutter =
vtkSmartPointer<vtkCutter>::New();<br>
<font color="#006600">
//circleCutter->SetInputConnection(sphereSource->GetOutputPort());
//ORIGINAL</font><br>
<font color="#ff0000">
circleCutter->SetInputConnection(polydata->GetProducerPort());
//MY CODE</font><br>
vtkSmartPointer<vtkPlane> cutPlane =
vtkSmartPointer<vtkPlane>::New();<br>
<font color="#006600">
//cutPlane->SetOrigin(sphereSource->GetCenter()); //ORIGINAL</font><br>
<font color="#ff0000">
cutPlane->SetOrigin(polydata->GetCenter()); //MY CODE</font><br>
cutPlane->SetNormal(0, 0, 1);<br>
circleCutter->SetCutFunction(cutPlane);<br>
vtkSmartPointer<vtkStripper> stripper =
vtkSmartPointer<vtkStripper>::New();<br>
stripper->SetInputConnection(circleCutter->GetOutputPort());
// valid circle<br>
stripper->Update();<br>
// that's our circle<br>
vtkSmartPointer<vtkPolyData> circle =
stripper->GetOutput();<br>
<br>
// write circle out<br>
vtkSmartPointer<vtkXMLPolyDataWriter> polyDataWriter =
vtkSmartPointer<vtkXMLPolyDataWriter>::New();<br>
polyDataWriter->SetInput(circle);<br>
polyDataWriter->SetFileName("circle.vtp");<br>
polyDataWriter->SetCompressorTypeToNone();<br>
polyDataWriter->SetDataModeToAscii();<br>
polyDataWriter->Write();<br>
<br>
// prepare the binary image's voxel grid <br>
double bounds[6];<br>
circle->GetBounds(bounds);<br>
double spacing[3]; // desired volume spacing<br>
spacing[0] = 0.5;<br>
spacing[1] = 0.5;<br>
spacing[2] = 0.5;<br>
vtkSmartPointer<vtkImageData> whiteImage =
vtkSmartPointer<vtkImageData>::New();<br>
whiteImage->SetSpacing(spacing);<br>
<br>
// compute dimensions<br>
int dim[3];<br>
for (int i = 0; i < 3; i++)<br>
{<br>
dim[i] = static_cast<int>(ceil((bounds[i * 2 + 1] -
bounds[i * 2]) /spacing[i])) + 1;<br>
if (dim[i] < 1){ dim[i] = 1;}<br>
}<br>
whiteImage->SetDimensions(dim);<br>
whiteImage->SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2]
- 1);<br>
double origin[3];<br>
// NOTE: I am not sure whether or not we had to add some offset!<br>
origin[0] = bounds[0];// + spacing[0] / 2;<br>
origin[1] = bounds[2];// + spacing[1] / 2;<br>
origin[2] = bounds[4];// + spacing[2] / 2;<br>
whiteImage->SetOrigin(origin);<br>
whiteImage->SetScalarTypeToUnsignedChar();<br>
whiteImage->AllocateScalars();<br>
// fill the image with foreground voxels:<br>
unsigned char inval = 255;<br>
unsigned char outval = 0;<br>
vtkIdType count = whiteImage->GetNumberOfPoints();<br>
for (vtkIdType i = 0; i < count; ++i)<br>
{<br>
whiteImage->GetPointData()->GetScalars()->SetTuple1(i,
inval);<br>
}<br>
<br>
// sweep polygonal data (this is the important thing with
contours!)<br>
vtkSmartPointer<vtkLinearExtrusionFilter> extruder =<br>
vtkSmartPointer<vtkLinearExtrusionFilter>::New();<br>
extruder->SetInput(circle);<br>
extruder->SetScaleFactor(1.);<br>
extruder->SetExtrusionTypeToNormalExtrusion();<br>
extruder->SetVector(0, 0, 1);<br>
extruder->Update();<br>
<br>
// polygonal data --> image stencil:<br>
vtkSmartPointer<vtkPolyDataToImageStencil> pol2stenc =
vtkSmartPointer<vtkPolyDataToImageStencil>::New();<br>
pol2stenc->SetTolerance(0); // important if
extruder->SetVector(0, 0, 1) !!!<br>
pol2stenc->SetInputConnection(extruder->GetOutputPort());<br>
pol2stenc->SetOutputOrigin(origin);<br>
pol2stenc->SetOutputSpacing(spacing);<br>
pol2stenc->SetOutputWholeExtent(whiteImage->GetExtent());<br>
pol2stenc->Update();<br>
<br>
// cut the corresponding white image and set the background:<br>
vtkSmartPointer<vtkImageStencil> imgstenc =
vtkSmartPointer<vtkImageStencil>::New();<br>
imgstenc->SetInput(whiteImage);<br>
imgstenc->SetStencil(pol2stenc->GetOutput());<br>
imgstenc->ReverseStencilOff();<br>
imgstenc->SetBackgroundValue(outval);<br>
imgstenc->Update();<br>
<br>
<font color="#006600"> //ORIGINAL CODE<br>
//vtkSmartPointer<vtkMetaImageWriter> imageWriter =<br>
// vtkSmartPointer<vtkMetaImageWriter>::New();<br>
//imageWriter->SetFileName("labelImage.mhd");<br>
//imageWriter->SetInputConnection(imgstenc->GetOutputPort());<br>
//imageWriter->Write();</font><br>
<br>
<font color="#ff0000"> //MY CODE<br>
vtkSmartPointer<vtkJPEGWriter> cutWriter =
vtkSmartPointer<vtkJPEGWriter>::New();<br>
cutWriter->SetFileName("cut1.jpg");<br>
cutWriter->SetInputConnection(imgstenc->GetOutputPort());<br>
cutWriter->Write(); </font><br><span class="HOEnZb"><font color="#888888">
<pre cols="72">--
Best regards / Mit freundlichen Grüßen
Marc Huber</pre>
</font></span></div>
<br>_______________________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
<br></blockquote></div><br></div>