Go to the documentation of this file.00001
00002 #include "ipts_local_entropy.h"
00003
00004
00005
00006
00007
00008 #include <vcl_vector.h>
00009 #include <vcl_cmath.h>
00010 #include <vcl_cassert.h>
00011
00012 inline double histo_entropy_sum(const vcl_vector<int>& histo,
00013 unsigned min_v, unsigned max_v)
00014 {
00015 double sum = 0.0;
00016 for (unsigned k=min_v;k<=max_v;++k)
00017 if (histo[k]>0) sum+=histo[k]*vcl_log(double(histo[k]));
00018 return sum;
00019 }
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 void ipts_local_entropy(const vil_image_view<vxl_byte>& image,
00030 vil_image_view<float>& entropy,
00031 unsigned h, unsigned min_v, unsigned max_v)
00032 {
00033 vcl_vector<int> histo(256);
00034 const unsigned ni=image.ni(),nj=image.nj();
00035 assert(image.nplanes()==1 && ni>2*h && nj>2*h);
00036 unsigned eni = ni-2*h, enj=nj-2*h;
00037 entropy.set_size(eni,enj);
00038
00039 unsigned h2 = 2*h+1;
00040 unsigned n = h2*h2;
00041 double logn = vcl_log(double(n));
00042
00043 const vcl_ptrdiff_t istep=image.istep(), jstep=image.jstep();
00044
00045
00046 for (unsigned i=0;i<eni;++i)
00047 {
00048 const vxl_byte* im = &image(i,0);
00049
00050
00051 for (unsigned k=min_v;k<=max_v;++k) histo[k]=0;
00052 for (unsigned j1=0;j1<h2;++j1, im+=jstep)
00053 {
00054 const vxl_byte* p = im;
00055 for (unsigned i1=0;i1<h2;++i1,p+=istep) histo[*p]++;
00056 }
00057
00058 entropy(i,0) = float(logn-histo_entropy_sum(histo,min_v,max_v)/n);
00059
00060 for (unsigned j=1;j<enj;++j)
00061 {
00062
00063
00064 const vxl_byte* p = &image(i,j-1);
00065 for (unsigned i1=0;i1<h2;++i1,p+=istep) histo[*p]--;
00066
00067 p = &image(i,j+h2-1);
00068 for (unsigned i1=0;i1<h2;++i1,p+=istep) histo[*p]++;
00069
00070
00071 entropy(i,j) = float(logn-histo_entropy_sum(histo,min_v,max_v)/n);
00072 }
00073 }
00074 }