Austin Schuh | 8c794d5 | 2019-03-03 21:17:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | # |
| 3 | # File : wavelet_atrous.cpp |
| 4 | # ( C++ source file ) |
| 5 | # |
| 6 | # Description : Performs a 2D or 3D 'a trous' wavelet transform |
| 7 | # (using a cubic spline) on an image or a video sequence. |
| 8 | # This file is a part of the CImg Library project. |
| 9 | # ( http://cimg.eu ) |
| 10 | # |
| 11 | # Author : Renaud Peteri |
| 12 | # ( Renaud.Peteri(at)mines-paris.org ) |
| 13 | # Andrea Onofri |
| 14 | # ( Andrea.Onofri(at)teletu.it ) |
| 15 | # |
| 16 | # Institution : CWI, Amsterdam |
| 17 | # |
| 18 | # Date : February 2005 |
| 19 | # |
| 20 | # References : Starck, J.-L., Murtagh, F. and Bijaoui, A., |
| 21 | # Image Processing and Data Analysis: The Multiscale Approach, |
| 22 | # Cambridge University Press, 1998. |
| 23 | # (Hardback and softback, ISBN 0-521-59084-1 and 0-521-59914-8.) |
| 24 | # |
| 25 | # License : CeCILL v2.0 |
| 26 | # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) |
| 27 | # |
| 28 | # This software is governed by the CeCILL license under French law and |
| 29 | # abiding by the rules of distribution of free software. You can use, |
| 30 | # modify and/ or redistribute the software under the terms of the CeCILL |
| 31 | # license as circulated by CEA, CNRS and INRIA at the following URL |
| 32 | # "http://www.cecill.info". |
| 33 | # |
| 34 | # As a counterpart to the access to the source code and rights to copy, |
| 35 | # modify and redistribute granted by the license, users are provided only |
| 36 | # with a limited warranty and the software's author, the holder of the |
| 37 | # economic rights, and the successive licensors have only limited |
| 38 | # liability. |
| 39 | # |
| 40 | # In this respect, the user's attention is drawn to the risks associated |
| 41 | # with loading, using, modifying and/or developing or reproducing the |
| 42 | # software by the user in light of its specific status of free software, |
| 43 | # that may mean that it is complicated to manipulate, and that also |
| 44 | # therefore means that it is reserved for developers and experienced |
| 45 | # professionals having in-depth computer knowledge. Users are therefore |
| 46 | # encouraged to load and test the software's suitability as regards their |
| 47 | # requirements in conditions enabling the security of their systems and/or |
| 48 | # data to be ensured and, more generally, to use and operate it in the |
| 49 | # same conditions as regards security. |
| 50 | # |
| 51 | # The fact that you are presently reading this means that you have had |
| 52 | # knowledge of the CeCILL license and that you accept its terms. |
| 53 | # |
| 54 | */ |
| 55 | |
| 56 | #include "CImg.h" |
| 57 | using namespace cimg_library; |
| 58 | #ifndef cimg_imagepath |
| 59 | #define cimg_imagepath "img/" |
| 60 | #endif |
| 61 | |
| 62 | // Define convolution mask. |
| 63 | CImg<float> mask(const unsigned char dirIdx, const unsigned char scale) { |
| 64 | const int |
| 65 | d1 = 1 << (scale-1), |
| 66 | d2 = 1 << scale, |
| 67 | c = d2, |
| 68 | vecLen = (1 << (scale + 1)) + 1; |
| 69 | |
| 70 | const float |
| 71 | valC = 0.375f, // 6/16 |
| 72 | valD1 = 0.25f, // 4/16 |
| 73 | valD2 = 0.0625f; // 1/16 |
| 74 | |
| 75 | switch(dirIdx) { |
| 76 | case 0 : { // x |
| 77 | CImg<float> m(vecLen,1,1,1,0); |
| 78 | m(c) = valC; |
| 79 | m(c - d1) = m(c + d1) = valD1; |
| 80 | m(c - d2) = m(c + d2) = valD2; |
| 81 | return m; |
| 82 | } |
| 83 | case 1: { // y |
| 84 | CImg<float> m(1,vecLen,1,1,0); |
| 85 | m(0,c) = valC; |
| 86 | m(0,c - d1) = m(0,c + d1) = valD1; |
| 87 | m(0,c - d2) = m(0,c + d2) = valD2; |
| 88 | return m; |
| 89 | } |
| 90 | case 2: { // t |
| 91 | CImg<float> m(1,1,vecLen,1,0); |
| 92 | m(0,0,c) = valC; |
| 93 | m(0,0,c - d1) = m(0,0,c + d1) = valD1; |
| 94 | m(0,0,c - d2) = m(0,0,c + d2) = valD2; |
| 95 | return m; |
| 96 | } |
| 97 | default: throw CImgException("Error, unknow decompostion axe, dirIdx = '%c'.",dirIdx); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /*------------------ |
| 102 | Main procedure |
| 103 | ----------------*/ |
| 104 | int main(int argc,char **argv) { |
| 105 | cimg_usage("Perform an 'a trous' wavelet transform (using a cubic spline) on an image or on a video sequence.\n" |
| 106 | "This wavelet transform is undecimated and produces 2 images/videos at each scale. For an example of\n" |
| 107 | "decomposition on a video, try -i img/trees.inr (sequence from the MIT).\n" |
| 108 | "\t(Type -h for help)"); |
| 109 | |
| 110 | // Read command line parameters |
| 111 | const char |
| 112 | *name_i = cimg_option("-i",cimg_imagepath "parrot.ppm","Input image or video"), |
| 113 | *name_o = cimg_option("-o","","Name of the multiscale analysis output"), |
| 114 | *axe_dec = cimg_option("-axe",(char*)0, |
| 115 | "Perform the multiscale decomposition in just one direction ('x', 'y' or 't')"); |
| 116 | const unsigned int |
| 117 | s = cimg_option("-s",3,"Scale of decomposition"); |
| 118 | |
| 119 | const bool help = cimg_option("-h",false,"Display Help"); |
| 120 | if (help) std::exit(0); |
| 121 | |
| 122 | // Initialize Image Data |
| 123 | std::fprintf(stderr," - Load image sequence '%s'...\n",cimg::basename(name_i)); |
| 124 | const CImg<float> texture_in(name_i); |
| 125 | CImg<float> mask_conv; |
| 126 | CImgList<float> res(s,texture_in.width(),texture_in.height(),texture_in.depth()); |
| 127 | CImgList<float> wav(s,texture_in.width(),texture_in.height(),texture_in.depth()); |
| 128 | cimglist_for(res,l) { res(l).fill(0.0); wav(l).fill(0.0); } |
| 129 | unsigned int i; |
| 130 | |
| 131 | int firstDirIdx = 0,lastDirIdx = 2; |
| 132 | if (axe_dec) { // The multiscale decomposition will be performed in just one direction |
| 133 | char c = cimg::lowercase(axe_dec[0]); |
| 134 | switch(c) { |
| 135 | case 'x': firstDirIdx = 0; break; |
| 136 | case 'y': firstDirIdx = 1; break; |
| 137 | case 't': firstDirIdx = 2; break; |
| 138 | default: throw CImgException("Error, unknow decompostion axe '%c', try 'x', 'y' or 't'",c); |
| 139 | } |
| 140 | lastDirIdx = firstDirIdx; // Only one direction |
| 141 | } |
| 142 | |
| 143 | for (i = 0; i<s; i++) { |
| 144 | std::fprintf(stderr," - Performing scale %u ...\n",i + 1); |
| 145 | if (i==0) { res(i) = texture_in;} else { res(i) = res(i - 1); } |
| 146 | for (int di = firstDirIdx; di<=lastDirIdx; di++) { |
| 147 | mask_conv = mask((unsigned char)di,(unsigned char)(i + 1)); |
| 148 | res(i) = res(i).get_convolve(mask_conv); |
| 149 | } |
| 150 | if (i==0) { wav(i) = texture_in - res(i); } // res(0) and wav(0) are the 1st scale of decompostion |
| 151 | else { wav(i) = res(i - 1) - res(i); } |
| 152 | } |
| 153 | |
| 154 | if (*name_o) { |
| 155 | // Save the Multi-Scale Analysis. |
| 156 | std::fprintf(stderr," - Saving of all output sequences : %s in the msa/ directory... \n",cimg::basename(name_o)); |
| 157 | int count = 1; // res0 = original image |
| 158 | char filename[256] = "", filename_wav[256] = ""; |
| 159 | char STmp[16] = ""; |
| 160 | const int err = std::system("mkdir msa"); |
| 161 | if (!err) for (i = 0; i<s; i++) { |
| 162 | std::strcpy( filename, "msa/res" ); |
| 163 | std::strcpy( filename_wav, "msa/wav" ); |
| 164 | if (count<10) { std::strcat( filename, "0" ); std::strcat( filename_wav, "0" ); } |
| 165 | std::sprintf(STmp,"%d_",count); |
| 166 | std::strcat(filename,STmp); std::strcat(filename_wav,STmp); |
| 167 | std::strcat(filename,name_o); std::strcat(filename_wav,name_o); |
| 168 | res(i).save(filename); |
| 169 | wav(i).save(filename_wav); |
| 170 | count++; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Result visualization. |
| 175 | const float col[] = { 255, 255, 255 }; |
| 176 | for (i = 0; i<s; i++) { |
| 177 | res[i].normalize(0,255).draw_text(2,2,"Scale %d",col,0,1,13,i); |
| 178 | wav[i].normalize(0,255).draw_text(2,2,"Scale %d",col,0,1,13,i); |
| 179 | } |
| 180 | |
| 181 | CImgDisplay disp(res,"Approximations levels by increasing scale",0); |
| 182 | CImgDisplay disp2(wav,"Wavelet coefficients by increasing scale",0); |
| 183 | while (!disp.is_closed() && !disp.is_keyQ() && !disp.is_keyESC() && |
| 184 | !disp2.is_closed() && !disp2.is_keyQ() && !disp2.is_keyESC()) { |
| 185 | if (disp.is_resized()) disp.resize().display(res); |
| 186 | if (disp2.is_resized()) disp2.resize().display(wav); |
| 187 | CImgDisplay::wait(disp,disp2); |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |