Commit e0cc40c3 authored by Andrey Filippov's avatar Andrey Filippov

extracting background

parent 695f5cda
...@@ -190,7 +190,9 @@ private Panel panel1,panel2,panel3,panel4,panel5,panel5a, panel6; ...@@ -190,7 +190,9 @@ private Panel panel1,panel2,panel3,panel4,panel5,panel5a, panel6;
0.075, // b_min; 0.075, // b_min;
1.0, // r_max; 1.0, // r_max;
1.0, // g_max; 1.0, // g_max;
1.0 // b_max; 1.0, // b_max;
0.0, // alpha_min;
1.0 // alpha_max;
); );
public static FilesParameters FILE_PARAMETERS= new FilesParameters ( public static FilesParameters FILE_PARAMETERS= new FilesParameters (
...@@ -802,7 +804,8 @@ if (PROCESS_PARAMETERS.saveSettings) saveProperties(FILE_PARAMETERS.resultsDirec ...@@ -802,7 +804,8 @@ if (PROCESS_PARAMETERS.saveSettings) saveProperties(FILE_PARAMETERS.resultsDirec
imp_colorStack.getTitle()+"-RGB24", imp_colorStack.getTitle()+"-RGB24",
0, 65536, // r range 0->0, 65536->256 0, 65536, // r range 0->0, 65536->256
0, 65536, // g range 0, 65536, // g range
0, 65536);// b range 0, 65536, // b range
0, 65536);// alpha range
imp_RGB24.setTitle(imp_colorStack.getTitle()+"rgb24"); imp_RGB24.setTitle(imp_colorStack.getTitle()+"rgb24");
imp_RGB24.show(); imp_RGB24.show();
return; return;
...@@ -1177,26 +1180,30 @@ if (PROCESS_PARAMETERS.saveSettings) saveProperties(FILE_PARAMETERS.resultsDirec ...@@ -1177,26 +1180,30 @@ if (PROCESS_PARAMETERS.saveSettings) saveProperties(FILE_PARAMETERS.resultsDirec
int g_min, int g_min,
int g_max, int g_max,
int b_min, int b_min,
int b_max){ int b_max,
int [] mins= {r_min,g_min,b_min}; int alpha_min,
int [] maxs= {r_max,g_max,b_max}; int alpha_max){
int [] mins= {r_min,g_min,b_min,alpha_min};
int [] maxs= {r_max,g_max,b_max,alpha_max};
int i; int i;
int length=stack16.getWidth()*stack16.getHeight(); int length=stack16.getWidth()*stack16.getHeight();
short [][] spixels=new short[3][]; int numSlices = stack16.getSize();
if (numSlices > 4) numSlices = 4;
short [][] spixels=new short[numSlices][];
int [] pixels=new int[length]; int [] pixels=new int[length];
int c,d; int c,d;
double [] scale=new double[3]; double [] scale=new double[numSlices];
for (c=0;c<3;c++) { for (c = 0; c < numSlices; c++) {
scale[c]=256.0/(maxs[c]-mins[c]); scale[c]=256.0/(maxs[c]-mins[c]);
} }
for (i=0;i<3;i++) spixels[i]= (short[])stack16.getPixels(i+1); for (i = 0; i < numSlices; i++) spixels[i]= (short[])stack16.getPixels(i+1);
for (i=0;i<length;i++) { for (i = 0; i < length; i++) {
pixels[i]=0; pixels[i]=0;
for (c=0;c<3;c++) { for (c=0;c < numSlices;c++) {
d=(int)(((spixels[c][i]& 0xffff)-mins[c])*scale[c]); d=(int)(((spixels[c][i]& 0xffff)-mins[c])*scale[c]);
if (d>255) d=255; if (d > 255) d=255;
else if (d<0) d=0; else if (d < 0) d=0;
pixels[i]= d | (pixels[i]<<8); pixels[i]= d | (pixels[i] << 8);
} }
} }
ColorProcessor cp=new ColorProcessor(stack16.getWidth(),stack16.getHeight()); ColorProcessor cp=new ColorProcessor(stack16.getWidth(),stack16.getHeight());
...@@ -1976,7 +1983,8 @@ if (PROCESS_PARAMETERS.saveSettings) saveProperties(FILE_PARAMETERS.resultsDirec ...@@ -1976,7 +1983,8 @@ if (PROCESS_PARAMETERS.saveSettings) saveProperties(FILE_PARAMETERS.resultsDirec
title+"-RGB24", title+"-RGB24",
0, 65536, // r range 0->0, 65536->256 0, 65536, // r range 0->0, 65536->256
0, 65536, // g range 0, 65536, // g range
0, 65536);// b range 0, 65536, // b range
0, 65536);// alpha range
if (processParameters.JPEG_scale!=1.0){ if (processParameters.JPEG_scale!=1.0){
ImageProcessor ip=imp_RGB24.getProcessor(); ImageProcessor ip=imp_RGB24.getProcessor();
ip.setInterpolationMethod(ImageProcessor.BICUBIC); ip.setInterpolationMethod(ImageProcessor.BICUBIC);
...@@ -3504,6 +3512,9 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg ...@@ -3504,6 +3512,9 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg
gd.addNumericField("Red color white level", rgbParameters.r_max, 3); gd.addNumericField("Red color white level", rgbParameters.r_max, 3);
gd.addNumericField("Green color white level", rgbParameters.g_max, 3); gd.addNumericField("Green color white level", rgbParameters.g_max, 3);
gd.addNumericField("Blue color white level", rgbParameters.b_max, 3); gd.addNumericField("Blue color white level", rgbParameters.b_max, 3);
gd.addNumericField("Alpha channel min", rgbParameters.alpha_min, 3);
gd.addNumericField("Alpha channel max", rgbParameters.alpha_max, 3);
gd.showDialog(); gd.showDialog();
if (gd.wasCanceled()) return false; if (gd.wasCanceled()) return false;
rgbParameters.r_min= gd.getNextNumber(); rgbParameters.r_min= gd.getNextNumber();
...@@ -3512,6 +3523,8 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg ...@@ -3512,6 +3523,8 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg
rgbParameters.r_max= gd.getNextNumber(); rgbParameters.r_max= gd.getNextNumber();
rgbParameters.g_max= gd.getNextNumber(); rgbParameters.g_max= gd.getNextNumber();
rgbParameters.b_max= gd.getNextNumber(); rgbParameters.b_max= gd.getNextNumber();
rgbParameters.alpha_min= gd.getNextNumber();
rgbParameters.alpha_max= gd.getNextNumber();
return true; return true;
} }
...@@ -4233,13 +4246,15 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg ...@@ -4233,13 +4246,15 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg
/* ======================================================================== */ /* ======================================================================== */
public static class RGBParameters { public static class RGBParameters {
public double r_min; public double r_min = 0.075;
public double g_min; public double g_min = 0.075;
public double b_min; public double b_min = 0.075;
public double r_max; public double r_max = 1.0;
public double g_max; public double g_max = 1.0;
public double b_max; public double b_max = 1.0;
public double alpha_min = 0.0;
public double alpha_max = 1.0;
/*
public RGBParameters(double r_min, double g_min, double b_min, double r_max, double g_max, double b_max) { public RGBParameters(double r_min, double g_min, double b_min, double r_max, double g_max, double b_max) {
this.r_min = r_min; this.r_min = r_min;
this.g_min = g_min; this.g_min = g_min;
...@@ -4248,6 +4263,18 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg ...@@ -4248,6 +4263,18 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg
this.g_max = g_max; this.g_max = g_max;
this.b_max = b_max; this.b_max = b_max;
} }
*/
public RGBParameters(double r_min, double g_min, double b_min, double r_max, double g_max, double b_max, double alpha_min, double alpha_max) {
this.r_min = r_min;
this.g_min = g_min;
this.b_min = b_min;
this.r_max = r_max;
this.g_max = g_max;
this.b_max = b_max;
this.alpha_min = alpha_min;
this.alpha_max = alpha_max;
}
public void setProperties(String prefix,Properties properties){ public void setProperties(String prefix,Properties properties){
properties.setProperty(prefix+"r_min",this.r_min+""); properties.setProperty(prefix+"r_min",this.r_min+"");
properties.setProperty(prefix+"g_min",this.g_min+""); properties.setProperty(prefix+"g_min",this.g_min+"");
...@@ -4255,6 +4282,9 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg ...@@ -4255,6 +4282,9 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg
properties.setProperty(prefix+"r_max",this.r_max+""); properties.setProperty(prefix+"r_max",this.r_max+"");
properties.setProperty(prefix+"g_max",this.g_max+""); properties.setProperty(prefix+"g_max",this.g_max+"");
properties.setProperty(prefix+"b_max",this.b_max+""); properties.setProperty(prefix+"b_max",this.b_max+"");
properties.setProperty(prefix+"alpha_min",this.alpha_min+"");
properties.setProperty(prefix+"alpha_max",this.alpha_max+"");
} }
public void getProperties(String prefix,Properties properties){ public void getProperties(String prefix,Properties properties){
this.r_min=Double.parseDouble(properties.getProperty(prefix+"r_min")); this.r_min=Double.parseDouble(properties.getProperty(prefix+"r_min"));
...@@ -4263,6 +4293,8 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg ...@@ -4263,6 +4293,8 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg
this.r_max=Double.parseDouble(properties.getProperty(prefix+"r_max")); this.r_max=Double.parseDouble(properties.getProperty(prefix+"r_max"));
this.g_max=Double.parseDouble(properties.getProperty(prefix+"g_max")); this.g_max=Double.parseDouble(properties.getProperty(prefix+"g_max"));
this.b_max=Double.parseDouble(properties.getProperty(prefix+"b_max")); this.b_max=Double.parseDouble(properties.getProperty(prefix+"b_max"));
if (properties.getProperty(prefix+"alpha_min")!=null) this.alpha_min=Double.parseDouble(properties.getProperty(prefix+"alpha_min"));
if (properties.getProperty(prefix+"alpha_max")!=null) this.alpha_max=Double.parseDouble(properties.getProperty(prefix+"alpha_max"));
} }
} }
......
...@@ -1285,7 +1285,8 @@ public class EyesisCorrections { ...@@ -1285,7 +1285,8 @@ public class EyesisCorrections {
title+"-RGB24", title+"-RGB24",
0, 65536, // r range 0->0, 65536->256 0, 65536, // r range 0->0, 65536->256
0, 65536, // g range 0, 65536, // g range
0, 65536);// b range 0, 65536,// b range
0, 65536);// alpha range
if (JPEG_scale!=1.0){ if (JPEG_scale!=1.0){
ImageProcessor ip=imp_RGB.getProcessor(); ImageProcessor ip=imp_RGB.getProcessor();
ip.setInterpolationMethod(ImageProcessor.BICUBIC); ip.setInterpolationMethod(ImageProcessor.BICUBIC);
...@@ -1524,7 +1525,6 @@ public class EyesisCorrections { ...@@ -1524,7 +1525,6 @@ public class EyesisCorrections {
/* ======================================================================== */ /* ======================================================================== */
public CompositeImage convertToComposite( ImagePlus imp) { public CompositeImage convertToComposite( ImagePlus imp) {
// if (imp.isComposite()) return imp;
if (imp.isComposite()) return null; if (imp.isComposite()) return null;
if (imp.getNChannels()>1) { if (imp.getNChannels()>1) {
return null; // number of channels should be just 1 return null; // number of channels should be just 1
...@@ -1532,8 +1532,6 @@ public class EyesisCorrections { ...@@ -1532,8 +1532,6 @@ public class EyesisCorrections {
int c = imp.getStackSize(); int c = imp.getStackSize();
imp.setDimensions(c, 1, 1); imp.setDimensions(c, 1, 1);
CompositeImage ci = new CompositeImage(imp, CompositeImage.COMPOSITE); CompositeImage ci = new CompositeImage(imp, CompositeImage.COMPOSITE);
// ci.show();
// imp.hide();
return ci; return ci;
} }
...@@ -1546,19 +1544,21 @@ public class EyesisCorrections { ...@@ -1546,19 +1544,21 @@ public class EyesisCorrections {
int i,j; int i,j;
float [] fpixels; float [] fpixels;
short [] spixels; short [] spixels;
double [] mins= {rgbParameters.r_min,rgbParameters.g_min,rgbParameters.b_min}; int numSlices = stack32.getSize();
double [] maxs= {rgbParameters.r_max,rgbParameters.g_max,rgbParameters.b_max}; if (numSlices > 4) numSlices = 4;
if (stack32.getSize()<3) return null; if (numSlices < 3) return null;
double [] mins= {rgbParameters.r_min, rgbParameters.g_min, rgbParameters.b_min, rgbParameters.alpha_min};
double [] maxs= {rgbParameters.r_max, rgbParameters.g_max, rgbParameters.b_max, rgbParameters.alpha_max};
double value; double value;
double scale; double scale;
for (i=0;i<3;i++) { for (i = 0; i < numSlices; i++) {
fpixels= (float[])stack32.getPixels(i+1); fpixels= (float[])stack32.getPixels(i + 1);
scale=65535.0/(maxs[i]-mins[i]); scale=65535.0 / (maxs[i] - mins[i]);
spixels=new short [length]; spixels=new short [length];
for (j=0;j<length;j++) { for (j = 0; j < length; j++) {
value=(fpixels[j]-mins[i])*scale; value=(fpixels[j]-mins[i])*scale;
if (value<0.0) value=0.0; if (value<0.0) value = 0.0;
else if (value>65535.0) value=65535.0; else if (value>65535.0) value = 65535.0;
spixels[j]=(short)(value+0.5); spixels[j]=(short)(value+0.5);
} }
stack16.addSlice(stack32.getSliceLabel(i+1), spixels); stack16.addSlice(stack32.getSliceLabel(i+1), spixels);
...@@ -1570,37 +1570,87 @@ public class EyesisCorrections { ...@@ -1570,37 +1570,87 @@ public class EyesisCorrections {
public ImagePlus convertRGB48toRGB24( public ImagePlus convertRGB48toRGB24(
ImageStack stack16, ImageStack stack16,
String title, String title,
int r_min,
int r_max,
int g_min,
int g_max,
int b_min,
int b_max,
int alpha_min,
int alpha_max){
int [] mins= {r_min,g_min,b_min,alpha_min};
int [] maxs= {r_max,g_max,b_max,alpha_max};
int i;
int length=stack16.getWidth()*stack16.getHeight();
int numSlices = stack16.getSize();
if (numSlices > 4) numSlices = 4;
short [][] spixels=new short[numSlices][];
int [] sliceSeq = new int [numSlices];
for (int j = 0; j < numSlices; j++) sliceSeq[j] = (j + ((numSlices > 3)? 3:0)) % 4;
int [] pixels=new int[length];
int c,d;
double [] scale=new double[numSlices];
for (c = 0; c < numSlices; c++) {
scale[c]=256.0/(maxs[c]-mins[c]);
}
for (i = 0; i < numSlices; i++) spixels[i]= (short[])stack16.getPixels(i+1);
for (i = 0; i < length; i++) {
pixels[i]=0;
for (int j=0; j < numSlices; j++) {
c = sliceSeq[j];
d=(int)(((spixels[c][i]& 0xffff)-mins[c])*scale[c]);
if (d > 255) d=255;
else if (d < 0) d=0;
pixels[i]= d | (pixels[i] << 8);
}
}
ColorProcessor cp=new ColorProcessor(stack16.getWidth(),stack16.getHeight());
cp.setPixels(pixels);
ImagePlus imp=new ImagePlus(title,cp);
return imp;
}
public ImageStack convertRGB48toRGBA24Stack(
ImageStack stack16,
double [] dalpha, // alpha pixel array 0..1.0 or null
// String title,
int r_min, int r_min,
int r_max, int r_max,
int g_min, int g_min,
int g_max, int g_max,
int b_min, int b_min,
int b_max){ int b_max){
ImageStack stack8 = new ImageStack(stack16.getWidth(), stack16.getHeight());
int [] mins= {r_min,g_min,b_min}; int [] mins= {r_min,g_min,b_min};
int [] maxs= {r_max,g_max,b_max}; int [] maxs= {r_max,g_max,b_max};
int i; int i;
int length=stack16.getWidth()*stack16.getHeight(); int length=stack16.getWidth()*stack16.getHeight();
short [][] spixels=new short[3][]; short [][] spixels=new short[3][];
int [] pixels=new int[length]; byte [][] bpixels=new byte [(dalpha != null)? 4: 3][length];
int c,d; int c,d;
double [] scale=new double[3]; double [] scale=new double[3];
for (c=0;c<3;c++) { for (c=0;c<3;c++) {
scale[c]=256.0/(maxs[c]-mins[c]); scale[c]=256.0/(maxs[c]-mins[c]);
} }
for (i=0;i<3;i++) spixels[i]= (short[])stack16.getPixels(i+1); for (i = 0; i <3; i++) spixels[i]= (short[])stack16.getPixels(i+1);
for (i=0;i<length;i++) { for (c = 0; c <3; c++) {
pixels[i]=0; for (i = 0; i < length; i++){
for (c=0;c<3;c++) {
d=(int)(((spixels[c][i]& 0xffff)-mins[c])*scale[c]); d=(int)(((spixels[c][i]& 0xffff)-mins[c])*scale[c]);
if (d>255) d=255; if (d>255) d=255;
else if (d<0) d=0; else if (d<0) d=0;
pixels[i]= d | (pixels[i]<<8); bpixels[c][i]= (byte) d;
} }
stack8.addSlice(stack16.getSliceLabel(c+1), bpixels[c]);
} }
ColorProcessor cp=new ColorProcessor(stack16.getWidth(),stack16.getHeight()); if (dalpha != null) {
cp.setPixels(pixels); for (i = 0; i < length; i++){
ImagePlus imp=new ImagePlus(title,cp); bpixels[3][i] = (byte) (255*dalpha[i]);
return imp; }
stack8.addSlice("alpha", bpixels[3]);
}
return stack8;
} }
/* ======================================================================== */ /* ======================================================================== */
...@@ -2312,16 +2362,77 @@ public class EyesisCorrections { ...@@ -2312,16 +2362,77 @@ public class EyesisCorrections {
if (path!=null) { if (path!=null) {
path+=Prefs.getFileSeparator()+imp.getTitle(); path+=Prefs.getFileSeparator()+imp.getTitle();
if (((imp.getStackSize()==1)) && (jpegQuality!=0) && ((imp.getFileInfo().fileType== FileInfo.RGB) || (jpegQuality>0))) { boolean hasAlphaHighByte = false;
if ((imp.getStackSize()==1) && (imp.getFileInfo().fileType== FileInfo.RGB)) {
int [] pixels = (int []) imp.getProcessor().getPixels();
for (int i = 0; i < pixels.length; i++){
if ((pixels[i] & 0xff000000) != 0){
hasAlphaHighByte = true;
break;
}
}
}
if (hasAlphaHighByte){
if (correctionsParameters.png){
if (this.debugLevel > 0) System.out.println("Saving RGBA result to "+path+".png");
(new EyesisTiff()).savePNG_ARGB32(
imp,
path+".png"
);
} else {
if (this.debugLevel > 0) System.out.println("Saving RGBA result to "+path+".tiff");
try {
(new EyesisTiff()).saveTiffARGB32(
imp,
path+".tiff",
false, // correctionsParameters.imageJTags,
debugLevel);
} catch (IOException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
} catch (DependencyException e) {
e.printStackTrace();
}
}
} else if (((imp.getStackSize()==1)) && (jpegQuality!=0) && ((imp.getFileInfo().fileType== FileInfo.RGB) || (jpegQuality>0))) {
if (this.debugLevel>0) System.out.println("Saving result to "+path+".jpeg"); if (this.debugLevel>0) System.out.println("Saving result to "+path+".jpeg");
FileSaver fs=new FileSaver(imp); FileSaver fs=new FileSaver(imp);
if (jpegQuality>0) FileSaver.setJpegQuality(jpegQuality); if (jpegQuality>0) FileSaver.setJpegQuality(jpegQuality);
fs.saveAsJpeg(path+".jpeg"); fs.saveAsJpeg(path+".jpeg");
} } else {
else {
if (this.debugLevel>0) System.out.println("Saving result to "+path+".tiff"); if (this.debugLevel>0) System.out.println("Saving result to "+path+".tiff");
FileSaver fs=new FileSaver(imp); FileSaver fs=new FileSaver(imp);
if (imp.getStackSize()>1) fs.saveAsTiffStack(path+".tiff"); // Save as RGBA if it is RGBA
int bytesPerPixel = imp.getBytesPerPixel();
int mode = 0;
if (bytesPerPixel > 1) mode = 1;
if (bytesPerPixel > 3) mode = 3;// float, mode 2 not supported
if ((imp.getStackSize() == 4) && imp.getStack().getSliceLabel(4).equals("alpha")){
try {
(new EyesisTiff()).saveTiff(
imp,
path+".tiff",
mode, //
1.0, // full scale, absolute
false, // correctionsParameters.imageJTags,
debugLevel);
} catch (IOException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
} catch (DependencyException e) {
e.printStackTrace();
}
} else if (imp.getStackSize()>1) fs.saveAsTiffStack(path+".tiff");
else fs.saveAsTiff(path+".tiff"); else fs.saveAsTiff(path+".tiff");
} }
} }
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment