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 212 213 214 215 216 217 218 219 220 221 222 223 224 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 406 407 408 409 410 411 412 413 414 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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
import ij.IJ;
import ij.ImagePlus;
import ij.gui.GenericDialog;
import ij.plugin.filter.PlugInFilter;
import ij.process.ByteProcessor;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Xavier Philippeau
*
* UnNoise Plugin.
*
* A noise detection and removal filter with edge preservation.
*/
public class UnNoise_ implements PlugInFilter {
private static final int ROAD = 0;
private static final int VARIANCE = 1;
private ImagePlus imgOrig = null;
private int aperture = 0;
private double factor = 0;
private int itermax = 0;
private int estimator;
/**
* This method gets a reference to the image to be locally ranked
* and returns the filter capabilities.
* @param arg calls for the "about" information.
* @param imp This is the image to be processed
*
* @return Returns DOES_8G + DOES_RGB or DONE.
*/
public int setup(String arg, ImagePlus imp) {
// about...
if (arg.equals("about")) {
showAbout();
return DONE;
}
// else...
if (imp==null) return DONE;
//original image
this.imgOrig = imp ;
// Configuration dialog.
GenericDialog gd = new GenericDialog("Parameters");
gd.addChoice("Estimator",new String[] {"ROAD","Variance"}, "ROAD");
gd.addNumericField("Window aperture (size=2*a+1)",2,0);
gd.addNumericField("Variance factor",1.0,1);
gd.addNumericField("Iterations",5,0);
while(true) {
gd.showDialog();
if ( gd.wasCanceled() ) return DONE;
String estimator = gd.getNextChoice();
if (estimator.equalsIgnoreCase("ROAD")) this.estimator=ROAD;
if (estimator.equalsIgnoreCase("Variance")) this.estimator=VARIANCE;
this.aperture = (int) gd.getNextNumber();
this.factor = (double) gd.getNextNumber();
this.itermax = (int) gd.getNextNumber();
if (this.aperture<=0) continue;
if (this.factor<=0) continue;
if (this.itermax<=0) continue;
break;
}
gd.dispose();
return DOES_8G+DOES_RGB;
}
/**
* Filters use this method to process the image. If the
* SUPPORTS_STACKS flag was set, it is called for each slice in
* a stack. ImageJ will lock the image before calling
* this method and unlock it when the filter is finished.
*/
public void run(ImageProcessor ip) {
// project image to HSV color space
ByteProcessor work = null;
if (this.imgOrig.getBitDepth()==24) work=transformRGB(this.imgOrig);
if (this.imgOrig.getBitDepth()==8) work=transformGray(this.imgOrig);
// filter = Noise Estimation (->mask) + Pixel replacement
ByteProcessor mask = null;
for(int i=0;i<this.itermax;i++) {
if (this.estimator==ROAD)
mask = this.ROAD(work,this.aperture,this.factor);
if (this.estimator==VARIANCE)
mask = this.Variance(work,this.aperture,this.factor);
work = this.inpaint(work,mask);
}
// project image to RGB color space
ImageProcessor newIp = null;
if (this.imgOrig.getBitDepth()==24) newIp=invTransformRGB(this.imgOrig,work);
if (this.imgOrig.getBitDepth()==8) newIp=invTransformGray(this.imgOrig,work);
// show new image
ImagePlus newImg = new ImagePlus(this.imgOrig.getTitle()+"_UnNoised", newIp);
newImg.show();
}
// RGB -> HSV
private ByteProcessor transformRGB(ImagePlus img) {
ByteProcessor bp = new ByteProcessor(img.getWidth(),img.getHeight());
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int[] lrgb = img.getPixel(x,y);
float[] hsb = Color.RGBtoHSB(lrgb[0],lrgb[1],lrgb[2],null);
bp.set(x,y,(int)(hsb[2]*255));
}
}
return bp;
}
// GRAY -> HSV
private ByteProcessor transformGray(ImagePlus img) {
ByteProcessor bp = new ByteProcessor(img.getWidth(),img.getHeight());
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int[] lrgb = img.getPixel(x,y);
bp.set(x,y,lrgb[0]);
}
}
return bp;
}
// HSV -> RGB
private ImageProcessor invTransformRGB(ImagePlus img, ByteProcessor bp) {
ImageProcessor out = new ColorProcessor(img.getWidth(),img.getHeight());
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int[] lrgb = img.getPixel(x,y);
float[] hsb = Color.RGBtoHSB(lrgb[0],lrgb[1],lrgb[2],null);
int rgb = Color.HSBtoRGB(hsb[0],hsb[1],bp.get(x,y)/255f);
out.set(x,y,rgb);
}
}
return out;
}
// HSV -> GRAY
private ImageProcessor invTransformGray(ImagePlus img, ByteProcessor bp) {
ImageProcessor out = new ByteProcessor(img.getWidth(),img.getHeight());
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
out.set(x,y,bp.get(x,y));
}
}
return out;
}
// About...
private void showAbout() {
IJ.showMessage("UnNoise...","UnNoise Filter by Pseudocode");
}
/**
* Noise estimation using the ROAD Estimator
*
* (ROAD: R. Garnett, T. Huegerich, C. Chui and W.-J. He)
*
* @param c Input data (luminosity/gray-level)
* @param aperture Window aperture
* @param coef variance theshold factor
* @return mask (255="masked" 0="unmasked")
*/
private ByteProcessor ROAD(ByteProcessor c, int aperture, double coef) {
int width = c.getWidth();
int height = c.getHeight();
// value-to-distance table
double[] distance = new double[256];
distance[0]=0;
for(int i=1;i<256;i++) {
double x = (double)i/255;
double log = Math.log(x)/Math.log(2);
distance[i]=1+Math.max(log,-5)/5;
}
ByteProcessor mask = new ByteProcessor(width,height);
for (int y=0; y<height; y++) {
for (int x=0; x<width; x++) {
// compute distances between pixels
int[] dist = new int[(2*aperture+1)*(2*aperture+1)];
int average=0; int count=0;
for(int dy=-aperture;dy<=aperture;dy++) {
for(int dx=-aperture;dx<=aperture;dx++) {
if (c.getPixel(x+dx,y+dy)<0) continue;
int i = Math.abs(c.getPixel(x+dx,y+dy)-c.getPixel(x,y));
dist[count]=(int)(255.0*distance[i]);
count++;
average+=c.getPixel(x+dx,y+dy);
}
}
average/=count;
// Noise estimation (ROAD at rank nmb_pixels/2)
Arrays.sort(dist,0,count);
int road = 0;
int rank=count/2;
for(int i=0;i<rank;i++) road+=dist[i];
// compute threshold (= variance)
int etype=0; count=0;
for(int dy=-aperture;dy<=aperture;dy++) {
for(int dx=-aperture;dx<=aperture;dx++) {
if ((x+dx)<0) continue;
if ((x+dx)>=width) continue;
if ((y+dy)<0) continue;
if ((y+dy)>=height) continue;
double e = c.getPixel(x+dx,y+dy)-average;
etype+=e*e;
count++;
}
}
etype=(int)Math.sqrt(etype/count);
int threshold = (int)(rank*etype);
// pixel exceed threshold -> noise
if ( (road>=(threshold*coef)) && (etype>0) ) {
mask.putPixel(x,y,255);
} else {
mask.putPixel(x,y,0);
}
} }
return mask;
}
/**
* Noise estimation using Variance Estimator
*
* @param c Input data (luminosity/gray-level)
* @param aperture Window aperture
* @param coef variance theshold factor
* @return mask (255="masked" 0="unmasked")
*/
private ByteProcessor Variance(ByteProcessor c, int aperture, double coef) {
int width = c.getWidth();
int height = c.getHeight();
ByteProcessor mask = new ByteProcessor(width,height);
for (int y=0; y<height; y++) {
for (int x=0; x<width; x++) {
// compute average
float average=0; int count=0;
for(int dy=-aperture;dy<=aperture;dy++) {
for(int dx=-aperture;dx<=aperture;dx++) {
if ((x+dx)<0) continue;
if ((x+dx)>=width) continue;
if ((y+dy)<0) continue;
if ((y+dy)>=height) continue;
average += c.get(x+dx,y+dy);
count++;
}
}
average/=count;
// compute variance
float etype=0; count=0;
for(int dy=-aperture;dy<=aperture;dy++) {
for(int dx=-aperture;dx<=aperture;dx++) {
if ((x+dx)<0) continue;
if ((x+dx)>=width) continue;
if ((y+dy)<0) continue;
if ((y+dy)>=height) continue;
float e = Math.abs(c.get(x+dx,y+dy)-average);
etype += e*e;
count++;
}
}
etype=(float)Math.sqrt(etype/count);
// pixel exceed threshold -> noise
if (Math.abs(c.get(x,y)-average)>coef*etype) {
mask.set(x,y,255);
} else {
mask.set(x,y,0);
}
} }
return mask;
}
/**
* Fast (not accurate) Inpainting
*
* @param c Input data (luminosity/gray-level)
* @param mask masked pixel to evaluate
* @return Output data (luminosity/gray-level)
*/
private ByteProcessor inpaint(ByteProcessor c, ByteProcessor mask) {
int width = c.getWidth();
int height = c.getHeight();
ByteProcessor c2 = new ByteProcessor(width,height);
for (int y=0; y<height; y++)
for (int x=0; x<width; x++)
c2.set(x,y,c.get(x,y));
ByteProcessor newmask = new ByteProcessor(width,height);
for (int y=0; y<height; y++)
for (int x=0; x<width; x++)
newmask.set(x,y,mask.get(x,y));
int n = 8;
int[] dx = new int[] {-1,0,1,1,1,0,-1,-1};
int[] dy = new int[] {-1,-1,-1,0,1,1,1,0};
while(true) {
// front-line masked/unmasked
List contourInt = new ArrayList();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (mask.get(x,y)==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.get(xk,yk)>0) continue;
contourInt.add( new int[] {x,y} );
break;
}
}
}
// exit when no front-line
if (contourInt.isEmpty()) break;
// isophotes continuation
for(int j=0;j<contourInt.size();j++) {
int[] pixel = (int[]) contourInt.get(j);
int x = pixel[0];
int y = pixel[1];
double value=0;
double 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.get(xk,yk)>0) continue;
// gradient
double[] grad = gradient(c,xk,yk);
double norme = grad[0];
double angle = grad[1];
// gradient normal = isophote direction
angle+=Math.PI/2;
// Weight of the propagation:
// 1. dotproduct ( gradient normal . propagation vector )
double pscal = Math.cos(angle)*(-dx[i]) + (-Math.sin(angle))*(-dy[i]);
pscal/=Math.sqrt(dx[i]*dx[i]+dy[i]*dy[i]);
// 2. gradient magnitude (O -> omnidirectionnal)
double w = (norme)*Math.abs(pscal)+(1-norme)*1;
value += w*c.get(xk,yk);
wsum+=w;
}
if (wsum<=0) continue;
value/=wsum;
// set new value
c2.set(x,y,(int)value);
// pixel becomes unmasked
newmask.set(x,y,0);
}
mask=newmask;
}
return c2;
}
/**
* Compute the local Gradient of one pixel
*
* @param c Input Data
* @param x X coord
* @param y Y Coord
* @return double[0] = gradient norme (0..1), double[1] = gradient direction (0..2*PI)
*/
private double[] gradient(ByteProcessor c, int x, int y) {
int width = c.getWidth();
int height = c.getHeight();
double cst1 = (0.25*(2-Math.sqrt(2.0)));
double cst2 = (0.5f*(Math.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;
int Ipp=c.get(px,py);
int Ipc=c.get(px,y) ;
int Ipn=c.get(px,ny);
int Icp=c.get(x,py);
int Icn=c.get(x,ny);
int Inp=c.get(nx,py);
int Inc=c.get(nx,y) ;
int Inn=c.get(nx,ny);
double IppInn = cst1*(Inn-Ipp);
double IpnInp = cst1*(Ipn-Inp);
int gradx = (int)(IppInn-IpnInp-cst2*Ipc+cst2*Inc);
int grady = (int)(IppInn+IpnInp-cst2*Icp+cst2*Icn);
double norme = Math.sqrt( gradx*gradx + grady*grady );
double angle = 0;
if (norme>0) {
angle = Math.acos(gradx/norme);
if (grady>0) angle = 2*Math.PI - angle;
}
norme/=255;
return new double[] {norme,angle};
}
} |
Partager