<div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div class="h5">Dear David, <br></div></div></blockquote><div><br>
thank you for the example, there were some things that were different: <br>this bit in the code I certainly coded differently than in the example:<br><br> histogram->SetComponentExtent(0, 4000, 0, 0, 0, 0);<br><div id=":1av" class="ii gt">
histogram->SetComponentOrigin(0, 0, 0);<br> histogram->SetComponentSpacing(0.16, 0.16, 3.26); // component spacing: depends on spacing of input volume<br><br>I'm still a bit confused about the componentspacing.. as described above, I assumed it to be dependent on the input volume,<br>
but it is not???<br><br>Anyway, I've extended your example a bit with writing the corresponding intensity levels to a file, for opening in a spreadsheet program.<br>I'm not sure if it's too basic to put in the Examples section, but it helped me and maybe someone else could benefit from it too.<br>
<br><br>Michael<br><br><br>#include <vtkActor.h><br>#include <vtkImageAccumulate.h><br>#include <vtkImageData.h><br>#include <vtkImageExtractComponents.h><br>#include <vtkJPEGReader.h><br>#include <vtkRenderer.h><br>
#include <vtkRenderWindow.h><br>#include <vtkRenderWindowInteractor.h><br>#include <vtkSmartPointer.h><br>#include <vtkStdString.h><br>#include <vtkXYPlotActor.h><br>#include <vtkPointData.h><br>
<br>int main( int argc, char *argv[] )<br>{<br> // Handle the arguments<br> if( argc < 2 )<br> {<br> vtkstd::cout << "Required arguments: filename.jpg, [optional ignore zero:] <y/n>" << vtkstd::endl;<br>
return EXIT_FAILURE;<br> }<br> <br> int ignoreZero = 0;<br> if( argc == 3 )<br> {<br> vtkStdString ignore = argv[2];<br> cout << ignore << endl;<br> if( ignore == "y" || ignore == "Y" )<br>
{<br> ignoreZero = 1;<br> }<br> }<br> <br> // Read a jpeg image<br> //<br> vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();<br> if( !reader->CanReadFile( argv[1] ) )<br>
{<br> vtkstd::cout << "Error: cannot read " << argv[1] << vtkstd::endl;<br> return EXIT_FAILURE;<br> }<br> reader->SetFileName( argv[1] );<br> reader->Update();<br> <br> int numComponents = reader->GetOutput()->GetNumberOfScalarComponents();<br>
if( numComponents > 3 )<br> {<br> vtkstd::cout << "Error: cannot process an image with " << numComponents << " components!" << vtkstd::endl;<br> return EXIT_FAILURE;<br>
}<br> <br> // Create a vtkXYPlotActor<br> //<br> vtkSmartPointer<vtkXYPlotActor> plot = vtkSmartPointer<vtkXYPlotActor>::New();<br> plot->ExchangeAxesOff();<br> plot->SetLabelFormat( "%g" );<br>
plot->SetXTitle( "Level" );<br> plot->SetYTitle( "Frequency" );<br> plot->SetXValuesToValue();<br> <br> double xmax = 0.;<br> double ymax = 0.;<br> <br> double colors[3][3] = {<br> { 1, 0, 0 },<br>
{ 0, 1, 0 },<br> { 0, 0, 1 } };<br> <br> const char* labels[3] = {<br> "Red", "Green", "Blue" };<br> <br> // Process the image, extracting and plotting a histogram for each component<br>
//<br> //<br> <br> ofstream myfile;<br> myfile.open ("histogram.txt");<br> myfile << "Pixel intensities and their counts.\n";<br><br> for( int i = 0; i < numComponents; ++i )<br>
{<br> vtkSmartPointer<vtkImageExtractComponents> extract = vtkSmartPointer<vtkImageExtractComponents>::New();<br> extract->SetInputConnection( reader->GetOutputPort() );<br> extract->SetComponents( i );<br>
extract->Update();<br> <br> double range[2];<br> extract->GetOutput()->GetScalarRange( range );<br> vtkSmartPointer<vtkImageAccumulate> histogram = vtkSmartPointer<vtkImageAccumulate>::New();<br>
histogram->SetInputConnection( extract->GetOutputPort() );<br> histogram->SetComponentExtent( 0,static_cast<int>(range[1])-static_cast<int>(range[0])-1,0,0,0,0 );<br> histogram->SetComponentOrigin( range[0],0,0 );<br>
histogram->SetComponentSpacing( 1,0,0 );<br> histogram->SetIgnoreZero( ignoreZero );<br> histogram->Update();<br> <br> if( range[1] > xmax ) xmax = range[1];<br> if( histogram->GetOutput()->GetScalarRange()[1] > ymax ) ymax = histogram->GetOutput()->GetScalarRange()[1];<br>
<br> plot->AddInput( histogram->GetOutput() );<br> <br> if( numComponents > 1 )<br> {<br> plot->SetPlotColor(i,colors[i]);<br> plot->SetPlotLabel(i,labels[i]);<br> plot->LegendOn();<br>
<br> myfile << "\n" << labels[i] << "\n\n";<br> }<br><br> unsigned int temp = (range[1]-1);<br> unsigned int frequency[numLevels];<br><br> std::cout<< "range: " << range[0] << " " << range[1] <<std::endl;<br>
<br> unsigned int j;<br><br> for(j=0;j<temp;j++){<br> frequency[j]=0;<br> frequency[j] = histogram->GetOutput()->GetPointData()->GetScalars()->GetTuple1(j);<br> myfile << j << "\t" << frequency[j] << "\n";<br>
}<br> }<br><br> myfile.close();<br> <br> plot->SetXRange( 0, xmax );<br> plot->SetYRange( 0, ymax );<br> <br> // Visualize the histogram(s)<br> vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();<br>
renderer->AddActor(plot);<br> <br> vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();<br> renderWindow->AddRenderer( renderer );<br> renderWindow->SetSize(640, 480);<br>
<br> vtkSmartPointer<vtkRenderWindowInteractor> interactor =<br> vtkSmartPointer<vtkRenderWindowInteractor>::New();<br> interactor->SetRenderWindow( renderWindow );<br> <br> // Initialize the event loop and then start it<br>
interactor->Initialize();<br> interactor->Start(); <br> <br><br> return EXIT_SUCCESS;<br>}<br><br><br></div><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div class="h5">
<br>
</div></div>I reorganized the code a little bit, changed the input to a jpeg, and<br>
posted an example in the "broken" section:<br>
<a href="http://www.cmake.org/Wiki/VTK/Examples/Histogram" target="_blank">http://www.cmake.org/Wiki/VTK/Examples/Histogram</a><br>
<br>
Maybe this will double your chances of someone seeing it and figuring<br>
out what is wrong :)<br>
<br>
Thanks,<br>
<br>
David<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>
</blockquote></div><br>