Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleUpsampleBy2Grey.cpp

Example for upsampling images by factor of 2 on grey images , ImageConvert

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleUpsampleBy2Grey.cpp
@brief Example for upsampling images by factor of 2 on grey images
@relates Rescale, ImageConvert
@ingroup g_examples
@author MIP
*/
#include <Base/Image/Image.hh>
#include <Base/Image/ImageIO.hh>
#include <Base/Image/ImageConvert.hh>
#include <Filter/Rescale.hh>
#include <Base/Debug/TimeMeasure.hh>
#include <Base/Common/BIASpragma.hh>
using namespace BIAS;
using namespace std;
int main(int argc, char *argv[])
{
cout<<"This example compares different upsampling methods for factor 2"
<<endl;
if (argc<2){
BIASERR(argv[0]<<" <image-file>");
return -1;
}
if (ImageIO::Load(argv[1], im)!=0){
BIASERR("error loading "<<argv[1]);
return -2;
}
Image<unsigned char> imbicubic(im.GetWidth()*2,im.GetHeight()*2,1);
Image<unsigned char> imbilinear(im.GetWidth()*2,im.GetHeight()*2,1);
Image<unsigned char> imnn(im.GetWidth()*2,im.GetHeight()*2,1);
// automatic
Upsampler.UpsampleBy2Grey(im, imnn, NearestNeighbour);
Upsampler.UpsampleBy2Grey(im, imbilinear, Bilinear);
Upsampler.UpsampleBy2Grey(im, imbicubic, Bicubic);
//if (ImageIO::Save("image_bicubic_auto.mip", imbicubic)!=0){
if (ImageIO::Save("image_bicubic_auto.mip", imbicubic)!=0){
BIASERR("error writing cubic image");
return -3;
}
//if (ImageIO::Save("image_bilinear_auto.mip", imbilinear)!=0){
if (ImageIO::Save("image_bilinear_auto.mip", imbilinear)!=0){
BIASERR("error writing bilinear image");
return -3;
}
//if (ImageIO::Save("image_nearestneighbor_auto.mip", imnn)!=0){
if (ImageIO::Save("image_nearestneighbor_auto.mip", imnn)!=0){
BIASERR("error writing bilinear image");
return -3;
}
imbicubic.SetZero();
imbilinear.SetZero();
// manual
unsigned char **pDc = imbicubic.GetImageDataArray();
unsigned char **pDb = imbilinear.GetImageDataArray();
double CurValue = 0;
const unsigned int channelcount = im.GetChannelCount();
for (unsigned int x=2; x<imbicubic.GetWidth()-6; x++) {
for (unsigned int y=2; y<imbicubic.GetHeight()-6; y++) {
for (unsigned int c=0; c<channelcount; c++) {
CurValue = rint(im.BicubicInterpolation(double(x)*0.5,
double(y)*0.5, c));
if (CurValue<0.0)CurValue = 0.0;
if (CurValue>255.0)CurValue = 255.0;
pDc[y][(x*channelcount)+c] = (unsigned char) CurValue;
CurValue = rint(im.BilinearInterpolation(double(x)*0.5,
double(y)*0.5,
c));
if (CurValue<0.0)CurValue = 0.0;
if (CurValue>255.0)CurValue = 255.0;
pDb[y][(x*channelcount)+c] = (unsigned char) CurValue;
}
}
}
//if (ImageIO::Save("image_bicubic.mip", imbicubic)!=0){
if (ImageIO::Save("image_bicubic.mip", imbicubic)!=0){
BIASERR("error writing cubic image");
return -3;
}
//if (ImageIO::Save("image_bilinear.mip", imbilinear)!=0){
if (ImageIO::Save("image_bilinear.mip", imbilinear)!=0){
BIASERR("error writing bilinear image");
return -3;
}
cout<<"Performing time measurements"<<endl;
TimeMeasure b1,b2,b3;
// warming up
Upsampler.UpsampleBy2Grey(im, imbilinear, Bilinear);
Upsampler.UpsampleBy2Grey(im, imbicubic, Bicubic);
Upsampler.UpsampleBy2Grey(im, imnn, NearestNeighbour);
unsigned int numberruns = 10;
for (unsigned int i=0; i<numberruns; i++) {
b1.Start();
Upsampler.UpsampleBy2Grey(im, imbilinear, Bilinear);
b1.Stop();
b2.Start();
Upsampler.UpsampleBy2Grey(im, imbicubic, Bicubic);
b2.Stop();
b3.Start();
Upsampler.UpsampleBy2Grey(im, imnn, NearestNeighbour);
b3.Stop();
}
cout<<"time nearest neighbor (real/user):"
<<b3.GetRealTime()/double(numberruns)
<<" "<<b3.GetUserTime()/double(numberruns)<<endl;
cout<<"time bilinear (real/user):"<<b1.GetRealTime()/double(numberruns)
<<" "<<b1.GetUserTime()/double(numberruns)<<endl;
cout<<"time bicubic (real/user):"<<b2.GetRealTime()/double(numberruns)
<<" "<<b2.GetUserTime()/double(numberruns)<<endl;
return 0;
}