Java Code Examples for ij.process.ImageProcessor#setColorModel()

The following examples show how to use ij.process.ImageProcessor#setColorModel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MorphologicalFilterPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Applies the specified morphological operation with specified structuring
 * element to the input image.
 * 
 * @param image
 *            the input image (grayscale or color)
 * @param op
 *            the operation to apply
 * @param strel
 *            the structuring element to use for the operation
 * @return the result of morphological operation applied to the input image
 * @deprecated use the process method instead
 */
@Deprecated
public ImagePlus exec(ImagePlus image, Operation op, Strel strel)
{
	// Check validity of parameters
	if (image == null)
		return null;
	
	// extract the input processor
	ImageProcessor inputProcessor = image.getProcessor();
	
	// apply morphological operation
	ImageProcessor resultProcessor = op.apply(inputProcessor, strel);
	
	// Keep same color model
	resultProcessor.setColorModel(inputProcessor.getColorModel());
	
	// create the new image plus from the processor
	ImagePlus resultImage = new ImagePlus(op.toString(), resultProcessor);
	resultImage.copyScale(image);
				
	// return the created array
	return resultImage;
}
 
Example 2
Source File: MorphologicalFilterPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Applies the specified morphological operation with specified structuring
 * element to the input image.
 * 
 * @param image
 *            the input image (grayscale or color)
 * @param op
 *            the operation to apply
 * @param strel
 *            the structuring element to use for the operation
 * @return the result of morphological operation applied to the input image
 */
public ImagePlus process(ImagePlus image, Operation op, Strel strel)
{
	// Check validity of parameters
	if (image == null)
		return null;
	
	// extract the input processor
	ImageProcessor inputProcessor = image.getProcessor();
	
	// apply morphological operation
	ImageProcessor resultProcessor = op.apply(inputProcessor, strel);
	
	// Keep same color model
	resultProcessor.setColorModel(inputProcessor.getColorModel());
	
	// create the new image plus from the processor
	ImagePlus resultImage = new ImagePlus(op.toString(), resultProcessor);
	resultImage.copyScale(image);
				
	// return the created array
	return resultImage;
}
 
Example 3
Source File: LUTCustom.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		Utils.log("Ignoring " + getClass().getSimpleName() + " filter for RGB image");
		return ip;
	}
	byte[] s1 = new byte[256];
	byte[] s2 = new byte[256];
	byte[] s3 = new byte[256];
	for (int i=0; i<256; ++i) {
		s1[i] = (byte)(int)(i * r);
		s2[i] = (byte)(int)(i * g);
		s3[i] = (byte)(int)(i * b);
		Utils.log2(i + ": r, g, b " + s1[i] + ", " + s2[i] + ", " + s3[i]);
	}
	ip.setColorModel(new IndexColorModel(8, 256, s1, s2, s3));
	return ip;
}
 
Example 4
Source File: SetLabelMapPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void setColorModel(ColorModel cm)
{
	ImageProcessor baseImage = imagePlus.getProcessor();
	baseImage.setColorModel(cm);
	if (imagePlus.getStackSize() > 1)
	{
		ImageStack stack = imagePlus.getStack();
		stack.setColorModel(cm);
	}
}
 
Example 5
Source File: Patch.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
/** Returns a PatchImage object containing the bottom-of-transformation-stack image and alpha mask, if any (except the AffineTransform, which is used for direct hw-accel screen rendering). */
public Patch.PatchImage createTransformedImage() {
	final Patch.PatchImage pi = createCoordinateTransformedImage();
	if (null != pi) return pi;
	// else, a new one with the untransformed, original image (a duplicate):
	final ImageProcessor ip = getImageProcessor();
	if (null == ip) return null;
	project.getLoader().releaseToFit(o_width, o_height, type, 3);
	final ImageProcessor copy = ip.duplicate();
	copy.setColorModel(ip.getColorModel()); // one would expect "duplicate" to do this but it doesn't!
	return new PatchImage(copy, getAlphaMask(), null, new Rectangle(0, 0, o_width, o_height), false);
}
 
