Dear David,<br><br>Actually, I took Dean's examples and added a bit of code that saves the frequencies<br>and their components to a text file. The text file can be opened in a spreadsheet program,<br>so you can produce the same plot.<br>
<br>It's not a very significant change, might be useful for people that are writing about<br>their data set?<br><br>Michael<br><br><div class="gmail_quote">On Sat, Jan 9, 2010 at 12:08 AM, David Doria <span dir="ltr"><<a href="mailto:daviddoria%2Bvtk@gmail.com">daviddoria+vtk@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">On Thu, Jan 7, 2010 at 9:31 AM, michiel mentink<br>
<div><div></div><div class="h5"><<a href="mailto:michael.mentink@st-hughs.ox.ac.uk">michael.mentink@st-hughs.ox.ac.uk</a>> wrote:<br>
> oops. Forgot a small error. Correct now.<br>
><br>
><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<br>
> 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 =<br>
> 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<br>
> << " components!" << vtkstd::endl;<br>
> return EXIT_FAILURE;<br>
> }<br>
><br>
> // Create a vtkXYPlotActor<br>
> //<br>
> vtkSmartPointer<vtkXYPlotActor> plot =<br>
> 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<br>
> 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 =<br>
> 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 =<br>
> vtkSmartPointer<vtkImageAccumulate>::New();<br>
> histogram->SetInputConnection( extract->GetOutputPort() );<br>
> histogram->SetComponentExtent(<br>
> 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 =<br>
> 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[temp];<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] =<br>
> 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 =<br>
> vtkSmartPointer<vtkRenderer>::New();<br>
> renderer->AddActor(plot);<br>
><br>
> vtkSmartPointer<vtkRenderWindow> renderWindow =<br>
> 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>
> On Thu, Jan 7, 2010 at 2:29 PM, michiel mentink<br>
> <<a href="mailto:michael.mentink@st-hughs.ox.ac.uk">michael.mentink@st-hughs.ox.ac.uk</a>> wrote:<br>
>>><br>
>>> Dear David,<br>
>><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>
>> histogram->SetComponentOrigin(0, 0, 0);<br>
>> histogram->SetComponentSpacing(0.16, 0.16, 3.26); // component spacing:<br>
>> depends on spacing of input volume<br>
>><br>
>> I'm still a bit confused about the componentspacing.. as described above,<br>
>> 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<br>
>> 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<br>
>> 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<br>
>> 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 =<br>
>> 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 " <<<br>
>> numComponents << " components!" << vtkstd::endl;<br>
>> return EXIT_FAILURE;<br>
>> }<br>
>><br>
>> // Create a vtkXYPlotActor<br>
>> //<br>
>> vtkSmartPointer<vtkXYPlotActor> plot =<br>
>> 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<br>
>> 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 =<br>
>> 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 =<br>
>> vtkSmartPointer<vtkImageAccumulate>::New();<br>
>> histogram->SetInputConnection( extract->GetOutputPort() );<br>
>> histogram->SetComponentExtent(<br>
>> 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 =<br>
>> 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] =<br>
>> 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 =<br>
>> vtkSmartPointer<vtkRenderer>::New();<br>
>> renderer->AddActor(plot);<br>
>><br>
>> vtkSmartPointer<vtkRenderWindow> renderWindow =<br>
>> 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>
>><br>
>>><br>
>>> 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>
</div></div>Michiel,<br>
<br>
Dean has kindly been working on some examples:<br>
<div class="im"><br>
<a href="http://www.cmake.org/Wiki/VTK/Examples/Histogram" target="_blank">http://www.cmake.org/Wiki/VTK/Examples/Histogram</a><br>
</div><a href="http://www.cmake.org/Wiki/VTK/Examples/HistogramBarChart" target="_blank">http://www.cmake.org/Wiki/VTK/Examples/HistogramBarChart</a><br>
<br>
Have you done something that is not present in one of these? If so,<br>
can you explain the difference and we'll work together to create a<br>
third example.<br>
<div><div></div><div class="h5"><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>
</div></div></blockquote></div><br>