tan_specular_highlights.cpp 10.5 KB
Newer Older
1 2 3 4
/**
 * @author David Geissbuhler <andre.anjos@idiap.ch>
 * @date Tue 27 Jun 15:54:00 2016
 *
5
 *  Original version of the specular highlights removal code by Robby T. Tan
6 7 8 9 10
 *  reference:
 *  "separating reflection components of textured surfaces using a single image"
 *  by Robby T. Tan, Katsushi Ikeuchi,
 *  IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI),
 *  27(2), pp.179-193, February, 2005
11 12 13
 *
 *  This is the original implementation based on the C++ code provided by Prof.
 *  Robby Tan but using Blitz++ arrays:
14 15 16 17 18 19 20 21 22 23 24
 *       http://tanrobby.github.io/code.html#
 *       http://tanrobby.github.io/code/highlight.zip
 *
 */

#include <blitz/array.h>

#define SPECULARX		10
#define SPECULARY		11
#define DIFFUSE			12
#define BOUNDARY		13
25 26
#define NOISE			14
#define CAMERA_DARK		15
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

void specular_free_image( blitz::Array<float ,3> &src,
                          blitz::Array<int,2>   &src_i,
					                blitz::Array<float ,3> &sfi);

void iteration(           blitz::Array<float ,3> &src,
                          blitz::Array<int,2>   &src_i,
                          blitz::Array<float ,3> &sfi,
				                  float  epsilon);

int init(                 blitz::Array<float ,3> &src,
                          blitz::Array<int,2>   &src_i,
                          blitz::Array<float ,3> &sfi,
				                  float  epsilon);

int reset_labels(         blitz::Array<int,2>   &src_i);

// the main function to remove highlights from a single image
void remove_highlights(   blitz::Array<float ,3> &img,
                          blitz::Array<float ,3> &diff,
                          blitz::Array<float ,3> &sfi,
                          blitz::Array<float ,3> &residue,
                          float  epsilon)
{
  // flags
  int dim_x = img.shape()[2];
  int dim_y = img.shape()[1];

  blitz::Array<int,2> img_i(dim_y, dim_x);

  //SPECULAR-FREE IMAGE

  specular_free_image(img, img_i, sfi);

  //ITERATIVE PART
  float  step =0.01f;

  // copy source
  diff = img;

  while( epsilon >= 0.0 )
  {
    // run the main iteration
    //printf("*");
    iteration(diff, img_i, sfi, epsilon);
    epsilon -= step;
    //printf(": %f\n",epsilon);
  }

  // compute residue
  residue = img - diff;
}

// utilities

inline float  tot(float  r, float  g, float  b)
{
  return r + g + b;
}

inline float  max(float  r, float  g, float  b)
{
  float  max_ = r;
  if(g>max_) max_ = g;
  if(b>max_) max_ = b;
  return max_;
}

inline float  r_chroma(float  r, float  g, float  b)
{
  float  tot_ = tot(r,g,b);
  if (tot_!=0)  return r/tot_;
  else          return 0;
}

inline float  g_chroma(float  r, float  g, float  b)
{
  float  tot_ = tot(r,g,b);
  if (tot_!=0)  return g/tot_;
  else          return 0;
}

inline float  b_chroma(float  r, float  g, float  b)
{
  float  tot_ = tot(r,g,b);
  if (tot_!=0)  return b/tot_;
  else          return 0;
}

inline float  max_chroma(float  r, float  g, float  b)
{
  float  tot_ = tot(r,g,b);
  if (tot_!=0)  return max(r,g,b)/tot_;
  else          return 0;
}

// remove the specular component from source

