Histogram Matching | python script attached, questions remain

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Histogram Matching | python script attached, questions remain

Rainer M. Engel
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
Reply | Threaded
Open this post in threaded view
|

Re: ***SPAM*** [MSX] Histogram Matching | python script attached, questions remain

Cardone, Giovanni
Hi Rainer,

orobably I can help with issue #1.
The error reported is not harmful, just annoying. The way I got rid of it was to modify the content of the file ImageJ.cfg
This file is located inside the Fiji.app directory. If you don't see it, go to the menu Edit/Options/Memory&Threads, change the memory size and press Ok. The file should show up.
Open the file with a text editor and add to the line of parameters the following
-Dpython.console.encoding=UTF-8

Just to give an idea, in my case the content of the file looks like
=====================================================
.
jre\bin\javaw.exe
-Dpython.console.encoding=UTF-8 -Xmx26000m -cp ij.jar ij.ImageJ
=====================================================

I hope that helps,
Giovanni


-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Rainer M. Engel
Sent: Dienstag, 8. Oktober 2019 13:40
To: [hidden email]
Subject: ***SPAM*** [MSX] Histogram Matching | python script attached, questions remain

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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Histogram Matching | python script attached, questions remain

Curtis Rueden-2
In reply to this post by Rainer M. Engel
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
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Histogram Matching | python script attached, questions remain

Rainer M. Engel
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