Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

ImageGrey-conv.cpp

Go to the documentation of this file.
00001 /**************************************************************************
00008  *
00009  * Source code for Real Time Image Library (libRTImage)
00010  *
00011  * Leeds Vision Group give permission for this code to be copied, modified 
00012  * and distributed within the University of Leeds subject to the following 
00013  * conditions:-
00014  *
00015  * - The code is not to be used for commercial gain.
00016  * - The code and use thereof will be attributed to the author where
00017  *   appropriate (inluding demonstrations which rely on it's use).
00018  * - All modified, distributions of the source files will retain this header.
00019  *
00020  ****************************************************************************/
00021 
00022 #include <stdio.h>
00023 #include <fstream>
00024 #include <ImageGrey.h>
00025 #include <iostream>
00026 
00027 using namespace std;
00028 
00029 float ImageGrey::convolve(unsigned int x,
00030                           unsigned int y,
00031                           CMatrix      *kernel)
00041 {
00042     int x_off ;
00043     int y_off ;
00044     int x_cnt ;
00045     int y_cnt ;
00046     int x_min ;
00047     int x_max ;
00048     int y_min ;
00049     int y_max ;
00050     float        *k_ptr ; 
00051     int          *d_ptr ; 
00052     float        ret_val = 0 ;
00053 
00054     x_off = kernel->get_no_columns() / 2 ;
00055     y_off = kernel->get_no_columns() / 2 ;
00056 
00057     x_min = x-x_off ;
00058     x_max = x+x_off ;
00059     y_min = y-y_off ;
00060     y_max = y+y_off ;
00061 
00062     k_ptr = kernel->data ;
00063 
00064     if(x_min >= 0 &&
00065        x_max < width &&
00066        y_min >= 0 &&
00067        y_max < height){
00068     // Limis within image, no checks
00069 
00070         for(y_cnt=y_min ; y_cnt<=y_max ; y_cnt++){ 
00071             d_ptr = brightness + (y_cnt*width) + x_min ;
00072             for(x_cnt=x_min ; x_cnt<=x_max ; x_cnt++){ 
00073                 ret_val += *(k_ptr++) * *(d_ptr++) ;
00074             }
00075         }
00076   
00077     }
00078     else{
00079     // Kernel partially outside image .. must check
00080 
00081         for(y_cnt=y_min ; y_cnt<=y_max ; y_cnt++){
00082             d_ptr = brightness + (y_cnt*width) + x_min ;
00083             for(x_cnt=x_min ; x_cnt<=x_max ; x_cnt++){
00084                 if(x_cnt >= 0 &&
00085                    x_cnt < width &&
00086                    y_cnt >= 0 &&
00087                    y_cnt < height){
00088                 // Point on image
00089                     ret_val += *(k_ptr++) * *(d_ptr++) ;
00090                 }
00091                 else{
00092                     k_ptr++ ;
00093                     d_ptr++ ;
00094                 }
00095             }
00096         }
00097 
00098     } 
00099 
00100     return ret_val ;
00101 }
00102 
00103 void ImageGrey::plot_conv_kernel(unsigned int x,
00104                                  unsigned int y,
00105                                  CMatrix      *kernel,
00106                                  float        scale_fact,
00107                                  float        offset)
00119 {
00120     unsigned int x_off ;
00121     unsigned int y_off ;
00122     unsigned int x_cnt ;
00123     unsigned int y_cnt ;
00124     unsigned int x_min ;
00125     unsigned int x_max ;
00126     unsigned int y_min ;
00127     unsigned int y_max ;
00128     float        *k_ptr ;
00129     int          *d_ptr ;
00130 
00131     x_off = kernel->get_no_columns() / 2 ;
00132     y_off = kernel->get_no_columns() / 2 ;
00133 
00134     x_min = x-x_off ;
00135     x_max = x+x_off ;
00136     y_min = y-y_off ;
00137     y_max = y+y_off ;
00138 
00139     k_ptr = kernel->data ;
00140 
00141     for(y_cnt=y_min ; y_cnt<=y_max ; y_cnt++){
00142         d_ptr = brightness + (y_cnt*width) + x_min ;
00143         for(x_cnt=x_min ; x_cnt<=x_max ; x_cnt++){
00144            *(d_ptr++) = (int)((float)(*(k_ptr++) + offset) * scale_fact + 0.5) ;
00145         }
00146     }
00147 
00148 }
00149 
00150 float ImageGrey::wavelet_convolve(unsigned int x,
00151                                   unsigned int y,
00152                                   Wavelet *wavelet,
00153                                   float        *real,
00154                                   float        *imag)
00167 {
00168 
00169     register float    r ;
00170     register float    i ;
00171 
00172 
00173     r = convolve(x, y, wavelet->real_kernel) ;
00174     i = convolve(x, y, wavelet->imag_kernel) ;
00175 
00176     if(real != NULL) *real = r ;
00177     if(imag != NULL) *imag = i ;
00178 
00179     i *= i ;
00180     r *= r ;
00181 
00182     return (r + i) ;
00183 }
00184 
00185 void ImageGrey::blur(unsigned int size, ImageGrey &result)
00193 {
00194     unsigned int x_cnt ;
00195     unsigned int y_cnt ;
00196     int          sz_2 ;
00197     unsigned int hb ;
00198     unsigned int wb ;
00199     unsigned int sz_sqd ;
00200     int          x_cnt2 ;
00201     int          y_cnt2 ;
00202     int          *r_ptr ;
00203     int          *d_ptr ;
00204     int          *d2_ptr ;
00205 
00206     sz_2 = size / 2 ;
00207     wb = width - sz_2 - 1 ;
00208     hb = height - sz_2 - 1 ;
00209     sz_sqd = size * size ;
00210 
00211     d_ptr = brightness ;
00212     r_ptr = result.brightness ;
00213 
00214     for(y_cnt=0 ; y_cnt<height ; y_cnt++){
00215         for(x_cnt=0 ; x_cnt<width ; x_cnt++, d_ptr++, r_ptr++){
00216             if(x_cnt < sz_2 || x_cnt > wb  ||
00217                y_cnt < sz_2 || y_cnt > hb ){
00218             // Too near edge
00219                 *r_ptr = *d_ptr ;
00220             }
00221             else{
00222                 *r_ptr = 0 ;
00223                 for(y_cnt2=-sz_2 ; y_cnt2<=sz_2 ; y_cnt2++){
00224                     d2_ptr = d_ptr + y_cnt2*width - sz_2 ;
00225                     for(x_cnt2=-sz_2 ; x_cnt2<=sz_2 ; x_cnt2++, d2_ptr++){
00226                         *r_ptr += *d2_ptr ; 
00227                     }
00228                 }
00229                 *r_ptr /= sz_sqd ;
00230             }
00231         }
00232     }
00233 }
00234 

Generated at Fri Aug 13 17:29:21 2004 for libRTImage by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001