#include "itkPointSet.h"
#include "itkVariableLengthVector.h"
#include "itkRGBPixel.h"
#include "itkImageRegionIterator.h"
#include <iostream>
#include <fstream>
int main(
int argc,
char* argv[])
{
if (argc != 8)
{
std::cerr << "Usage: " << argv[0];
std::cerr << " InputImage OutputImage OutputSIFTFile octaves scales threshold ratio" << std::endl;
return 1;
}
const char* infname = argv[1];
const char* outfname = argv[3];
const char* outputImageFilename = argv[2];
const unsigned int octaves = atoi(argv[4]);
const unsigned int scales = atoi(argv[5]);
float threshold = atof(argv[6]);
float ratio = atof(argv[7]);
using RealType = float;
const unsigned int Dimension = 2;
using RealVectorType = itk::VariableLengthVector<RealType>;
using PointSetType = itk::PointSet<RealVectorType, Dimension>;
using PointsContainerType = PointSetType::PointsContainer;
using PointsIteratorType = PointsContainerType::Iterator;
ReaderType::Pointer reader = ReaderType::New();
ImageToSIFTKeyPointSetFilterType::Pointer filter = ImageToSIFTKeyPointSetFilterType::New();
reader->SetFileName(infname);
filter->SetInput(reader->GetOutput());
filter->SetOctavesNumber(octaves);
filter->SetScalesNumber(scales);
filter->SetDoGThreshold(threshold);
filter->SetEdgeThreshold(ratio);
filter->Update();
ImageType::OffsetType t = {{0, 1}};
ImageType::OffsetType b = {{0, -1}};
ImageType::OffsetType r = {{1, 0}};
ImageType::OffsetType l = {{-1, 0}};
using RGBPixelType = itk::RGBPixel<unsigned char>;
OutputImageType::Pointer outputImage = OutputImageType::New();
OutputImageType::RegionType region;
OutputImageType::SizeType outputSize;
outputSize = reader->GetOutput()->GetLargestPossibleRegion().GetSize();
region.SetSize(outputSize);
OutputImageType::IndexType indexStart;
indexStart[0] = 0;
indexStart[1] = 0;
region.SetIndex(indexStart);
outputImage->SetRegions(region);
outputImage->Allocate();
itk::ImageRegionIterator<OutputImageType> iterOutput(outputImage, reader->GetOutput()->GetLargestPossibleRegion());
for (iterOutput.GoToBegin(); !iterOutput.IsAtEnd(); ++iterOutput)
{
ImageType::IndexType index = iterOutput.GetIndex();
ImageType::PixelType grayPix = reader->GetOutput()->GetPixel(index);
OutputImageType::PixelType rgbPixel;
rgbPixel.SetRed(static_cast<unsigned char>(grayPix));
rgbPixel.SetGreen(static_cast<unsigned char>(grayPix));
rgbPixel.SetBlue(static_cast<unsigned char>(grayPix));
iterOutput.Set(rgbPixel);
}
PointsIteratorType pIt = filter->GetOutput()->GetPoints()->Begin();
ImageType::SpacingType spacing = reader->GetOutput()->GetSignedSpacing();
ImageType::PointType origin = reader->GetOutput()->GetOrigin();
OutputImageType::SizeType size = outputImage->GetLargestPossibleRegion().GetSize();
while (pIt != filter->GetOutput()->GetPoints()->End())
{
ImageType::IndexType index;
index[0] = (unsigned int)(std::floor((double)((pIt.Value()[0] - origin[0]) / spacing[0] + 0.5)));
index[1] = (unsigned int)(std::floor((double)((pIt.Value()[1] - origin[1]) / spacing[1] + 0.5)));
OutputImageType::PixelType keyPixel;
keyPixel.SetRed(0);
keyPixel.SetGreen(255);
keyPixel.SetBlue(0);
if (static_cast<unsigned int>(index[1]) < static_cast<unsigned int>(size[1]) && static_cast<unsigned int>(index[0]) < static_cast<unsigned int>(size[0]))
{
outputImage->SetPixel(index, keyPixel);
if (static_cast<unsigned int>(index[1]) < static_cast<unsigned int>(size[1] - 1))
outputImage->SetPixel(index + t, keyPixel);
if (index[1] > 0)
outputImage->SetPixel(index + b, keyPixel);
if (static_cast<unsigned int>(index[0]) < static_cast<unsigned int>(size[0] - 1))
outputImage->SetPixel(index + r, keyPixel);
if (index[0] > 0)
outputImage->SetPixel(index + l, keyPixel);
}
++pIt;
}
std::ofstream outfile(outfname);
outfile << filter;
outfile.close();
WriterType::Pointer writer = WriterType::New();
writer->SetInput(outputImage);
writer->SetFileName(outputImageFilename);
writer->Update();
return EXIT_SUCCESS;
}