SHOW:
|
|
- or go back to the newest paste.
| 1 | #include "itkImage.h" | |
| 2 | #include "itkImageFileWriter.h" | |
| 3 | #include "itkImageFileReader.h" | |
| 4 | #include "itkMinimumMaximumImageCalculator.h" | |
| 5 | #include <iostream> | |
| 6 | #include <string> | |
| 7 | #include <stdlib.h> | |
| 8 | #include <fstream> | |
| 9 | ||
| 10 | using namespace std; | |
| 11 | ||
| 12 | const unsigned int ImageDimension = 4; | |
| 13 | const unsigned int RDImageDimension = ImageDimension-1; | |
| 14 | typedef double PixelType; | |
| 15 | typedef itk::Image< PixelType, ImageDimension > ImageType; | |
| 16 | typedef itk::Image< PixelType, RDImageDimension> RDImageType; | |
| 17 | typedef itk::ImageFileReader< ImageType > ReaderType; | |
| 18 | typedef itk::ImageFileWriter< ImageType > WriterType; | |
| 19 | ofstream file; | |
| 20 | ||
| 21 | void writetofile(PixelType min, PixelType max) | |
| 22 | {
| |
| 23 | file << min << " " << max << std::endl; | |
| 24 | } | |
| 25 | ||
| 26 | ImageType::Pointer randomscaler(ImageType::Pointer inputimage, ImageType::RegionType iterregion) | |
| 27 | {
| |
| 28 | ||
| 29 | typedef itk::MinimumMaximumImageCalculator <ImageType> ImageCalculatorFilterType; | |
| 30 | ImageCalculatorFilterType::Pointer imageCalculatorFilter = ImageCalculatorFilterType::New (); | |
| 31 | ||
| 32 | double pi = 4.0*atan(1.0); | |
| 33 | double U = rand()/float(RAND_MAX); double V = rand()/float(RAND_MAX); | |
| 34 | double X = sqrt(-2*log(U))*cos(2*pi*V); | |
| 35 | double Y = sqrt(-2*log(U))*sin(2*pi*V); | |
| 36 | double randomnumber = (Y*Y + 0.1)*(X/abs(X)); | |
| 37 | ||
| 38 | imageCalculatorFilter->SetImage( inputimage ); | |
| 39 | imageCalculatorFilter->Compute(); | |
| 40 | ||
| 41 | std::cout << 25*randomnumber << std::endl; | |
| 42 | ||
| 43 | PixelType Omin = 25*randomnumber; | |
| 44 | PixelType Omax = 255-Omin; | |
| 45 | PixelType Imin = imageCalculatorFilter->GetMinimum(); | |
| 46 | PixelType Imax = imageCalculatorFilter->GetMaximum(); | |
| 47 | writetofile(Imin, Imax); | |
| 48 | ||
| 49 | itk::ImageRegionIterator<ImageType> imageIterator(inputimage,iterregion); | |
| 50 | PixelType outputPixel; | |
| 51 | PixelType inputPixel; | |
| 52 | ||
| 53 | imageIterator.GoToBegin(); | |
| 54 | ||
| 55 | - | std::cout << "image iterator at end?" << imageIterator.IsAtEnd() << std::endl; \\gives 1 always |
| 55 | + | std::cout << "image iterator at end?" << imageIterator.IsAtEnd() << std::endl; //gives 1 always |
| 56 | ||
| 57 | while(!imageIterator.IsAtEnd()) | |
| 58 | {
| |
| 59 | inputPixel = imageIterator.Get(); | |
| 60 | outputPixel = (inputPixel-Imin)*((Omax-Omin)/(Imax-Imin))+Omin; | |
| 61 | imageIterator.Set(outputPixel); | |
| 62 | ++imageIterator; | |
| 63 | } | |
| 64 | ||
| 65 | ImageType::Pointer outputimage = inputimage; | |
| 66 | ||
| 67 | return outputimage; | |
| 68 | } | |
| 69 | ||
| 70 | int main(int argc , char *argv[]) | |
| 71 | {
| |
| 72 | //srand(0); | |
| 73 | file.open("minmax.txt");
| |
| 74 | ||
| 75 | if( argc < 3 ) | |
| 76 | {
| |
| 77 | std::cerr << "Usage: " << std::endl; | |
| 78 | std::cerr << argv[1] << " inputImageFile" << std::endl; | |
| 79 | std::cerr << argv[2] << " outputImageFile" << std::endl; | |
| 80 | return EXIT_FAILURE; | |
| 81 | } | |
| 82 | ||
| 83 | ReaderType::Pointer reader = ReaderType::New(); | |
| 84 | ||
| 85 | reader->SetFileName(argv[1]); | |
| 86 | reader->Update(); | |
| 87 | ||
| 88 | ImageType::Pointer image = reader->GetOutput(); | |
| 89 | ImageType::RegionType region = image->GetLargestPossibleRegion(); | |
| 90 | ImageType::SizeType size = region.GetSize(); | |
| 91 | ||
| 92 | ImageType::IndexType volumestart; | |
| 93 | volumestart = image->GetLargestPossibleRegion().GetIndex(); | |
| 94 | int nrofloops = size[ImageDimension-1]; | |
| 95 | size[ImageDimension-1] = 0; | |
| 96 | region.SetSize(size); | |
| 97 | ||
| 98 | ImageType::Pointer scaledImage; | |
| 99 | vnl_vector< PixelType > Imaxvector; | |
| 100 | ||
| 101 | scaledImage = image; | |
| 102 | std::cout << "total nr of loops: " << nrofloops << std::endl; | |
| 103 | for(int i=0; i < nrofloops; i++) | |
| 104 | {
| |
| 105 | volumestart[ImageDimension-1] = i; | |
| 106 | region.SetIndex(volumestart); | |
| 107 | scaledImage = randomscaler(scaledImage, region); | |
| 108 | } | |
| 109 | ||
| 110 | WriterType::Pointer writer = WriterType::New(); | |
| 111 | ||
| 112 | writer->SetFileName( argv[2] ); | |
| 113 | ||
| 114 | try | |
| 115 | {
| |
| 116 | writer->SetInput( scaledImage ); | |
| 117 | writer->Update(); | |
| 118 | } | |
| 119 | catch( itk::ExceptionObject & excp ) | |
| 120 | {
| |
| 121 | std::cerr << excp << std::endl; | |
| 122 | return EXIT_FAILURE; | |
| 123 | } | |
| 124 | ||
| 125 | file.close(); | |
| 126 | return 0; | |
| 127 | } |