Search code examples
c++image-processingitk

how to read and write an image using C++ in visual studio with ITK configured


I am a beginner to ITK and c++. I have the following code where I can get the height and width of an image. Instead of giving the input image in the console, I want to do it in the code itself. How do I directly give the input image to this code?

#include "itkImage.h"
#include "itkImageFileReader.h"

int main()
{
mat m("filename");
imshow("windowname", m);
}
// verify command line arguments
if( argc < 2 )
{
std::cout << "usage: " << std::endl;
std::cerr << argv[0] << " inputimagefile" << std::endl;
return exit_failure;
}

 typedef itk::image<float, 2>            imagetype;
 typedef itk::imagefilereader<imagetype> readertype;

 readertype::pointer reader = readertype::new();
 reader->setfilename( argv[1] );
 reader->update();

 std::cout << reader->getoutput()->getlargestpossibleregion().getsize()[0] << " "
 << reader->getoutput()->getlargestpossibleregion().getsize()[1] << std::endl;

// an example image had w = 200 and h = 100 (it is wider than it is tall). the above output
// 200 100
// so w = getsize()[0]
// and h = getsize()[1]

// a pixel inside the region
itk::index<2> indexinside;
indexinside[0] = 150;
indexinside[1] = 50;
std::cout << reader->getoutput()- 
>getlargestpossibleregion().isinside(indexinside) << std::endl;

// a pixel outside the region
itk::index<2> indexoutside;
indexoutside[0] = 50;
indexoutside[1] = 150;
std::cout << reader->getoutput()-   >getlargestpossibleregion().isinside(indexoutside) << std::endl;

// this means that the [0] component of the index is referencing the left to right (column) index
// and the [1] component of index is referencing the top to bottom (row) index

return exit_success;
}

Solution

  • Change the line reader->setfilename( argv[1] ); by reader->setfilename( "C:/path/to/file.png" );

    I assume that

    mat m("filename");
    imshow("windowname", m);
    

    sneaked in from some unrelated code? Otherwise the example would not compile.