Example 6
Source File: LUTMagenta.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		Utils.log("Ignoring " + getClass().getSimpleName() + " filter for RGB image");
		return ip;
	}
	byte[] s = new byte[256];
	for (int i=0; i<256; ++i) s[i] = (byte)i;
	ip.setColorModel(new IndexColorModel(8, 256, s, new byte[256], s));
	return ip;
}
 
Example 7
Source File: LUTYellow.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		Utils.log("Ignoring " + getClass().getSimpleName() + " filter for RGB image");
		return ip;
	}
	byte[] s = new byte[256];
	for (int i=0; i<256; ++i) s[i] = (byte)i;
	ip.setColorModel(new IndexColorModel(8, 256, s, s, new byte[256]));
	return ip;
}
 
Example 8
Source File: LUTGreen.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		Utils.log("Ignoring " + getClass().getSimpleName() + " filter for RGB image");
		return ip;
	}
	byte[] s = new byte[256];
	for (int i=0; i<256; ++i) s[i] = (byte)i;
	ip.setColorModel(new IndexColorModel(8, 256, new byte[256], s, new byte[256]));
	return ip;
}
 
Example 9
Source File: LUTRed.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		Utils.log("Ignoring " + getClass().getSimpleName() + " filter for RGB image");
		return ip;
	}
	byte[] s = new byte[256];
	for (int i=0; i<256; ++i) s[i] = (byte)i;
	ip.setColorModel(new IndexColorModel(8, 256, s, new byte[256], new byte[256]));
	return ip;
}
 
Example 10
Source File: LUTBlue.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		Utils.log("Ignoring " + getClass().getSimpleName() + " filter for RGB image");
		return ip;
	}
	byte[] s = new byte[256];
	for (int i=0; i<256; ++i) s[i] = (byte)i;
	ip.setColorModel(new IndexColorModel(8, 256, new byte[256], new byte[256], s));
	return ip;
}
 
Example 11
Source File: LUTCyan.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		Utils.log("Ignoring " + getClass().getSimpleName() + " filter for RGB image");
		return ip;
	}
	byte[] s = new byte[256];
	for (int i=0; i<256; ++i) s[i] = (byte)i;
	ip.setColorModel(new IndexColorModel(8, 256, new byte[256], s, s));
	return ip;
}
 
Example 12
Source File: ThresholderOrbit.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public void applyThreshold(ImagePlus imp) {
	imp.deleteRoi();
	ImageProcessor ip = imp.getProcessor();
	ip.resetBinaryThreshold();
	int type = imp.getType();
	if (type==ImagePlus.GRAY16 || type==ImagePlus.GRAY32) {
		applyShortOrFloatThreshold(imp);
		return;
	}
	if (!imp.lock()) return;
	double saveMinThreshold = ip.getMinThreshold();
	double saveMaxThreshold = ip.getMaxThreshold();
	autoThreshold = saveMinThreshold==ImageProcessor.NO_THRESHOLD;

	boolean useBlackAndWhite = true;
	boolean noArgMacro =IJ.macroRunning() && Macro.getOptions()==null;
	if (skipDialog)
		fill1 = fill2 = useBlackAndWhite = true;
	else if (!(autoThreshold||noArgMacro)) {
		GenericDialog gd = new GenericDialog("Make Binary");
		gd.addCheckbox("Thresholded pixels to foreground color", fill1);
		gd.addCheckbox("Remaining pixels to background color", fill2);
		gd.addMessage("");
		gd.addCheckbox("Black foreground, white background", useBW);
		gd.showDialog();
		if (gd.wasCanceled())
		{imp.unlock(); return;}
		fill1 = gd.getNextBoolean();
		fill2 = gd.getNextBoolean();
		useBW = useBlackAndWhite = gd.getNextBoolean();
	} else {
		fill1 = fill2 = true;
		convertToMask = true;
	}

	if (type!=ImagePlus.GRAY8)
		convertToByte(imp);
	ip = imp.getProcessor();

	if (autoThreshold)
		autoThreshold(ip);
	else {
		if (Recorder.record && !Recorder.scriptMode() && (!IJ.isMacro()||Recorder.recordInMacros))
			Recorder.record("setThreshold", (int)saveMinThreshold, (int)saveMaxThreshold);
		minThreshold = saveMinThreshold;
		maxThreshold = saveMaxThreshold;
	}

	if (convertToMask && ip.isColorLut())
		ip.setColorModel(ip.getDefaultColorModel());
	int fcolor, bcolor;
	ip.resetThreshold();
	int savePixel = ip.getPixel(0,0);
	if (useBlackAndWhite)
		ip.setColor(Color.black);
	else
		ip.setColor(Toolbar.getForegroundColor());
	ip.drawPixel(0,0);
	fcolor = ip.getPixel(0,0);
	if (useBlackAndWhite)
		ip.setColor(Color.white);
	else
		ip.setColor(Toolbar.getBackgroundColor());
	ip.drawPixel(0,0);
	bcolor = ip.getPixel(0,0);
	ip.setColor(Toolbar.getForegroundColor());
	ip.putPixel(0,0,savePixel);

	int[] lut = new int[256];
	for (int i=0; i<256; i++) {
		if (i>=minThreshold && i<=maxThreshold)
			lut[i] = fill1?fcolor:(byte)i;
		else {
			lut[i] = fill2?bcolor:(byte)i;
		}
	}
	if (imp.getStackSize()>1)
		new StackProcessor(imp.getStack(), ip).applyTable(lut);
	else
		ip.applyTable(lut);
	if (convertToMask) {
		if (!imp.isInvertedLut()) {
			setInvertedLut(imp);
			fcolor = 255 - fcolor;
			bcolor = 255 - bcolor;
		}
		if (Prefs.blackBackground)
			ip.invertLut();
	}
	if (fill1 && fill2 && ((fcolor==0&&bcolor==255)||(fcolor==255&&bcolor==0)))
		imp.getProcessor().setThreshold(fcolor, fcolor, ImageProcessor.NO_LUT_UPDATE);
	imp.updateAndRepaintWindow();
	imp.unlock();
}
 