void specular_free_image( blitz::Array<float ,3> &src,
                          blitz::Array<int,2>   &src_i,
					                blitz::Array<float ,3> &sfi)
{
  float  Lambda=0.6f;
  float  camDark=10.0f;	// for pixels that are too dark
  float  lambdaConst = 3.0f * Lambda - 1.0f;

  //SPECULAR FREE IMAGE
  int dim_x = src.shape()[2];
  int dim_y = src.shape()[1];

  int y,x;
  for(y = 0; y < dim_y; y++){
    for(x = 0; x < dim_x; x++){
      //get the rgb values
      float  r = src(0,y,x);
      float  g = src(1,y,x);
      float  b = src(2,y,x);

      //copy the rgb to the sfi
      sfi(0,y,x) = r;
      sfi(1,y,x) = g;
      sfi(2,y,x) = b;

      // init
      src_i(y,x) = 0;

      //check for camera dark and achromatic pixels
      if(((r < camDark) &&
          (g < camDark) &&
          (b < camDark)))
      {
	       src_i(y,x) = CAMERA_DARK;
	       continue;
      }

      //perform the specular-to-diffuse mechanism
163

164 165 166
      float  c    = max_chroma(r,g,b);
      float  numr = max(r,g,b) * (3.0f * c - 1.0f);
      float  denm = c * lambdaConst;
167
      float  dI   = numr / denm;
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

      float  sI = (tot(r,g,b) - dI)/3.0f;

      float  dr,dg,db;
      dr = (r - sI);
      dg = (g - sI);
      db = (b - sI);

      if(dr<0) dr=0;
      if(dg<0) dg=0;
      if(db<0) db=0;

      if(dr>255) dr=255;
      if(dg>255) dg=255;
      if(db>255) db=255;

      sfi(0,y,x) = dr;
      sfi(1,y,x) = dg;
      sfi(2,y,x) = db;
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    }
  }

}

// to apply specular to diffuse equation or mechanism

inline int specular_2_diffuse(int y, int x, blitz::Array<float ,3> &iro,
                              blitz::Array<int,2> &iro_i, float  maxChroma)
{
  float  c = max_chroma(iro(0,y,x), iro(1,y,x), iro(2,y,x));
  float  m = max(iro(0,y,x), iro(1,y,x), iro(2,y,x));
  float  t = tot(iro(0,y,x), iro(1,y,x), iro(2,y,x));
  float  numr = (m*(3.0f*c - 1.0f));
  float  denm = (c*(3.0f*maxChroma - 1.0f));

204
  float  dI = numr / denm;
205

206
  float  sI = (t - dI)/3.0f;
207

208 209 210
  float  nr = (iro(0,y,x) - sI);
  float  ng = (iro(1,y,x) - sI);
  float  nb = (iro(2,y,x) - sI);
211

212
  if(nr<=0 || ng<=0 || nb<=0)
213 214 215 216
  {
    iro_i(y,x)=NOISE;
    return 1;
  }
217 218 219 220 221 222 223 224
  else
  {
    iro(0,y,x) = nr;
    iro(1,y,x) = ng;
    iro(2,y,x) = nb;

    return 0;
  }
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
}

// specular reduction mechanism

void iteration( blitz::Array<float ,3> &src,
                blitz::Array<int,2>   &src_i,
                blitz::Array<float ,3> &sfi,
				        float  epsilon)
{
  int x,y;
  int dim_x = src.shape()[2];
  int dim_y = src.shape()[1];

  float  thR = 0.1f, thG = 0.1f;

  // to have the initial labels
  int count = init(src,src_i,sfi,epsilon);
  int pcount;

  while(1)
  {
    for(y=0;y<dim_y-1;y++)
    {
	    for(x=0;x<dim_x-1;x++)
      {

        if(src_i(y,x)==CAMERA_DARK) continue;

        //get the rgb values
        float  r = src(0,y,x);
        float  g = src(1,y,x);
        float  b = src(2,y,x);

        float  cr = r_chroma(r,g,b);		// red chroma
        float  cg = g_chroma(r,g,b);		// green chroma

        //get the rgb values
        float  rx = src(0,y,x+1);
        float  gx = src(1,y,x+1);
        float  bx = src(2,y,x+1);

        float  cr_next_x = r_chroma(rx,gx,bx); // red chroma
        float  cg_next_x = g_chroma(rx,gx,bx); // green chroma

        //get the rgb values
        float  ry = src(0,y+1,x);
        float  gy = src(1,y+1,x);
        float  by = src(2,y+1,x);

        float  cr_next_y = r_chroma(ry,gy,by); // red chroma
        float  cg_next_y = g_chroma(ry,gy,by); // green chroma

        // derivatives
        float  drx = cr_next_x-cr;//pixel right
        float  dgx = cg_next_x-cg;
        float  dry = cr_next_y-cr;//pixel below
        float  dgy = cg_next_y-cg;

        if(src_i(y,x) == SPECULARX)
        {
          //if it is  a boundary in the x direction
          if(fabs(drx) > thR && fabs(dgx) > thG)
          {
            //pixel right
            src_i(y,x)=BOUNDARY;
            continue;
          }

          //if it is a noise
          if(fabs(max_chroma(r,g,b) - max_chroma(rx,gx,bx)) < 0.01)
          {
            src_i(y,x)=NOISE;
            continue;
          }

          //reduce the specularity at x direction
          if(max_chroma(r,g,b) < max_chroma(rx,gx,bx))
          {
            specular_2_diffuse(y,x,src,src_i,max_chroma(rx,gx,bx));
            src_i(y,x)=DIFFUSE;
            src_i(y,x+1)=DIFFUSE;
          }
          else
          {
            specular_2_diffuse(y,x+1,src,src_i,max_chroma(r,g,b));
            src_i(y,x)=DIFFUSE;
            src_i(y,x+1)=DIFFUSE;
          }
        }

	      if(src_i(y,x) == SPECULARY)
	      {
	        //if it is a boundary in the y direction
	        if(fabs(dry) > thR && fabs(dgy) > thG)
          {
            //pixel right
		        src_i(y,x)=BOUNDARY;
		        continue;
	        }

	        //if it is a noise
	        if(fabs(max_chroma(r,g,b) - max_chroma(ry,gy,by))<0.01)
          {
		        src_i(y,x)=NOISE;
		        continue;
	        }

	        //reduce the specularity in y direction
	        if(max_chroma(r,g,b) < max_chroma(ry,gy,by))
          {
            specular_2_diffuse(y,x,src,src_i,max_chroma(ry,gy,by));
            src_i(y,x)=DIFFUSE;
            src_i(y+1,x)=DIFFUSE;
          }
          else
          {
            specular_2_diffuse(y+1,x,src,src_i,max_chroma(r,g,b));
            src_i(y,x)=DIFFUSE;
            src_i(y+1,x)=DIFFUSE;
          }
	      }
	    }
    }

    pcount=count;
    count = init(src,src_i,sfi,epsilon);

    if(count==0)
	    break;
    if(pcount<=count)
	    break;
  }

  reset_labels(src_i);
}

