How to have a batch of images with the same brightness and contrast?

imagej
histogram-matching

#18

Hi @iarganda,

Thanks!
I actually modified it a bit to have a proper optimization function:
I now have :

(getResult("Mean ",f)-stackMean)*(getResult("Mean ",f)-stackMean)/(stackMean*stackMean))).

This is an application of the least square method, with (a-b)^2/b^2 a correct unbiased estimation of my error, that enable efficient comparison.

I actually want to know which parameter (between Mean and StD) is the more accurate when I want to select an image for histogram matching. Are you sure I can determine it experimentally? I don’t think so, because I don’t know which image is the best: I’m looking for it!

Cheers,
Baptiste


#19

Sure, but you have some intuition. For example, by visually inspecting the chosen image, you will see if the large standard deviation is due to artifacts in the image (such as bleaching, over-saturation, etc) or to real content diversity. That’s what I meant when I said “experimentally find the values”. Your script will point you to the theoretically best candidates and you will choose based on your knowledge. Does it make sense?


#20

@iarganda Yes it does make sense, thank you so much.

I just checked and it’s not very obvious to be honest (I don’t have such artifacts, and it’s difficult to estimate what content diversity is the most valuable, since I’m working on brain sections).

So, for those who face the same problem as me, I recommend putting a higher weight on the optMean (estimation of the error of each image in relation to mean), somehow 0.9 (against 0.1 for the optStd), because often, your image with the highest diversity content isn’t the best candidate (in my case). But you have to experimentally try some values.

Thank you once more @iarganda for the help and solving this problem with me, that was cool!

Best,
Baptiste


#21

Sorry @iarganda, one last question: do you know how to turn the Histogram Stack Matcher (.bsh script) into a Plugin? I’d like to integrate it to a global macro, without having to use the Script Editor.
Thank you very much,

Baptiste


#22

No need to turn a script into a plugin. As long as you put the script file somewhere into your ./scripts/ (or ./plugins/Scripts/) directory, and the filename contains an underscore (_), it will appear as a menu command (in a menu corresponding to the subfolder where you put it in), and it will be recordable by the macro recorder and callable from other scripts.

See also:


Including a Beanshell Script in a Macro (.txt)
#23

Thank you!!
I’ve been looking for this for hours …
Thank you very much.


#24

how could i creat the reference according to your method? Many thanks


#25

how could i creat the reference according to your method? Many thanks


#26

You just need to select one image of the stack as reference. You should select one of the slices with more content.


#27

Hi iarganda,

Do you know if there a youtube video on how to carryout out whole operation starting from copying the script into Fiji, loading the reference image (with final brightness & contrast) and loading all images which are to be processed. And finally the output images.
Thank you.


#29

Dear @Pavan_Nishad,

We don’t have a video of that, but I can explain in the detail what you need to do:

  1. In Fiji go to “File > New > Script …”. Then a new window will be displayed showing the Script Editor:

  2. Copy and paste there the script I wrote in my previous post:

  3. In the Script Editor, change the language to BeanShell (under “Language > BeanShell”). That will make the text get highlighted:

  4. Using the main Fiji bar, open your images as a stack (“File > Import > Image Sequence…”). I took one of my TEM datasets as an example:

  5. In the Script Editor, click on “Run”, and a the next dialog will appear:

    image

  6. There, choose the slice number to be used as reference and click “OK”. The script will be run an a new stack will show up at the end of the process with the result:

I hope this helps!


#30

Hi iarganda,

Thank you very much for putting the efforts to prepare this tutorials. Let me try and check!


#31

Hi iarganda,

I tried and it seems it worked. How do I save the newly created Histogram-matched images?


#32

Do you mean how to save the stack as individual images? If so, “File > Save As > Image Sequence…”.


#33

Yes, I mean saving processed stack as individual images. I tried your instruction in saving processed files and it showed me that it is saving the files in process bar, but in actual there are no files saved in that location. I tries saving in different location, but same problem occured. I am getting any error while saving the files, but it is just that files are not visible to me.


#34

Hi iarganda,

The files appeared now. I think it take some time to copy the file to destination folder.

Thanks alot for prompt support. You are genius.


#35

Hi iarganda,

Sorry to trouble you once again but could you please help me with one thing, I am trying to match histogram of many images with different resolution and it is giving me an error that resolution is not matching when I import the image as sequence so is there any way to bypass this match and still match the histogram?


#36

Sorry for the late answer, @Pavan_Nishad. Are you talking about image size or image resolution? If it is resolution, I would simply set the resolution to the same values on all images before applying the histogram matching, and will re-assign it later.


#37

Hi,

When I try to run your (very promising !) script, I get this :

WARNING] Auto-imports are active, but deprecated.
Sourced file: inline evaluation of: import ini.trakem2.persistence.StaleFiles; import ini.trakem2.persistence.Cache; . . . '' : illegal use of undefined variable, class, or 'void' literal : at Line: 633 : in file: inline evaluation of:import ini.trakem2.persistence.StaleFiles; import ini.trakem2.persistence.Cache; . . . ‘’ : || referenceSlice > imp .getImageStackSize ( ) ) {

at bsh.BSHBinaryExpression.eval(BSHBinaryExpression.java:193)
at bsh.BSHBinaryExpression.eval(BSHBinaryExpression.java:46)
at bsh.BSHIfStatement.evaluateCondition(BSHIfStatement.java:57)
at bsh.BSHIfStatement.eval(BSHIfStatement.java:40)
at bsh.Interpreter.eval(Interpreter.java:659)
at bsh.Interpreter.eval(Interpreter.java:750)
at bsh.Interpreter.eval(Interpreter.java:739)
at org.scijava.plugins.scripting.beanshell.BeanshellScriptEngine.eval(BeanshellScriptEngine.java:68)
at org.scijava.script.ScriptModule.run(ScriptModule.java:160)
at org.scijava.module.ModuleRunner.run(ModuleRunner.java:168)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:127)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:66)
at org.scijava.thread.DefaultThreadService$3.call(DefaultThreadService.java:238)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

I guess this is something related to the stacks importation ?
I just did File > Import > Image sequence…

Thank you for your help !

Thibault


#38

Now I realized that it’s probably not a importation problem but a language issue (I did not read yet your nice tutorial above !) : when I set on “BeanShell”, the reference slice dialog window does not appear. I tried (just to see) leaving the language set on “IJ1 Macro” and I got it, but then obviously an error like Baptiste ("< import > ij.IJ;")…

Thanks,
T.