img_filters.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002  * VCGLib                                                            o o     *
00003  * Visual and Computer Graphics Library                            o     o   *
00004  *                                                                _   O  _   *
00005  * Copyright(C) 2009                                                \/)\/    *
00006  * Visual Computing Lab                                            /\/|      *
00007  * ISTI - Italian National Research Council                           |      *
00008  *                                                                    \      *
00009  * All rights reserved.                                                      *
00010  *                                                                           *
00011  * This program is free software; you can redistribute it and/or modify      *   
00012  * it under the terms of the GNU General Public License as published by      *
00013  * the Free Software Foundation; either version 2 of the License, or         *
00014  * (at your option) any later version.                                       *
00015  *                                                                           *
00016  * This program is distributed in the hope that it will be useful,           *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019  * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020  * for more details.                                                         *
00021  *                                                                           *
00022  ****************************************************************************/
00023 
00024 #include <QtCore>
00025 #include "wrap/qt/img_qt.h"
00026 
00027 void sample001_open_save_color(QString input_file, QString output_file)
00028 {
00029   img::Image<> image;
00030   img::openQtRGB(input_file, image);
00031   img::saveQtRGB(image, output_file);
00032 }
00033 
00034 void sample002_open_save_grayscale(QString input_file, QString output_file)
00035 {
00036   img::Image<1> image;
00037   img::openQtY(input_file, image);
00038   img::saveQtY(image, output_file);
00039 }
00040 
00041 void sample003_normalize(QString input_file, QString output_file)
00042 {
00043   img::Image<> image;
00044   img::openQtRGB(input_file, image);
00045   img::saveQtRGB(img::getNormalized(image), output_file);
00046 }
00047 
00048 
00049 void sample004_boxfilter(QString input_file, QString output_file)
00050 {
00051   img::Image<> image;
00052   img::openQtRGB(input_file, image);
00053   int radius=3;
00054   img::saveQtRGB(img::getBoxFiltered(image,radius), output_file);
00055 }
00056 
00057 void sample005_gaussiansmooth(QString input_file, QString output_file)
00058 {
00059   img::Image<> image;
00060   img::openQtRGB(input_file, image);
00061   int radius=4;
00062   img::saveQtRGB(img::getGaussianSmoothed(image,radius), output_file);
00063 }
00064 
00065 void sample006_medianfilter(QString input_file, QString output_file)
00066 {
00067   img::Image<> image;
00068   img::openQtRGB(input_file, image);
00069   int radius=5;
00070   img::saveQtRGB(img::getMedianFiltered(image,radius), output_file);
00071 }
00072 
00073 void sample007_unsharpmask(QString input_file, QString output_file)
00074 {
00075   img::Image<> image;
00076   img::openQtRGB(input_file, image);
00077 
00078   int radius=4;
00079   double factor=0.6;
00080 
00081   img::saveQtRGB(img::getUnsharpMasked(image,radius,factor), output_file);
00082 }
00083 
00084 
00085 void sample008_laplacianfilter(QString input_file, QString output_file)
00086 {
00087   img::Image<> image;
00088   img::openQtRGB(input_file, image);
00089   img::Image<> laplacianfiltered;
00090   img::LaplacianFilter(image,laplacianfiltered);
00091   img::saveQtRGB(img::getNormalized(laplacianfiltered), output_file);
00092 }
00093 
00094 void sample009_logfilter(QString input_file, QString output_file)
00095 {
00096   img::Image<> image;
00097   img::openQtRGB(input_file, image);
00098   int radius=5;
00099   img::Image<> logfiltered;
00100   img::LoGFilter(image,logfiltered,radius);
00101   img::saveQtRGB(img::getNormalized(logfiltered), output_file);
00102 }
00103 
00104 void sample010_dogfilter(QString input_file, QString output_file)
00105 {
00106   img::Image<> image;
00107   img::openQtRGB(input_file, image);
00108   // must be radius1 < radius2
00109   int radius1=2; 
00110   int radius2=4;
00111   img::Image<> dogfiltered;
00112   img::DoGFilter(image,dogfiltered,radius1,radius2);
00113 
00114   img::saveQtRGB(img::getNormalized(dogfiltered), output_file);
00115 }
00116 
00117 void sample011_general_convolutions(QString input_file, QString output_dir,QString output_suffix)
00118 {
00119   img::Image<> image;
00120   img::openQtRGB(input_file, image);
00121 
00122   QVector< QPair< double*, QPair< QPair< int, int > , QString> > > mm;
00123   double *f;
00124 
00125   f=new double[9];
00126   f[0]= 0.0f; f[1]= 0.0f; f[2]= 0.0f;
00127   f[3]=-1.0f; f[4]= 1.0f; f[5]= 0.0f;
00128   f[6]= 0.0f, f[7]= 0.0f; f[8]= 0.0f;
00129   mm.push_back(qMakePair(f,qMakePair(qMakePair(3,3),QString("edge_enhance"))));    
00130 
00131   f=new double[9];
00132   f[0]= 2.0f; f[1]= 0.0f; f[2]= 0.0f;
00133   f[3]= 0.0f; f[4]=-1.0f; f[5]= 0.0f;
00134   f[6]= 0.0f, f[7]= 0.0f; f[8]=-1.0f;
00135   mm.push_back(qMakePair(f,qMakePair(qMakePair(3,3),QString("embross"))));    
00136 
00137   f=new double[15];
00138   f[0] = 1.0f; f[1] = 2.0f; f[2] =  0.0f; f[3] = 2.0f; f[4] = 1.0f;
00139   f[5] = 1.0f; f[6] = 2.0f; f[7] =-18.0f; f[8] = 2.0f; f[9] = 1.0f;
00140   f[10]= 1.0f; f[11]= 2.0f; f[12]=  0.0f; f[13]= 2.0f; f[14]= 1.0f;
00141   mm.push_back(qMakePair(f,qMakePair(qMakePair(5,3),QString("my_vert_edges"))));
00142 
00143   f=new double[15];
00144   f[0] = 1.0f; f[1] =  1.0f; f[2] = 1.0f; 
00145   f[3] = 2.0f; f[4] =  2.0f; f[5] = 2.0f; 
00146   f[6] = 0.0f; f[7] =-18.0f; f[8] = 0.0f; 
00147   f[9] = 2.0f; f[10]=  2.0f; f[11]= 2.0f; 
00148   f[12]= 1.0f; f[13]=  1.0f; f[14]= 1.0f;
00149   mm.push_back(qMakePair(f,qMakePair(qMakePair(3,5),QString("my_horiz_edges"))));
00150 
00151   QPair< double*, QPair< QPair< int, int > , QString> > m;
00152 
00153   foreach(m,mm){
00154     double* matrix=m.first;
00155     int matrix_width=((m.second).first).first;
00156     int matrix_height=((m.second).first).second;
00157     QString matrix_name=(m.second).second;
00158 
00159     img::Image<> convolved;
00160     img::convolution(image,convolved,matrix,matrix_width,matrix_height);
00161 
00162     delete [] matrix; 
00163 
00164     bool normalize=(img::minValue(convolved)<0.0f)||(img::maxValue(convolved)>=255.0f);
00165     QString output_file(output_dir+"/011_general_convolution_"+matrix_name+
00166                         "_"+(normalize?" normalized_":"")+output_suffix);
00167 
00168     if(normalize)
00169       img::saveQtRGB(img::getNormalized(convolved),output_file);
00170     else
00171       img::saveQtRGB(convolved,output_file);
00172   }
00173 }
00174 
00175 void img_filters(QString input_dir,QString image,QString output_dir)
00176 {
00177   QString input_file(input_dir+"/"+image);
00178   sample001_open_save_color(input_file, output_dir+"/001-open_save_color_"+image);
00179 
00180   sample002_open_save_grayscale(input_file, output_dir+"/002-open_save_grayscale_"+image);
00181 
00182   sample003_normalize(input_file, output_dir+"/003_normalize_"+image);
00183 
00184   sample004_boxfilter(input_file, output_dir+"/004_boxfilter_"+image);
00185 
00186   sample005_gaussiansmooth(input_file, output_dir+"/005_gaussiansmooth_"+image);
00187 
00188   sample006_medianfilter(input_file, output_dir+"/006_medianfilter_"+image);
00189 
00190   sample007_unsharpmask(input_file, output_dir+"/007_unsharpmask_"+image);
00191 
00192   sample008_laplacianfilter(input_file, output_dir+"/008_laplacianfilter_normalized_"+image);
00193 
00194   sample009_logfilter(input_file, output_dir+"/009_logfilter_normalized_"+image);
00195 
00196   sample010_dogfilter(input_file, output_dir+"/010_dogfilter_normalized_"+image);
00197 
00198   sample011_general_convolutions(input_file, output_dir,image);
00199 }
00200 
00201 bool clean_dir(QDir dir); // utility, unrelated with the sample 
00202 
00203 int main(int argc,char ** argv)
00204 {
00205   if(argc<3) 
00206   {
00207     printf("Usage: img_filters <input_dir> <output_dir>\n");
00208     return 1;
00209   }
00210   printf("Executing img_filters over all images in \"%s\", ouput is in \"%s\"\n",  argv[1], argv[2]);
00211 
00212   QString input_dir(argv[1]);
00213   QString output_dir(argv[2]);
00214 
00215   QStringList readable_image_extensions = QStringList()
00216        << "*.bmp" << "*.gif" << "*.jpg" << "*.jpeg"
00217        << "*.png" << "*.pbm" << "*.pgm" << "*.ppm"
00218        << "*.tiff" << "*.xbm" << "*.xpm";
00219 
00220   QStringList image_list = QDir(input_dir).entryList(readable_image_extensions,QDir::Files|QDir::Readable,QDir::Name);
00221   assert(clean_dir(QDir(output_dir)));
00222 
00223   try {
00224     foreach(QString image, image_list)
00225       img_filters(input_dir,image,output_dir);  
00226   } catch (img::ImageException& e) {
00227     qDebug() << "caught ImageException, message:" << e.message;
00228   }
00229   return 0;
00230 }
00231 
00232 bool clean_dir(QDir dir){ // utility, unrelated with the sample
00233   if(!dir.exists()){
00234     qDebug() << QString("dir \"%1\" does not exists\n").arg(dir.path());
00235     return false;
00236   }
00237   foreach(QString e,dir.entryList(QDir::NoDotAndDotDot|QDir::Dirs|QDir::Files)){
00238     QFileInfo i(QString("%1/%2").arg(dir.path(),e));
00239     if(i.isDir()){
00240       if(!clean_dir(QDir(QString("%1/%2").arg(dir.path(),e)))){
00241         qDebug() << QString("cannot clean \"%1/%2\"\n").arg(dir.path(),e);
00242         return false;
00243       }
00244       if(!dir.rmdir(e)){
00245         qDebug() << QString("cannot remove \"%1/%2\"\n").arg(dir.path(),e);
00246         return false;
00247       }
00248     }else{
00249       if(!dir.remove(e)){
00250         qDebug() << QString("cannot remove \"%1/%2\"\n").arg(dir.path(),e);
00251        return false;
00252       }
00253     }
00254   }
00255   return true;
00256 }


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:31:54