Example 13
Source File: ShollAnalysisDialog.java    From SNT with GNU General Public License v3.0 4 votes vote down vote up
public ImagePlus makeShollCrossingsImagePlus(final ImagePlus original) {
	final int width = original.getWidth();
	final int height = original.getHeight();
	final int depth = original.getStackSize();
	final Calibration c = original.getCalibration();
	double x_spacing = 1;
	double y_spacing = 1;
	double z_spacing = 1;
	if (c != null) {
		x_spacing = c.pixelWidth;
		y_spacing = c.pixelHeight;
		z_spacing = c.pixelDepth;
	}
	final ImageStack stack = new ImageStack(width, height);
	for (int z = 0; z < depth; ++z) {
		final short[] pixels = new short[width * height];
		for (int y = 0; y < height; ++y) {
			for (int x = 0; x < width; ++x) {
				final double xdiff = x_spacing * x - x_start;
				final double ydiff = y_spacing * y - y_start;
				final double zdiff = z_spacing * z - z_start;
				final double distanceSquared = xdiff * xdiff + ydiff * ydiff + zdiff * zdiff;
				pixels[y * width + x] = (short) crossingsAtDistanceSquared(distanceSquared);
			}
		}
		final ShortProcessor sp = new ShortProcessor(width, height);
		sp.setPixels(pixels);
		stack.addSlice("", sp);
	}
	final ImagePlus result = new ImagePlus(description, stack);
	result.show();
	final IndexColorModel icm = FindConnectedRegions.backgroundAndSpectrum(255);
	stack.setColorModel(icm);
	final ImageProcessor ip = result.getProcessor();
	if (ip != null) {
		ip.setColorModel(icm);
		ip.setMinAndMax(0, maxCrossings);
	}
	result.updateAndDraw();

	if (c != null)
		result.setCalibration(c);
	return result;
}
 
