Login  Register

Re: Histogram Matching | python script attached, questions remain

Posted by Rainer M. Engel on Oct 10, 2019; 10:19am
URL: http://imagej.273.s1.nabble.com/Histogram-Matching-python-script-attached-questions-remain-tp5022497p5022517.html

Hi Curtis,

thank you very much for your detailed answer. Also the information
provided by Giovanni helped a lot to retrieve the cfg-file in the first
place, which is very comfortable to use.

The char-error is gone and also the call from menu is now recordable
following the guideline you provided. This is very nice.. :)

Your "By the way" suggestion to use script parameters is also highly
appreciated.

Kind regards,
Rainer


Am 08.10.2019 um 20:50 schrieb Curtis Rueden:

> Hi Rainer,
>
>> console: Failed to install '':
>> java.nio.charset.UnsupportedCharsetException: cp0. What can I do about
>> this char-error?
>
> This is a bug in Jython on Windows:
> https://bugs.jython.org/issue2222
>
> See also:
> https://forum.image.sc/t/script-editor-error-when-working-with-python-scripts/5893
>
> If you launch ImageJ with:
>   -Dpython.console.encoding=UTF-8
> the error should be prevented. However, if I understand correctly, it does
> not prevent the script from running successfully.
>
>> I hoped calling it from a macro would work with ease.. but I do not
>> know how or where to put the script.
>
> Save your script somewhere like Fiji.app/scripts/Analyze/Match_Histogram.py.
> Then you will have a menu entry Analyze > Match Histogram.
> Launch the Macro Recorder, then run Match Histogram, and a macro line of
> code will be generated.
>
> See also:
> https://imagej.net/Scripting#Adding_scripts_to_the_Plugins_menu
>
>> Can someone point me to python examples of a script, which
>> differentiates between image types?
>
> You can check the type of the image using ImpImage1.getType() and compare
> the result to ImagePlus.COLOR_RGB.
>
> By the way: your script would be a lot shorter using script parameters:
>
> --snip--
> #@ ImagePlus (label="target image") ImpImage1
> #@ ImagePlus (label="reference image") ImpImage2
>
> import ij.IJ
> from histogram2 import HistogramMatcher
>
> ip1 = ImpImage1.getProcessor()
> ip2 = ImpImage2.getProcessor()
>
> hist1 = ip1.getHistogram()
> hist2 = ip2.getHistogram()
>
> matcher = HistogramMatcher()
> newHist = matcher.matchHistograms(hist1, hist2)
>
> ip1.applyTable(newHist)
> ImpImage1.setProcessor(ip1)
>
> ImpImage1.show()
> ImpImage2.show()
> --snap--
>
> For details see https://imagej.net/Script_Parameters
>
> Regards,
> Curtis
>
> --
> Curtis Rueden
> Software architect, LOCI/Eliceiri lab - https://loci.wisc.edu/software
> ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
> Have you tried the Image.sc Forum? https://forum.image.sc/
>
>
>
> On Tue, Oct 8, 2019 at 6:55 AM Rainer M. Engel <[hidden email]> wrote:
>
>> Hey everyone..
>>
>> I'm typically working with another software, where histogram matching is
>> easy available. Recently I stumbled upon an thread on stack overflow on
>> this topic with ImageJ
>> https://stackoverflow.com/questions/25085871/match-histogram-imagej
>>
>> Since I wanted to create a "script plugin" which I can call from a
>> macro, I finally chose python, because it was easier to find examples
>> helping me out.
>>
>> I used these two images..
>> "
>> https://juliahartel.files.wordpress.com/2013/07/kornfeld-in-goldener-abendsonne.jpg
>> "
>> "
>> https://naturfotografen-forum.de/data/o/142/713934/image::Frank_Steinmann_auwald_rheinaue_wald.jpg
>> "
>>
>> Hoping the indentation is preserved!
>> #PYTHON-CODE---------------------------------------------------
>> from fiji.util.gui import GenericDialogPlus
>> import ij.IJ
>> from histogram2 import HistogramMatcher
>>
>>
>> # Create an instance of GenericDialogPlus
>> gui = GenericDialogPlus("histogram matching..")
>>
>> # Add possibility to choose some images already opened in Fiji
>> gui.addImageChoice("target image","image1")
>> gui.addImageChoice("reference image","image2")
>>
>> gui.showDialog()
>>
>> # Recover the inputs in order of "appearance"
>> if gui.wasOKed():
>>     ImpImage1 = gui.getNextImage() # This method directly returns the
>> ImagePlus object
>>     ImpImage2 = gui.getNextImage()
>>
>> ip1 = ImpImage1.getProcessor()
>> ip2 = ImpImage2.getProcessor()
>>
>> hist1 = ip1.getHistogram()
>> hist2 = ip2.getHistogram()
>>
>> matcher = HistogramMatcher()
>> newHist = matcher.matchHistograms(hist1, hist2)
>>
>> ip1.applyTable(newHist)
>> ImpImage1.setProcessor(ip1)
>>
>> ImpImage1.show()
>> ImpImage2.show()
>> #PYTHON-CODE---------------------------------------------------
>>
>>
>> Questions:
>> 1) This works interactively, but on first execution I get an error:
>> [INFO] Reading available sites from https://imagej.net/
>> console: Failed to install '':
>> java.nio.charset.UnsupportedCharsetException: cp0.
>> What can I do about this char-error?
>>
>>
>> 2) I hoped calling it from a macro would work with ease.. but I do not
>> know how or where to put the script.
>>
>> run("script:histogram-match.py", "target=[image
>> Frank_Steinmann_auwald_rheinaue_wald.jpg]
>> reference=kornfeld-in-goldener-abendsonne.jpg");
>>
>> With the above, I get an file.io error.
>>
>> <Error: java.io.FileNotFoundException:
>>
>> http://wsr.imagej.net/download/Examples/Python/script:G:ImageJ_hist-matchhistogram-match.py
>>>
>> in line 1:
>>         var ; initializeSciJavaParameters ( ) ; run (
>> "script:G:ImageJ_hist-matchhistogram-match.py" , "target=[image
>> Frank_Ste...
>>
>>
>> 3) Can someone point me to python examples of a script, which
>> differentiates between image types? What the provided script needs is a
>> fork for RGB images (Split Channels, working on them separately and
>> merging them later on again). This creates very good results. One would
>> only need to tiny smooth the correction curve, to reduce artefacts
>> (especially if i.e. "kornfeld" is the target image). Currently I don't
>> know if this is possible with "histogram2".
>>
>>
>> Thank you very much, for any direction or help..
>>
>> Kind regards,
>> Rainer
>>
>> --
>> Rainer M. Engel

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html