2

Given two ImagePlus or BufferedImages (I don't care) how can I match the histogram of the first to the second one?

By matching I mean:

Matching the cumulative distribution function (CDF) of one image to the CDF of the other.

DeMarco
  • 599
  • 1
  • 8
  • 26

1 Answers1

3

You can use the HistogramMatcher class included in Fiji (in its sub-project CorrectBleach).

Here is an example Beanshell script (you can run it via the Script Editor in Fiji):

import ij.IJ;
import histogram2.HistogramMatcher;

// get first image
imp1 = IJ.openImage("http://imagej.nih.gov/ij/images/bridge.gif");
// get second image
imp2 = IJ.openImage("http://imagej.nih.gov/ij/images/boats.gif");

ip1 = imp1.getProcessor();
ip2 = imp2.getProcessor();

hist1 = ip1.getHistogram();
hist2 = ip2.getHistogram();

matcher = new HistogramMatcher();
newHist = matcher.matchHistograms(hist1, hist2);

ip1.applyTable(newHist);
imp1.setProcessor(ip1);

imp1.show();
imp2.show();

// show the histograms of both images
IJ.run(imp1, "Histogram", "");
IJ.run(imp2, "Histogram", "");
Jan Eglinger
  • 3,995
  • 1
  • 25
  • 51
  • and what about do the matching in IJ macro? Thx – Jirka Mar 07 '19 at 15:41
  • 2
    You can't access the Java API of `HistogramMatcher` from IJ1 Macro language. But you can save the above script into `./Fiji.app/scripts/MyScripts/Match_Histograms.bsh` and then call from macro: `run("Match Histograms");`. For all further questions, please ask on the forum: https://forum.image.sc :) – Jan Eglinger Mar 07 '19 at 17:10
  • 1
    ok, I do not see any similar topic there so shall I create new and refer to this discussion? Basically, I would like to further know if I can save the script in the path and how to pass parameters. – Jirka Mar 08 '19 at 09:10
  • 1
    As I understand this script applies the match to the first image only. Thank you for this answer!! it opened for me a new word of imageJ/Fiji – Kostanos Mar 15 '19 at 06:17