1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| #include "InpaintOperator.hpp"
#include <cmath>
using namespace FImage;
static float absolue(float a);
inline float absolue(float a)
{
if(a<0)
return -a;
else
return a;
}
void InpaintOperator::compute(Image & out, const Image & c, Image& mask)
{
int width = c.getWidth();
int height = c.getHeight();
static Image * pNewmask = NULL;
if(out.getNumComponents() != c.getNumComponents())
throw std::invalid_argument("InpaintOperator::compute");
if(mask.getHeight() != c.getHeight() || mask.getWidth() != c.getWidth())
throw std::invalid_argument("InpaintOperator::compue");
out.resize(c.getWidth(), c.getHeight());
if(pNewmask == NULL)
{
pNewmask = new Image(mask);
}
else
{
if(mask.getNumComponents() != pNewmask->getNumComponents())
{
delete pNewmask;
pNewmask = new Image(mask);
}
}
Image &newmask = *pNewmask;
out = c;
newmask = mask;
for(int canal = 0; canal < c.getNumComponents(); canal++)
{
/*taille des vecteurs dx et dy*/
int n = 8;
int dx [] =
{
-1,0,1,1,1,0,-1,-1
};
int dy [] =
{
-1,-1,-1,0,1,1,1,0
};
std::vector<int> contourInt1;
std::vector<int> contourInt2;
while(true)
{
// front-line masked/unmasked
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (mask.getPixel(x,y, canal)==0)
continue;
for (int i = 0; i < n; i++)
{
int xk = x + dx[i];
int yk = y + dy[i];
if (xk<0 || xk>=width)
continue;
if (yk<0 || yk>=height)
continue;
if (mask.getPixel(xk,yk, canal)>0)
continue;
contourInt1.push_back(x);
contourInt2.push_back(y);
break;
}
}
}
// exit when no front-line
if (contourInt1.empty())
break;
// isophotes continuation
for(int j=0;j<contourInt1.size();j++)
{
int x = contourInt1[j];
int y = contourInt2[j];
float value=0;
float wsum=0;
for (int i = 0; i < n; i++)
{
int xk = x + dx[i];
int yk = y + dy[i];
if (xk<0 || xk>=width)
continue;
if (yk<0 || yk>=height)
continue;
if (mask.getPixel(xk,yk, canal)>0)
continue;
// gradient
float norme = 0;
float angle = 0;
InpaintOperator::gradient(c, xk, yk, canal, norme, angle);
// gradient normal = isophote direction
angle+=M_PI/2;
// Weight of the propagation:
// 1. dotproduct ( gradient normal . propagation vector )
float pscal = cos(angle)*(-dx[i]) + (-sin(angle))*(-dy[i]);
pscal/=sqrt(dx[i]*dx[i]+dy[i]*dy[i]);
// 2. gradient magnitude (O -> omnidirectionnal)
float w = (norme)*absolue(pscal)+(1-norme)*1;
value += w*c.getPixel(xk,yk, canal);
wsum+=w;
}
if (wsum<=0)
continue;
value/=wsum;
// set new value
out.setPixel(x,y, canal, (int)value);
// pixel becomes unmasked
newmask.setPixel(x,y,canal, 0);
}
for(int j = 0; j<height; j++)
for(int i = 0; i<width; i++)
mask.setPixel(i,j, canal, newmask.getPixel(i,j, canal));
/*vide la lsite*/
contourInt1.clear();
contourInt2.clear();
}
}
}
void InpaintOperator::gradient(const Image& c, int x, int y, int canal, float & norme, float & angle)
{
int width = c.getWidth();
int height = c.getHeight();
float cst1 = (0.25*(2-sqrt(2.0)));
float cst2 = (0.5f*(sqrt(2.0)-1));
int px = x-1;
int nx = x+1;
int py = y-1;
int ny = y+1;
if (px<0)
px=0;
if (nx>=width)
nx=width-1;
if (py<0)
py=0;
if (ny>=height)
ny=height-1;
float Ipp=c.getPixel(px,py, canal);
float Ipc=c.getPixel(px,y, canal) ;
float Ipn=c.getPixel(px,ny, canal);
float Icp=c.getPixel(x,py, canal);
float Icn=c.getPixel(x,ny, canal);
float Inp=c.getPixel(nx,py, canal);
float Inc=c.getPixel(nx,y, canal) ;
float Inn=c.getPixel(nx,ny, canal);
float IppInn = cst1*(Inn-Ipp);
float IpnInp = cst1*(Ipn-Inp);
float gradx = (IppInn-IpnInp-cst2*Ipc+cst2*Inc);
float grady = (IppInn+IpnInp-cst2*Icp+cst2*Icn);
norme = sqrt( gradx*gradx + grady*grady );
angle = 0;
if (norme>0)
{
angle = acos(gradx/norme);
if (grady>0)
angle = 2*M_PI - angle;
}
norme/=255;
} |