Commit e0cc40c3 authored by Andrey Filippov's avatar Andrey Filippov
Browse files

extracting background

parent 695f5cda
Loading
Loading
Loading
Loading
+54 −22
Original line number Diff line number Diff line
@@ -190,7 +190,9 @@ private Panel panel1,panel2,panel3,panel4,panel5,panel5a, panel6;
		0.075, //  b_min;
		1.0, //  r_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 (
@@ -802,7 +804,8 @@ if (PROCESS_PARAMETERS.saveSettings) saveProperties(FILE_PARAMETERS.resultsDirec
   	    		  imp_colorStack.getTitle()+"-RGB24",
	    		  0, 65536, // r range 0->0, 65536->256
	    		  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.show();
        return;
@@ -1177,22 +1180,26 @@ if (PROCESS_PARAMETERS.saveSettings) saveProperties(FILE_PARAMETERS.resultsDirec
		  int g_min,
		  int g_max,
		  int b_min,
		  int b_max){
	  int [] mins= {r_min,g_min,b_min};
	  int [] maxs= {r_max,g_max,b_max};
		  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();
	  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 c,d;
	  double [] scale=new double[3];
	  for (c=0;c<3;c++) {
	  double [] scale=new double[numSlices];
	  for (c = 0; c < numSlices; 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++) {
		  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]);
			  if (d > 255) d=255;
			  else if (d < 0) d=0;
@@ -1976,7 +1983,8 @@ if (PROCESS_PARAMETERS.saveSettings) saveProperties(FILE_PARAMETERS.resultsDirec
						  title+"-RGB24",
						  0, 65536, // r range 0->0, 65536->256
						  0, 65536, // g range
						  0, 65536);// b range
			    		  0, 65536, // b range
			    		  0, 65536);// alpha range
				  if (processParameters.JPEG_scale!=1.0){
					  ImageProcessor ip=imp_RGB24.getProcessor();
					  ip.setInterpolationMethod(ImageProcessor.BICUBIC);
@@ -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("Green color white level",             rgbParameters.g_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();
    if (gd.wasCanceled()) return false;
    rgbParameters.r_min=                gd.getNextNumber();
@@ -3512,6 +3523,8 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg
    rgbParameters.r_max=                gd.getNextNumber();
    rgbParameters.g_max=                gd.getNextNumber();
    rgbParameters.b_max=                gd.getNextNumber();
    rgbParameters.alpha_min=            gd.getNextNumber();
    rgbParameters.alpha_max=            gd.getNextNumber();
    return true;
  }

@@ -4233,13 +4246,15 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg

/* ======================================================================== */
  public static class RGBParameters {
		public double r_min;
		public double g_min;
		public double b_min;
		public double r_max;
		public double g_max;
		public double b_max;

		public double r_min = 0.075;
		public double g_min = 0.075;
		public double b_min = 0.075;
		public double r_max = 1.0;
		public double g_max = 1.0;
		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) {
			this.r_min = r_min;
			this.g_min = g_min;
@@ -4248,6 +4263,18 @@ G= Y +Pr*(- 2*Kr*(1-Kr))/Kg + Pb*(-2*Kb*(1-Kb))/Kg
			this.g_max = g_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){
			properties.setProperty(prefix+"r_min",this.r_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
			properties.setProperty(prefix+"r_max",this.r_max+"");
			properties.setProperty(prefix+"g_max",this.g_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){
			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
			this.r_max=Double.parseDouble(properties.getProperty(prefix+"r_max"));
			this.g_max=Double.parseDouble(properties.getProperty(prefix+"g_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"));
		}
		
	}
+133 −13

File changed.

Preview size limit exceeded, changes collapsed.

+176 −65
Original line number Diff line number Diff line
@@ -1285,7 +1285,8 @@ public class EyesisCorrections {
					  title+"-RGB24",
					  0, 65536, // r range 0->0, 65536->256
					  0, 65536, // g range
					  0, 65536);// b range
					  0, 65536,// b range
					  0, 65536);// alpha range
			  if (JPEG_scale!=1.0){
				  ImageProcessor ip=imp_RGB.getProcessor();
				  ip.setInterpolationMethod(ImageProcessor.BICUBIC);
@@ -1524,7 +1525,6 @@ public class EyesisCorrections {

	/* ======================================================================== */
	  public CompositeImage convertToComposite( ImagePlus imp) {
//		  if (imp.isComposite()) return imp;
		  if (imp.isComposite()) return null;
		  if (imp.getNChannels()>1) {
			  return null; // number of channels should be just 1
@@ -1532,8 +1532,6 @@ public class EyesisCorrections {
		  int c = imp.getStackSize();
		  imp.setDimensions(c, 1, 1);
		  CompositeImage ci = new CompositeImage(imp, CompositeImage.COMPOSITE);
//		  ci.show();
//		  imp.hide();
		  return ci;
	  }

@@ -1546,12 +1544,14 @@ public class EyesisCorrections {
		  int i,j;
		  float [] fpixels;
		  short [] spixels;
		  double [] mins= {rgbParameters.r_min,rgbParameters.g_min,rgbParameters.b_min};
		  double [] maxs= {rgbParameters.r_max,rgbParameters.g_max,rgbParameters.b_max};
		  if (stack32.getSize()<3) return null;
		  int numSlices  = stack32.getSize();
		  if (numSlices > 4) numSlices = 4;
		  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 scale;
		  for (i=0;i<3;i++) {
		  for (i = 0; i < numSlices; i++) {
			 fpixels= (float[])stack32.getPixels(i + 1);
			 scale=65535.0 / (maxs[i] - mins[i]);
			 spixels=new short [length];
@@ -1575,22 +1575,31 @@ public class EyesisCorrections {
			  int g_min,
			  int g_max,
			  int b_min,
			  int b_max){
		  int [] mins= {r_min,g_min,b_min};
		  int [] maxs= {r_max,g_max,b_max};
			  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();
		  short [][] spixels=new short[3][];
		  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[3];
		  for (c=0;c<3;c++) {
		  
		  double [] scale=new double[numSlices];
		  for (c = 0; c < numSlices; 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++) {
			  pixels[i]=0;
			  for (c=0;c<3;c++) {
			  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;
@@ -1603,6 +1612,47 @@ public class EyesisCorrections {
		  return imp;
	  }

	  public ImageStack convertRGB48toRGBA24Stack(
			  ImageStack stack16,
			  double [] dalpha, // alpha pixel array 0..1.0 or null 
//			  String title,
			  int r_min,
			  int r_max,
			  int g_min,
			  int g_max,
			  int b_min,
			  int b_max){
		  ImageStack stack8 = new ImageStack(stack16.getWidth(), stack16.getHeight());
		  int [] mins= {r_min,g_min,b_min};
		  int [] maxs= {r_max,g_max,b_max};
	      int i;
		  int length=stack16.getWidth()*stack16.getHeight();
		  short [][] spixels=new short[3][];
		  byte [][] bpixels=new byte [(dalpha != null)? 4: 3][length];
		  int c,d;
		  double [] scale=new double[3];
		  for (c=0;c<3;c++) {
			  scale[c]=256.0/(maxs[c]-mins[c]);
		  }
		  for (i = 0; i <3; i++) spixels[i]= (short[])stack16.getPixels(i+1);
		  for (c = 0; c <3; c++) {
			  for (i = 0; i < length; i++){
				  d=(int)(((spixels[c][i]& 0xffff)-mins[c])*scale[c]);
				  if (d>255) d=255;
				  else if (d<0) d=0;
				  bpixels[c][i]= (byte) d;
			  }
			  stack8.addSlice(stack16.getSliceLabel(c+1), bpixels[c]);
		  }
		  if (dalpha != null) {
			  for (i = 0; i < length; i++){
				  bpixels[3][i] = (byte) (255*dalpha[i]);
			  }
			  stack8.addSlice("alpha", bpixels[3]);
		  }
		  return stack8;
	  }
	  
	/* ======================================================================== */
	  public ImagePlus Image32toGreyRGB24(
			  ImagePlus imp){
@@ -2312,16 +2362,77 @@ public class EyesisCorrections {

		   if (path!=null) {
			   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");
				   FileSaver fs=new FileSaver(imp);
				   if (jpegQuality>0) FileSaver.setJpegQuality(jpegQuality);
				   fs.saveAsJpeg(path+".jpeg");
	 		  }
	 		  else {
			   } else {
				   if (this.debugLevel>0) System.out.println("Saving result to "+path+".tiff");
				   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");
			   }
		   }
+1445 −351

File changed.

Preview size limit exceeded, changes collapsed.

+304 −247
Original line number Diff line number Diff line
@@ -26,12 +26,19 @@
**
*/
import java.awt.Frame;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferInt;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.util.Arrays;

import javax.imageio.ImageIO;

//import org.apache.log4j.Logger;

import ij.IJ;
@@ -56,6 +63,33 @@ public EyesisTiff(){


	}
	public void savePNG_ARGB32(
			ImagePlus imp,
			String path
			){
		int width = imp.getWidth();
		int height = imp.getHeight();
		int [] pixels = (int []) imp.getProcessor().getPixels();
		System.out.println("width="+width+", height="+height+" length="+pixels.length);
		
		DataBufferInt buffer = new DataBufferInt(pixels, pixels.length);

		int[] bandMasks = {0xFF0000, 0xFF00, 0xFF, 0xFF000000}; // ARGB (yes, ARGB, as the masks are R, G, B, A always) order
		WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, bandMasks, null);
		
		ColorModel cm = ColorModel.getRGBdefault();
		BufferedImage bimage = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
		try{
			(new File(path)).delete(); // Otherwise TiffSaver appends! - is it the same for PNG?
			ImageIO.write(bimage, "PNG", (new File(path)));
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}



	public void saveTiff(
			ImagePlus imp,
			String path,
@@ -64,8 +98,8 @@ public EyesisTiff(){
			boolean imageJTags,
			int debugLevel
			) throws IOException, FormatException, ServiceException, DependencyException{
		if (imp.getType()==ImagePlus.COLOR_RGB) {
			if (debugLevel>1) System.out.println("Saving 8-bit TIFF with alpha-channel: "+path);
		if (imp.getType()==ImagePlus.COLOR_RGB) { // 4*8 bit - type = 0
			if (debugLevel>1) System.out.println("Saving 8-bit TIFF with alpha-channel: "+path); // is it possible to just add alpha to high bytes?
			saveTiffARGB32(imp, path, imageJTags, debugLevel);
			return;
		} else if (imp.getStackSize()==4) {
@@ -96,7 +130,30 @@ public EyesisTiff(){
		String description=(imp.getProperty("description")!=null)?((String) imp.getProperty("description")):"Elphel Eyesis4pi";
		//		int [] iPixels= (int []) imp.getProcessor().getPixels();
		final float [][] imagePixels= new float [4][];
		for (int c=0;c<imagePixels.length;c++) imagePixels[c]=	(float []) imp.getStack().getPixels(c+1); // bytes are little endian
		try {
			for (int c=0;c<imagePixels.length;c++){
				imagePixels[c]=	(float []) imp.getStack().getPixels(c+1); // bytes are little endian
			}
		} catch (ClassCastException e) {
			try {
				for (int c=0;c<imagePixels.length;c++) {
					byte [] bpixels = (byte []) imp.getStack().getPixels(c+1); // bytes are little endian
					imagePixels[c] = new float[bpixels.length];
					for (int i = 0; i <bpixels.length; i++)
						imagePixels[c][i]=	((float) bpixels[i])/255;
				}
			} catch (ClassCastException e1) {
				for (int c=0;c<imagePixels.length;c++) {
					short [] spixels = (short []) imp.getStack().getPixels(c+1); // bytes are little endian
					imagePixels[c] = new float[spixels.length];
					for (int i = 0; i < spixels.length; i++)
						imagePixels[c][i]=	((float) spixels[i])/65535;
				}
			}
		}
		//		for (int i = 0; i < imagePixels[3].length; i++){
		//			imagePixels[3][i] = 1.0f - imagePixels[3][i]; 
		//		}
		int bw;
		byte [] bytes;
		int pIndex=0;
Loading