00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __OPENKN_IMAGE__IMAGECONVOLUTION_HPP__
00027 #define __OPENKN_IMAGE__IMAGECONVOLUTION_HPP__
00028
00029
00030
00031
00032
00033 #include <iostream>
00034
00035
00036
00037
00038 #include "Image.hpp"
00039 #include "ImageProcessing.hpp"
00040 #include "ConvolutionKernel.hpp"
00041 #include "ConvolutionOperator.hpp"
00042
00043
00044
00045
00046 namespace kn{
00047
00048
00049
00050
00051
00052
00056 template<typename T>
00057 class ImageConvolution : public ImageProcessing<T>{
00058
00059
00060
00061
00062 public:
00068 ImageConvolution(const ConvolutionKernel & kernel, const ConvolutionOperator & op);
00069
00070
00074 ImageConvolution(const ImageConvolution & e);
00075
00076
00080 ~ImageConvolution();
00081
00082 protected :
00085 ConvolutionKernel kernel;
00086
00089 ConvolutionOperator * op;
00090
00095 void computeConvolution(const Image<T> & img, Image<T>& convo);
00096
00097
00098
00099 public :
00104 void filterOnCopy(const Image<T> & img, Image<T> & result);
00105
00109 void filter(Image<T> & img);
00110
00114 const ConvolutionKernel& getKernel() const;
00115
00116
00120 const ConvolutionOperator& getOperator() const;
00121
00122 };
00123
00124
00125
00126
00127
00128
00129
00130
00131 template<typename T>
00132 ImageConvolution<T>::ImageConvolution(const ConvolutionKernel & kernel,
00133 const ConvolutionOperator& op) {
00134 this->kernel = kernel;
00135 this->op = op.clone();
00136 }
00137
00138 template<typename T>
00139 ImageConvolution<T>::ImageConvolution(const ImageConvolution& e) {
00140 this->kernel = e.getKernel();
00141 this->op = e.getOperator().clone();
00142 }
00143
00144 template<typename T>
00145 ImageConvolution<T>::~ImageConvolution() {
00146 delete op;
00147 }
00148
00149 template<typename T>
00150 void ImageConvolution<T>::computeConvolution(const Image<T>& img, Image<T>& tmpImg){
00151 for(size_t j = 0; j < img.height(); j++){
00152 int yLow = kernel.yLowerBound();
00153 yLow = (yLow < -(int)j) ? -(int)j : yLow;
00154 int yUp = kernel.yUpperBound();
00155 yUp = (yUp > (int)img.height() - (int)j) ? (int)img.height() - (int)j : yUp;
00156
00157 for(size_t i = 0; i < img.width(); i++){
00158 int xLow = kernel.xLowerBound();
00159 xLow = (xLow < -(int)i) ? -(int)i : xLow;
00160 int xUp = kernel.xUpperBound();
00161 xUp = (xUp > (int)img.width() - (int)i) ? (int)img.width() - (int)i : xUp;
00162 for(unsigned int d = 0; d < img.nbChannel(); d++) {
00163 op->initialize();
00164
00165 for(int y = yLow; y < yUp; y++){
00166 for(int x = xLow; x < xUp; x++){
00167 try {
00168 (*op)(kernel(x,y), double(img(i+x, j+y)[d]));
00169 }
00170 catch(ImageException& ){
00171
00172 }
00173 }
00174 }
00175 tmpImg(i,j)[d] = (T) op->getResult();
00176 }
00177 }
00178 }
00179 }
00180
00181
00182 template<typename T>
00183 void ImageConvolution<T>::filter(Image<T>& img){
00184 Image<T> tmpImg(img.width(),img.height(),img.nbChannel());
00185 computeConvolution(img,tmpImg);
00186 std::copy(tmpImg.begin(), tmpImg.end(), img.begin());
00187 }
00188
00189
00190 template<typename T>
00191 void ImageConvolution<T>::filterOnCopy(const Image<T>& img, Image<T> & result){
00192 Image<T> tmpImg(img.width(),img.height(),img.nbChannel());
00193 computeConvolution(img,tmpImg);
00194 std::copy(tmpImg.begin(), tmpImg.end(), result.begin());
00195 }
00196
00197 template<typename T>
00198 const ConvolutionKernel& ImageConvolution<T>::getKernel()const {
00199 return kernel;
00200 }
00201
00202 template<typename T>
00203 const ConvolutionOperator& ImageConvolution<T>::getOperator()const {
00204 return *op;
00205 }
00206
00207
00208
00209
00210 typedef ImageConvolution<float> ImageConvolutionf;
00211 typedef ImageConvolution<double> ImageConvolutiond;
00212 typedef ImageConvolution<unsigned char> ImageConvolution8u;
00213 typedef ImageConvolution<unsigned short int> ImageConvolution16u;
00214 typedef ImageConvolution<unsigned int> ImageConvolution32u;
00215 typedef ImageConvolution<unsigned long int> ImageConvolution64u;
00216 typedef ImageConvolution<char> ImageConvolution8s;
00217 typedef ImageConvolution<short int> ImageConvolution16s;
00218 typedef ImageConvolution<int> ImageConvolution32s;
00219 typedef ImageConvolution<long int> ImageConvolution64s;
00220
00221
00222
00223
00224
00225
00226
00227
00228 }
00229
00230
00231
00232
00233 #endif
00234