Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleConvolution.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 /**
25  @example ExampleConvolution.cpp
26  @relates Convolution
27  @brief Example for using Convolution Filter
28  @ingroup g_examples
29  @author fkellner, 11/2008
30 */
31 // must be first:
32 //#include <Base/Common/LeakChecking.h>
33 
34 #include <iostream>
35 #include "Filter/Convolution.hh"
36 #include <Base/Common/BIASpragma.hh>
37 
38 using namespace BIAS;
39 using namespace std;
40 
41 int main(int argc, char *argv[])
42 {
43  Image<unsigned char> src,dst;
44  src.Init(10,10);
45  dst.Init(10,10);
46 
48  FilterMask fm;
49 
50  unsigned char *srcdata=src.GetImageData();
51  unsigned char *dstdata=dst.GetImageData();
52 
53  for(int x=0;x<100;x++)
54  {
55  srcdata[x]=0;
56  dstdata[x]=0;
57  }
58  srcdata[10*5+5]=100;
59  srcdata[0]=100;
60 
61  Matrix<short> kern;
62  Vector<short> khor;
63  Vector<short> kver;
64  kern.newsize(3,3);
65  kern[0][0]=1;
66  kern[0][1]=2;
67  kern[0][2]=1;
68  kern[1][0]=2;
69  kern[1][1]=4;
70  kern[1][2]=2;
71  kern[2][0]=1;
72  kern[2][1]=2;
73  kern[2][2]=1;
74 
75  khor.newsize(3);
76  kver.newsize(3);
77  khor[0]=1;
78  khor[1]=2;
79  khor[2]=1;
80  kver[0]=1;
81  kver[1]=2;
82  kver[2]=1;
83 
84 
85  fm.Init(kern,1);
86  fm.CreateFloatFilter();
87 
88  conv.SetKernel(fm);
89  conv.FilterInt(src,dst);
90  cout <<"dst matrix:\n";
91  for(int y=0;y<10;y++)
92  {
93  for(int x=0;x<10;x++)
94  cout <<(int)dstdata[y*10+x]<<" ";
95  cout <<endl;
96  }
97 
98  fm.Init(khor,kver,1,0);
99  conv.SetKernel(fm);
100  conv.FilterInt(src,dst);
101  cout <<"dst sep:\n";
102  for(int y=0;y<10;y++)
103  {
104  for(int x=0;x<10;x++)
105  cout <<(int)dstdata[y*10+x]<<" ";
106  cout <<endl;
107  }
108 
109  conv.SetKernel(fm);
110  conv.FilterFloat(src,dst);
111  cout <<"dst matrix float:\n";
112  for(int y=0;y<10;y++)
113  {
114  for(int x=0;x<10;x++)
115  cout <<(int)dstdata[y*10+x]<<" ";
116  cout <<endl;
117  }
118 
119  conv.SetKernel(fm);
120  conv.FilterFloat(src,dst);
121  cout <<"dst sep float:\n";
122  for(int y=0;y<10;y++)
123  {
124  for(int x=0;x<10;x++)
125  cout <<(int)dstdata[y*10+x]<<" ";
126  cout <<endl;
127  }
128 
129  return 0;
130 }
class for column vectors with arbitrary size
virtual int FilterFloat(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
do the convolution using floating point calculations
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
void CreateFloatFilter()
create the float filter from the int filter
Definition: FilterMask.cpp:91
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
void SetKernel(FilterMask &fm)
set a new filter mask
Definition: Convolution.hh:96
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
matrix class with arbitrary size, indexing is row major.
void Init(const Matrix< FM_INT > &kernel, int rs, bool ResetOtherType=FM_RESET_OTHER)
sets _sKernel and also _fKernel.
Definition: FilterMask.cpp:37
A filter mask (or a kernel) used for convolution.
Definition: FilterMask.hh:61
virtual int FilterInt(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
do the convolution using integer arithmetics and shifts