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

smooth_convolve.cxx VIGRA

Convolve an image in different ways (gray scale or color)
Usage: smooth_convolve infile outfile

#include <vigra/multi_array.hxx>
#include <vigra/impex.hxx>
#include <vigra/convolution.hxx>
#include <iostream>
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
ImageImportInfo info(argv[1]);
// choose convolving mode
std::cout << "Which mode of convolution?\n";
std::cout << "1 - disk average\n";
std::cout << "2 - corners of a 3x3-box\n";
std::cout << "3 - Gaussian filter\n";
std::cout << "4 - separable Gaussian filter (x- and x-dimension separately)\n";
int mode;
std::cin >> mode;
// instantiate convolution kernels
Kernel2D<double> kernel2dim;
Kernel1D<double> kernel1dim;
// initialize kernel
switch(mode)
{
case 1:
// homogenous averaging within a disk of radius 2
kernel2dim.initDisk(2);
break;
case 2:
// strange custom-made filter: averaging the corners of a 3x3-box
kernel2dim.initExplicitly(Diff2D(-1,-1), Diff2D(1,1)) =
0.25, 0.0, 0.25,
0.0, 0.0, 0.0,
0.25, 0.0, 0.25;
break;
case 3:
// gaussian convolution
kernel2dim.initGaussian(1.5);
break;
case 4:
// separable gaussian convolution
kernel1dim.initGaussian(1.5);
break;
default:
vigra_precondition(false, "mode must be between 1 and 4.");
}
// process grayscale image
if (info.isGrayscale())
{
// instantiate arrays for image data and for smoothed image of appropriate size
MultiArray<2, float> imageArray(info.shape()),
exportArray(info.shape());
// copy image data into array
importImage(info, imageArray);
// convolve image
if (mode == 4)
{
convolveImage(imageArray, exportArray, kernel1dim, kernel1dim);
}
else
{
convolveImage(imageArray, exportArray, kernel2dim);
}
// write image data to the file given as second argument
exportImage(exportArray, ImageExportInfo(argv[2]));
}
// process color image
else
{
// instantiate arrays for image data and for smoothed image of appropriate size
MultiArray<2, RGBValue<float> > imageArray(info.shape());
MultiArray<2, RGBValue<float> > exportArray(info.shape());
// copy image data into array
importImage(info, imageArray);
// convolve image
if (mode == 4)
{
convolveImage(imageArray, exportArray, kernel1dim, kernel1dim);
}
else
{
convolveImage(imageArray, exportArray, kernel2dim);
}
// write image data to the file given as second argument
exportImage(exportArray, 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::Kernel2D::initExplicitly
Kernel2D & initExplicitly(Shape2 const &upperleft, Shape2 const &lowerright)
Definition: stdconvolution.hxx:1130
vigra::Kernel1D::initGaussian
void initGaussian(double std_dev, value_type norm, double windowRatio=0.0)
Definition: separableconvolution.hxx:2253
vigra::Diff2D
Two dimensional difference vector.
Definition: diff2d.hxx:185
vigra::MultiArray
Main MultiArray class containing the memory management.
Definition: multi_array.hxx:2474
vigra
vigra::Kernel2D
Generic 2 dimensional convolution kernel.
Definition: stdconvolution.hxx:52
vigra::impexListFormats
std::string impexListFormats()
List the image formats VIGRA can read and write.
vigra::importImage
void importImage(...)
Read an image from a file.
vigra::ImageImportInfo::isGrayscale
bool isGrayscale() const
vigra::Kernel1D
Generic 1 dimensional convolution kernel.
Definition: separableconvolution.hxx:52
vigra::convolveImage
void convolveImage(...)
Convolve an image with the given kernel(s).
impex.hxx
image import and export functions
vigra::Kernel2D::initDisk
void initDisk(int radius)
Definition: stdconvolution.hxx:1050
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::Kernel2D::initGaussian
void initGaussian(double std_dev, value_type norm)
Definition: stdconvolution.hxx:1016
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