Luis and Jim,<br><br>I will try to describe what I'm trying to do more clearly. I want to create a colored output image that is identical to pixel spacing and size as the input image. An index and size specified at the command prompt will provide a region in which the colors of the input image will be averaged and outputed as one value in the exact same corresponding region in the output image. It will produce an output image that looks like the input image except for the 'color smearing' in region of interest.
<br><br>I tried setting the index to [0,0] and non-zero values at the command prompt but the *.exe still crashes so I don't think it would be a problem with the ImageFileWriter. Below is the whole file..<br><br>Jared<br>
<br>#include "itkImage.h"<br>#include "itkImageRegionConstIterator.h"<br>#include "itkImageRegionIterator.h"<br>#include "itkImageFileReader.h"<br>#include "itkImageFileWriter.h
"<br><br>int main( int argc, char *argv[] ) {<br><br> // Verify the number of parameters on the command line.<br> if ( argc < 7 ) {<br> std::cerr << "Missing parameters. " << std::endl;
<br> std::cerr << "Usage: " << std::endl;<br> std::cerr << argv[0]<br> << " inputImageFile outputImageFile originX originY lengthX lengthY"<br> << std::endl;
<br> return -1;<br> }<br><br> // Color Pixel Type<br> typedef itk::RGBPixel<unsigned char> PixelType;<br> // ND Type<br> const unsigned int Dimension = 2;<br> // Image Type<br> typedef itk::Image< PixelType, Dimension > ImageType;
<br> // Image File Reader Type<br> typedef itk::ImageFileReader< ImageType > ReaderType;<br> // Image File Writer Type<br> typedef itk::ImageFileWriter< ImageType > WriterType;<br> // Image Region Constant Iterator Type
<br> typedef itk::ImageRegionConstIterator<ImageType><br> ConstIteratorType;<br> // Image Region Iterator Type<br> typedef itk::ImageRegionIterator<ImageType><br> IteratorType;
<br><br> ////////////////////////////////////////////////////////////////<br> /* try updating the reader */<br> //<br> ReaderType::Pointer reader = ReaderType::New();<br> reader->SetFileName(argv[1]);<br>
try {<br> reader->Update();<br> }<br> catch(itk::ExceptionObject &err) {<br> std::cerr << "ExceptionObject caught!" << std::endl;<br> std::cerr << err << std::endl;
<br> return -1;<br> }<br> ImageType::ConstPointer sourceImage = reader->GetOutput();<br><br><br> //////////////////////////////////////////////////////////<br> /* create the writer */
<br> //<br> WriterType::Pointer writer = WriterType::New();<br> writer->SetFileName(argv[2]);<br><br><br> //////////////////////////////////////////////////////////<br> /* create the region in source to read from */
<br> //<br> ImageType::RegionType sourceInputRegion;<br> ImageType::RegionType::IndexType sourceRegionStartIndex;<br> ImageType::RegionType::SizeType sourceRegionStartSize;<br><br> sourceRegionStartIndex[0] = ::atoi(argv[3]);
<br> sourceRegionStartIndex[1] = ::atoi(argv[4]);<br><br> sourceRegionStartSize[0] = ::atoi(argv[5]);<br> sourceRegionStartSize[1] = ::atoi(argv[6]);<br><br> sourceInputRegion.SetSize(sourceRegionStartSize);<br>
sourceInputRegion.SetIndex(sourceRegionStartIndex);<br><br><br> //////////////////////////////////////////////////////////<br> /* create the region to output into the output image */<br> //<br> ImageType::RegionType outputRegion;
<br><br> outputRegion.SetSize(sourceRegionStartSize);<br> outputRegion.SetIndex(sourceRegionStartIndex);<br> <br><br> //////////////////////////////////////////////////////////<br> /* Allocate an output image and set valid values to
<br> / some of the basic image information during the<br> / copying process. The starting index of the output<br> / region is now filled with zero values and the<br> / cooridinates of the physical origin are computed
<br> / as a shift from the origin of the input image.<br> // */<br> ImageType::Pointer outputImage = ImageType::New();<br> const ImageType::SpacingType& spacing =
<br> reader->GetOutput()->GetSpacing();<br> const ImageType::PointType& inputOrigin =<br> reader->GetOutput()->GetOrigin();<br> double outputOrigin[Dimension];
<br><br> for(unsigned int i=0; i<Dimension; i++) {<br> outputOrigin[i] = inputOrigin[i]+spacing[i]*sourceRegionStartIndex[i];<br> }<br> outputImage->SetSpacing(spacing); // Same pixel spacing as input image
<br> outputImage->SetOrigin(outputOrigin); // Same origin as input image<br> outputImage->SetLargestPossibleRegion(sourceImage->GetLargestPossibleRegion());<br> outputImage->SetBufferedRegion(outputRegion);
<br> outputImage->SetRequestedRegion(outputRegion);<br> //outputImage->SetRegions(outputRegion);<br> outputImage->Allocate();<br><br> //////////////////////////////////////////////////////////<br> /* Image Region Constant Iterator */
<br> //<br> ConstIteratorType inputIt(reader->GetOutput(),sourceInputRegion);<br> IteratorType outputIt(outputImage,outputImage->GetBufferedRegion());<br><br> int i = 0;<br> PixelType averageTemp, average;
<br> for( inputIt.GoToBegin(); !inputIt.IsAtEnd() ;<br> ++inputIt) {<br> if(i!=0){<br> averageTemp = inputIt.Get();<br> average.SetRed(int((average.GetRed()+averageTemp.GetRed())/2.0));
<br> average.SetGreen(int((average.GetGreen()+averageTemp.GetGreen())/2.0));<br> average.SetBlue(int((average.GetBlue()+averageTemp.GetBlue())/2.0));<br> }<br> else {<br>
averageTemp = inputIt.Get();<br> average.SetRed(int(averageTemp.GetRed()));<br> average.SetGreen(int(averageTemp.GetGreen()));<br> average.SetBlue(int(averageTemp.GetBlue
()));<br> i++;<br> }<br> }<br><br><br> for( outputIt.GoToBegin() ; !outputIt.IsAtEnd() ;<br> ++outputIt) {<br> outputIt.Set(average);<br> }<br><br><br> //////////////////////////////////////////////////////////
<br> /* Setting attributes to writer and writing data */<br> //<br> writer->SetInput(outputImage);<br> try {<br> writer->Update();<br> }<br> catch(itk::ExceptionObject &err) {<br>
std::cerr << "ExceptionObject caught!" << std::endl;<br> std::cerr << err << std::endl;<br> return -1;<br> }<br><br> return 0;<br>}<br><br><br><div><span class="gmail_quote">
On 5/12/06, <b class="gmail_sendername">Miller, James V (GE, Research)</b> <<a href="mailto:millerjv@crd.ge.com">millerjv@crd.ge.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>
<div>
<div><span><font color="#0000ff" face="Arial" size="2">At
first glance, the only issue I see is your calculation of the origin. In
ITK, the origin corresponds to the physical coordinate (mm or ft, etc.) of the
pixel at index [0, 0, ...]. Since your output region starts at a non-zero
index, you should not need to move the origin. So just copy the origin and
spacing from the input image to the output image.</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">Can
you describe the "problem" in more detail? How does the output image
look?</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">There
could be a problem with the ImageFileWriter when the BufferedRegion does not
start at index [0,0,...]. You could place a RegionOfInterestImageFilter
before the ImageFileWriter (or an ChangeInformationImageFilter) to adjust the
image so that the BufferedRegion starts at [0,0,...]</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">Here
are the definitions of regions:</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">RequestedRegion: How much of the image to process.</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2">BufferedRegion: How much of the image is in memory.</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2">LargestPossibleRegion: How big could the image be.
</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">Filters are told to process a RequestedRegion. When a filter runs,
the RequestedRegion must be a subset of the BufferedRegion. The
LargestPossibleRegion is typically used to tell whether a pixel is a true
boundary condition or just a voxel at the edge of
BufferedRegion.</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">Jim</font></span></div></div><div><span class="e" id="q_10b29a75662f0b34_1">
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
</span></div></div></blockquote></div><br>