Example 14
Source File: ImposeMinAndMaxPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void run(String arg) {
	// Open a dialog to choose:
	// - mask image
	// - marker image
	int[] indices = WindowManager.getIDList();
	if (indices == null) {
		IJ.error("No image", "Need at least one image to work");
		return;
	}

	// create the list of image names
	String[] imageNames = new String[indices.length];
	for (int i = 0; i < indices.length; i++) {
		imageNames[i] = WindowManager.getImage(indices[i]).getTitle();
	}
	
	// create the dialog
	GenericDialog gd = new GenericDialog("Impose Min & Max");
	
	gd.addChoice("Original Image", imageNames, IJ.getImage().getTitle());
	gd.addChoice("Marker Image", imageNames, IJ.getImage().getTitle());
	gd.addChoice("Operation", 
			Operation.getAllLabels(), 
			Operation.IMPOSE_MINIMA.label);
	gd.addChoice("Connectivity", connectivityLabels, connectivityLabels[0]);
	gd.showDialog();
	
	if (gd.wasCanceled())
		return;

	// set up current parameters
	int refImageIndex = gd.getNextChoiceIndex();
	ImagePlus refImage = WindowManager.getImage(refImageIndex + 1);
	int markerImageIndex = gd.getNextChoiceIndex();
	ImagePlus markerImage = WindowManager.getImage(markerImageIndex + 1);
	Operation op = Operation.fromLabel(gd.getNextChoice());
	int conn = connectivityValues[gd.getNextChoiceIndex()];
	
	// Extract image procesors
	ImageProcessor refProc = refImage.getProcessor();
	ImageProcessor markerProc = markerImage.getProcessor();
	
	long t0 = System.currentTimeMillis();
	
	// Compute geodesic reconstruction
	ImageProcessor recProc = op.applyTo(refProc, markerProc, conn);
	
	// Keep same color model as 
	recProc.setColorModel(refProc.getColorModel());

	// create resulting image
	String newName = createResultImageName(refImage);
	ImagePlus resultImage = new ImagePlus(newName, recProc);
	resultImage.copyScale(markerImage);
	resultImage.show();
	
	long t1 = System.currentTimeMillis();
	IJ.showStatus("Elapsed time: " + (t1 - t0) / 1000. + "s");
	IJUtils.showElapsedTime(op.toString(), t1 - t0, refImage);
}
 
Example 15
Source File: MorphologicalReconstructionPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void run(String arg) 
{
	// Open a dialog to choose:
	// - mask image
	// - marker image
	int[] indices = WindowManager.getIDList();
	if (indices == null) 
	{
		IJ.error("No image", "Need at least one image to work");
		return;
	}

	// create the list of image names
	String[] imageNames = new String[indices.length];
	for (int i = 0; i < indices.length; i++) 
	{
		imageNames[i] = WindowManager.getImage(indices[i]).getTitle();
	}
	
	// create the dialog
	GenericDialog gd = new GenericDialog("Morphological Reconstruction");
	
	gd.addChoice("Marker Image", imageNames, IJ.getImage().getTitle());
	gd.addChoice("Mask Image", imageNames, IJ.getImage().getTitle());
	gd.addChoice("Type of Reconstruction", 
			Operation.getAllLabels(),
			Operation.BY_DILATION.label);
	gd.addChoice("Connectivity", 
			Conn2D.getAllLabels(), 
			Conn2D.C4.label);
	gd.showDialog();
	
	if (gd.wasCanceled())
		return;

	// set up current parameters
	int markerImageIndex = gd.getNextChoiceIndex();
	ImagePlus markerImage = WindowManager.getImage(markerImageIndex + 1);
	int maskImageIndex = gd.getNextChoiceIndex();
	ImagePlus maskImage = WindowManager.getImage(maskImageIndex + 1);
	Operation op = Operation.fromLabel(gd.getNextChoice());
	int conn = Conn2D.fromLabel(gd.getNextChoice()).getValue();
	
	// Extract image procesors
	ImageProcessor markerProc = markerImage.getProcessor();
	ImageProcessor maskProc = maskImage.getProcessor();
	
	long t0 = System.currentTimeMillis();
	
	// Compute geodesic reconstruction
	ImageProcessor recProc = op.applyTo(markerProc, maskProc, conn);
	
	// Keep same color model
	recProc.setColorModel(maskProc.getColorModel());

	// create resulting image
	String newName = createResultImageName(markerImage);
	ImagePlus resultImage = new ImagePlus(newName, recProc);
	resultImage.copyScale(maskImage);
	resultImage.show();
	
	long t1 = System.currentTimeMillis();
	IJUtils.showElapsedTime(op.toString(), t1 - t0, markerImage);
}