// to have initial labels

int init(   blitz::Array<float ,3> &src,
            blitz::Array<int,2>   &src_i,
            blitz::Array<float ,3> &sfi,
				    float  epsilon)
{
  int dim_x = src.shape()[2];
  int dim_y = src.shape()[1];
  int x,y;	// pixel iterators

  int count=0;

  for(y = 1; y < dim_y - 1; y++){
    for(x = 1; x < dim_x - 1; x++){
      switch(src_i(y,x))
      {
        case BOUNDARY:
        case NOISE:
        case CAMERA_DARK:
	        continue;
	      break;
      }

      float  src_tot_0 = tot(src(0,y,x), src(1,y,x), src(2,y,x));
      float  src_tot_x = tot(src(0,y,x+1), src(1,y,x+1), src(2,y,x+1));
      float  src_tot_y = tot(src(0,y+1,x), src(1,y+1,x), src(2,y+1,x));

      float  sfi_tot_0 = tot(sfi(0,y,x), sfi(1,y,x), sfi(2,y,x));
      float  sfi_tot_x = tot(sfi(0,y,x+1), sfi(1,y,x+1), sfi(2,y,x+1));
      float  sfi_tot_y = tot(sfi(0,y+1,x), sfi(1,y+1,x), sfi(2,y+1,x));

      float  dlog_src_x = log( fabs( src_tot_x - src_tot_0 ) );
      float  dlog_src_y = log( fabs( src_tot_y - src_tot_0 ) );

      float  dlog_sfi_x = log( fabs( sfi_tot_x - sfi_tot_0 ) );
      float  dlog_sfi_y = log( fabs( sfi_tot_y - sfi_tot_0 ) );

      float  dlogx = (dlog_src_x - dlog_sfi_x);
      float  dlogy = (dlog_src_y - dlog_sfi_y);

      dlogx=fabs(dlogx);
      dlogy=fabs(dlogy);

      // specular in the x direction
406
      if(dlogx > epsilon)
407 408 409 410 411 412 413
      {
  	     src_i(y,x) = SPECULARX;
	       count++;
	       continue;  // go to the next pixel
      }

      //specular in the y direction
414
      if(dlogy > epsilon)
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
      {
	       src_i(y,x)= SPECULARY;
	       count++;
	       continue;
      }

      src_i(y,x) = DIFFUSE;

    }
  }

  return count;	// return the number of specular pixels
}

// to reset the label of the pixels

int reset_labels(blitz::Array<int,2> &src_i)
{
  int dim_x = src_i.shape()[1];
  int dim_y = src_i.shape()[0];

  for(int y=0;y<dim_y;y++)
  {
    for(int x=0;x<dim_x;x++)
    {
      if(src_i(y,x)==CAMERA_DARK) continue;
      src_i(y,x)=0;
    }
  }
  return 0;
}