[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

subimage_tutorial.cxx |

Create a half-sized subimage
Usage: `subimage_tutorial infile outfile`

#include <iostream>
#include <vigra/multi_array.hxx>
#include <vigra/stdimage.hxx>
#include <vigra/impex.hxx>
using namespace vigra;
int main(int argc, char ** argv)
{
if(argc != 3)
{
std::cout << "Usage: " << argv[0] << " infile outfile" << std::endl;
std::cout << "(supported formats: " << impexListFormats() << ")" << std::endl;
return 1;
}
try
{
// read image given as first argument
// file type is determined automatically
ImageImportInfo info(argv[1]);
if(info.isGrayscale())
{
// read image from file
MultiArray<2, UInt8> imageArray(info.shape());
importImage(info, imageArray);
// we want to cut out the center of the original image, such that the
// size of the remaining image is half the original
Shape2 upperLeft = info.shape() / 4,
lowerRight = info.shape() - upperLeft;
// create subimage around center for output
MultiArrayView<2, UInt8> subimage = imageArray.subarray(upperLeft, lowerRight);
// write the subimage to the file provided as second command line argument
// the file type will be determined from the file name's extension
exportImage(subimage, ImageExportInfo(argv[2]));
}
else
{
// read image from file
MultiArray<2, RGBValue<UInt8> > imageArray(Shape2(info.shape()));
importImage(info, imageArray);
// we want to cut out the center of the original image, such that the
// size of the remaining image is half the original
Shape2 upperLeft = info.shape() / 4,
lowerRight = info.shape() - upperLeft;
// create subimage around center for output
MultiArrayView<2, RGBValue<UInt8> > subimage = imageArray.subarray(upperLeft, lowerRight);
// write the subimage to the file provided as second command line argument
// the file type will be determined from the file name's extension
exportImage(subimage, ImageExportInfo(argv[2]));
}
}
catch (std::exception & e)
{
// catch any errors that might have occurred and print their reason
std::cout << e.what() << std::endl;
return 1;
}
return 0;
}
vigra::MultiArrayView< 2, UInt8 >
vigra::MultiArray
Main MultiArray class containing the memory management.
Definition: multi_array.hxx:2474
vigra
vigra::impexListFormats
std::string impexListFormats()
List the image formats VIGRA can read and write.
vigra::MultiArrayView::subarray
MultiArrayView subarray(difference_type p, difference_type q) const
Definition: multi_array.hxx:1528
vigra::importImage
void importImage(...)
Read an image from a file.
vigra::ImageImportInfo::isGrayscale
bool isGrayscale() const
impex.hxx
image import and export functions
vigra::ImageImportInfo::shape
MultiArrayShape< 2 >::type shape() const
vigra::ImageImportInfo
Argument object for the function importImage().
Definition: imageinfo.hxx:390
vigra::ImageExportInfo
Argument object for the function exportImage().
Definition: imageinfo.hxx:133
vigra::exportImage
void exportImage(...)
Write an image to a file.

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.11.1