|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + *=========================================================================*/ |
| 18 | + |
| 19 | +#include <iostream> |
| 20 | +#include <vector> |
| 21 | +#include <string> |
| 22 | +#include <cstdlib> |
| 23 | +#include <itkImageFileReader.h> |
| 24 | +#include <itkImageFileWriter.h> |
| 25 | +#include "itkMultipleImageIterator.h" |
| 26 | + |
| 27 | +// Dumps random samples from files into a csv file |
| 28 | + |
| 29 | +int |
| 30 | +DumpIntensities(int argc, char * argv[]) |
| 31 | +{ |
| 32 | + if (argc < 3) |
| 33 | + { |
| 34 | + std::cerr << "Usage: DumpIntensities outfile inImage [inImage ...]" << std::endl; |
| 35 | + return 1; |
| 36 | + } |
| 37 | + using PixelType = unsigned short; |
| 38 | + using ImageType = itk::Image<PixelType, 3>; |
| 39 | + using ReaderType = itk::ImageFileReader<ImageType>; |
| 40 | + |
| 41 | + using IteratorType = itk::ImageRegionIterator<ImageType>; |
| 42 | + itk::MultipleImageIterator<IteratorType> it; |
| 43 | + |
| 44 | + std::vector<ImageType::Pointer> images; // Need to keep a reference as iterators only have weak references |
| 45 | + for (int i = 2; i < argc; ++i) |
| 46 | + { |
| 47 | + ReaderType::Pointer r = ReaderType::New(); |
| 48 | + r->SetFileName(argv[i]); |
| 49 | + r->Update(); |
| 50 | + ImageType::Pointer im = r->GetOutput(); |
| 51 | + im->DisconnectPipeline(); |
| 52 | + images.push_back(im); |
| 53 | + it.AddIterator(itk::ImageRegionIterator<ImageType>(im, im->GetLargestPossibleRegion())); |
| 54 | + } |
| 55 | + |
| 56 | + unsigned long long c = 0; |
| 57 | + using Vec3 = itk::FixedArray<PixelType, 3>; |
| 58 | + std::vector<Vec3> values; |
| 59 | + for (it.GoToBegin(); !it.IsAtEnd(); ++it, ++c) |
| 60 | + { |
| 61 | + if (c % 42 == 0) |
| 62 | + { |
| 63 | + Vec3 v; |
| 64 | + for (unsigned int i = 0; i < it.Size(); ++i) |
| 65 | + { |
| 66 | + v[i] = it[i].Get(); |
| 67 | + } |
| 68 | + values.push_back(v); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + using Image1D = itk::Image<Vec3, 1>; |
| 73 | + Image1D::RegionType region; |
| 74 | + region.SetIndex(0, 0); |
| 75 | + region.SetSize(0, values.size()); |
| 76 | + Image1D::Pointer randImage = Image1D::New(); |
| 77 | + randImage->SetRegions(region); |
| 78 | + randImage->Allocate(); |
| 79 | + |
| 80 | + int index = 0; |
| 81 | + itk::ImageRegionIterator<Image1D> oIt(randImage, randImage->GetLargestPossibleRegion()); |
| 82 | + while (!oIt.IsAtEnd()) |
| 83 | + { |
| 84 | + oIt.Set(values[index++]); |
| 85 | + ++oIt; |
| 86 | + } |
| 87 | + |
| 88 | + using WriterType = itk::ImageFileWriter<Image1D>; |
| 89 | + WriterType::Pointer writer = WriterType::New(); |
| 90 | + writer->SetInput(randImage); |
| 91 | + writer->SetFileName(argv[1]); |
| 92 | + writer->Update(); |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
0 commit comments