Error in ij.plugin.ContrastEnhancer.stretchHistogram(ips,10)

I want to enhance the contrast for a image using ImageJ by code(Matlab):

ips = IJ.getImage;
ij.plugin.ContrastEnhancer.stretchHistogram(ips,10)

Error:
No method 'stretchHistogram' with matching signature found for class 'ij.plugin.ContrastEnhancer'.

However, I have checked the source code that there is a re-loaded version of stretchHistogram using imageplus and double as argument.

	public void stretchHistogram(ImagePlus imp, double saturated) {
		ImageStatistics stats = null;
		if (useStackHistogram)
			stats = new StackStatistics(imp);
		if (processStack) {
			ImageStack stack = imp.getStack();
			for (int i=1; i<=stackSize; i++) {
				IJ.showProgress(i, stackSize);
				ImageProcessor ip = stack.getProcessor(i);
				ip.setRoi(imp.getRoi());
				if (!useStackHistogram)
					stats = ImageStatistics.getStatistics(ip, MIN_MAX, null);
				stretchHistogram(ip, saturated, stats);
			}
		} else {
			ImageProcessor ip = imp.getProcessor();
			ip.setRoi(imp.getRoi());
			if (stats==null)
				stats = ImageStatistics.getStatistics(ip, MIN_MAX, null);
			if (imp.isComposite())
				stretchCompositeImageHistogram((CompositeImage)imp, saturated, stats);
			else
				stretchHistogram(ip, saturated, stats);
		}
	}

I have also tried:

version 2:
IJ.run('Enhance Contrast...', 'saturated=10 update')
this version works with no problem. But to work, the image must be shown in GUI.

version 3:
IJ.runPlugIn(ips,'ij.plugin.ContrastEnhancer','')
This version seems to work without shows the imageplus. However, the dialogue shows up to get the parameters(saturated pixles percantage and whether qeuilize histogram) . How can I pass the
two argument so that it will not show the dialogue?

Thanks.

Haven’t tested this, but I’d try

ips = IJ.getImage();
ij.plugin.ContrastEnhancer.stretchHistogram(ips,10.0)

getImage() is a method so you should use the parentheses.

stretchHistogram requires a double, not an int.

Probably works either way, but just in case there’s an overloaded method that takes an int then I guess you may as well be sure.

Dear Peter,
I’m using Matlab code, in Matlab, call function no need (), so the ips = IJ.getImage(); and ips = IJ.getImage; produce the same thing.

Also, in Matlab, explicit number is always treated as double, no matter 10 or 10.1.

I have tried 10.1, or java.lang.Double(10.1), the error persist.

I notice that in class ContrastEnhancer, the argument is decleared as:

static double saturated = 0.35;

Is the reason that the " ststic " here make" saturated "a constant so it cannot be transferred?

Ok. It has been a long time since I used MATLAB… I missed that bit and saw only the QuPath tag.

The method isn’t static so you need an instance of ContrastEnhancer.

May also be able to add ips as the first argument to your approach using method 2.

Wow, it worked with an instance of ContrastEnhancer:

    aEnhancer = ij.plugin.ContrastEnhancer;
    aEnhancer.stretchHistogram(ips,10.0);
    ips.draw;

I notice that all the properties/options in the constructor is also not public,

	boolean classicEqualization;
	boolean updateSelectionOnly;
	boolean equalize, normalize, processStack, useStackHistogram, entireImage;
	static boolean gEqualize, gNormalize;

so I cannot set these properties via:

aEnhancer.equalize = false
%error:`No public field equalize exists for class ij.plugin.ContrastEnhancer.

The plugin entry method run() calls a showDialog(imp) method to set all the options (varies based on imp image type).

In this case, I directly call the basal function
aEnhancer.stretchHistogram
and unable to configuring these boolean options,
were they configured as java default boolean?univeral True or False?

If I really want to set the options via
aEnhancer.equalize = false
do I need to make the properties public, and then re-compile the ij.jar?

That’s a familiar problem when trying to work with many ImageJ1 plugins… if MATLAB permits you might achieve what you want with Java reflection, but both that and recompiling the jar are very unpleasant-sounding workarounds (Groovy just lets you access private fields… for better or worse).

Did you try my second suggestion?

IJ.run(ips, 'Enhance Contrast...', 'saturated=10 update')

Yes, I tried and it worked.
This is the best way by now to call this method.