Jump to content
UltravioletPhotography

Scripted white balance for Nikon


lukaszgryglicki

Recommended Posts

lukaszgryglicki

Hi, I've read white balance topics (globally approved on this forum), I want to achieve a specific goal that's why I'm asking.

- I can't use any photo or PTFE/grey concrete as a white balance source for Nikon (also known on this forum).

- I know that I can shoot with whatever setting because NEF files always contain raw data, and white balance used by camera is just a hint, we can always get 16bit data for each pixel, even before Bayer algorithm (so each pixel will have 0-65535 value for each RGGB channel).

 

1) How can I white-balance Nikon NEF file to match standard colors used here - there must be some set of approximate factors for each R, G, B channels to apply to a RAW file right?

2) I can always just auto-white balance a given shot, so its average color is gray (for all pixels) - I have scripts that do this (I'm a programmer).

 

All I want is a script that would white balance NEF file given as a command-line argument.

I have dcraw, ImageMagick, gimp programs installed - need to build a script that would auto balance to widely accepted color forums.

 

Currently I'm using my own script that "auto-white balances" (averaging NEF to gray), I don't think this is correct, I believe I need to just apply a custom R, G, B factors just after Bayer algorithm is applied on the RAW data (speaking about my non-debayered camera, monochrome one is simple - I just get every pixel before Bayer algorithm and save - that also make resolution up to 2x higher - in practice just a bit better).

 

What I do now for bayered NEF's is:

"dcraw -H 1 -a -q 3 -T filename.NEF"

- "-H 1" - do not clip light

- "-a" - average white balance

- "-q 3" - inerpolation detail [0-3]

- "-T " save as TIFF instead of PPM

 

If I have a white balance "source" file I can also do:

- color=`convert white-balance-source.png -resize 1x1! -modulate 100,100,0 -format "%[pixel:u.p{0,0}]" info:`
- echo "White balance color $color"
- convert filename.png -colorspace sRGB \( -clone 0 -fill "$color" -colorize 50% \) -compose colorize -composite -colorspace sRGB -quality 98% output.jpeg

 

For mono, I can save total raw data with each pixel RGGB raw 16bit signal via:

- "dcraw -4 -o 0 -D -t 0 -k 0 -H 1 -T filename.NEF":

- "- 4" - linear 16bit grey

- "-o 0" [0-6] possible, 0=raw

- "-D" - document mode, no scale, total raw

- "-t 0" - rotate=0 (no rotate)

- "-k 0" - black cut level 0

- "-H 1" - highlight clip mode 1=no clip

- "-T " save as TIFF instead of PPM

 

Again to sum up : I need a script to auto balance Nikon NEF file and save as PNG/JPG whatever (file from full spectrum camera, but with Bayer RGGB matrix present, assuming it registers only UV <= 400nm, no IR leaks) - probably maybe just R, G, B const factors?

 

Link to comment

I don’t think auto white balance scripts are going to make you happy if you want nice colors although they may get you in the ballpark. Why don’t you use one of the several white balance capable programs we have found work, like PhotoNinja or others? (Not Photoshop.) you just shoot a PTFE photo at the start of the shoot and copy the white balance settings to all your other photos in PhotoNinja, which can batch process them. We have many Nikon users on this forum, which actually started on the Nikon forums!

 

- I can't use any photo or PTFE/grey concrete as a white balance source for Nikon (also known on this forum).”

 

False! You just can’t do it in-camera. PhotoNinja will do it from the RAW as described above. 

Link to comment

There could possibly be a Nikon WB algorithm because Raw Digger uses some kind of white balance algorithm based on the characteristics of a particular camera model in order to output its RGB Render version of a raw photo.  I think the RD WB has something to do with the "white point" of the camera. If you knew that, then you might be able to work out how to reset a photo files four RGGB coordinates on a command line. But Raw Digger also could also make use of some AI in order to "guess" what areas are neutral (gray, black, white). I just don't know.

 

However #1...... Any broadband raw UV photo is red/orange or magenta/purple and that can stress white balance algorithms in converters on on a command line even when using PTFE or Spectralon against which to white balance. Adobe Camera Raw has been one notable example. ACR never could achieve a good white on PTFE or Spectralon in a UV photo. (Note:  I have not used it in recent years and do not know if ACR's WB is better now or what.)

 

However #2..... One such "automatic" algorithm can't work for every UV photo because what is recorded depends on the UV-capable lens, the UV illumination, and the UV-pass filter.  RD does rather well, but it isn't perfect.

 

My very best advice on this is that It is much easier to simply store a white balance preset in the converter for each combination of filter + lens + light used with the Nikon converted camera. Just shoot the PTFE at the beginning of each shoot. Whenever the filter or light changes, make another PTFE shot. When you unload the photos onto the computer, the first thing you do is make the WB presets for that shooting session. Then batch it onto the files.

 

Things to be careful about when recording PTFE for later use to create a white balance preset are:

 

  1. It is easy to blow out one or more channels because PTFE is very reflective but not necessarily diffuse. So be sure to check your JPG red, green and blue histograms to make sure no channel is blown. Many converter white balance tools will not work over the PTFE image if a channel is blown. And some which will work given only 1 (or perhaps 2) blown channel(s) will not produce a proper white. (A cyan cast is often seen, for example.)
  2. It is best to make the UV photo of the PTFE using a Neutral Picture Control with no increased contrast or increased saturation. Newer Nikons have a Flat Picture Control, so that's even better.

 


.

Lazy Girl UV false color WB algorithm for photos made with broadband UV-pass filter:

while (there are magenta/purple/pink hues in photo) {

click the white-balance dropper on magenta/purple/pink hues

}

It usually takes only a couple of clicks to obtain a reasonably good UV false color WB in apps like Photo Ninja

or the ancient NX2.

The Lazy Girl method is really just for casual use when you want a quick review of how something is going to look.

 

((Bad coding. In some converters you would never exit that while loop. Ha-ha.))

 

Link to comment
lukaszgryglicki

OK makes sense, so for given "setup" - 1st photo of PTFE or grey concrete (don't have PTFE yet) - store it as a reference white balance image, then WB everything else to that photo - I do already have algorithms to set WB using a reference photo - thanks, now I need to buy a PTFE...

 

Link to comment

You don’t even need ptfe. Assuming your lighting is constant through the session. Just open a first photo, manually set the wb, then save it back to the NEF. Script something to read that value from the NEF and write it into your other 100 raw files either with API or find and replace the new value in the file header with reg expressions. 
 

Back to your original method you can save an embedded jpg in the raw from the camera or just output a bitmap from the api to get your estimated white balance then put that value into the downstream NEF files. 

 

Unless you are creating a Mars rover then you probably want a piece of reference material in each frame that you can crop out. 

 

 

 

 

Link to comment
lukaszgryglicki

Yes, I need to write a script that will have 2 parameters:

- White balance source NEF

- List of "other" NEFs to process

Output will be an array of JPGs.

Easy, I needed just to figure out how to get a correct white balance for *any* shot - this clarifies everything - I'll do some scripting when I have time (maybe weekend?).

Link to comment

Dark table has an API. Though I don’t know if is extensive enough to open a photo, draw a rectangle and compute an average white balance, and save to file with scripting in the background. If you can control it that way and you always take your preparation photo orientation to the location of that rectangle you could maybe save a few manual steps. It might be worth looking into. 
 

Are you just trying to save some steps from your day out taking snapshots or is this an automated system without a user?

Link to comment
lukaszgryglicki

This handles mass white balance for me: https://github.com/lukaszgryglicki/true_dark_frame/blob/main/wbsrc.sh

Can be called like this: NOLG=1 WB=wbsrc.NEF /usr/local/bin/wbsrc.sh *.NEF

wbsrc.NEF is a source white balance photo - can be PTFE or grey concrete (I've tested using grey sidewalk)

Then you specify a list of NEF files to process. It saves each of them as JPEG (and will skip wbsrc.NEF if specified anywhere in arguments list).

This script needs:

- "dcraw" installed.

- Imagemagick installed ("convert" binary)

- My own jpeg processing binary installed: https://github.com/lukaszgryglicki/jpegbw (but this is optional, I only use it to stretch values from darkness 3% to saturation 99.95% - adjusting to most bright channel), if you specify "NOLG=1" it will use ImageMagick's "convert" tool to do the same.

Link to comment
lukaszgryglicki

Yep, it makes sense - the only photo of "grey sidewalk" I did when I was using Kolari UV-Pass a few days ago - those hots white balance good IMHO, while other photos with other filter sets (all of them include Hoya U-340) are too yellow or too green.

This is how Kolari UV-Pass photos looks like white-balanced to a concrete grey sidewalk photo (using the above script, yes I know that I had a light leak in the center that wasn't visible in those crazy red-pink images on the camera):

 

small_co__FSC3562.jpeg

small_co__FSC3564.jpeg

small_co__FSC3570.jpeg

small_co__FSC3600.jpeg

Link to comment
lukaszgryglicki

I will try to develop a habit of taking white balance source photo first.

It does a difference, each time you choose a different photo as WB=filename.NEF - different results are returned - tried even on NEFs made indoor in IR or FS or visible - applying them to UV NEFs - effects were funny, but not usable of course.

Regarding different day or weather (but same setup of filters/lenses) I think it will make a difference too, but a lot smaller.

 

 

Link to comment

Solar UV changes with cloud cover quite a bit. WB should generally be with whatever the light used to make the photo is. I WB once per day usually if it's not critical, but I don't reuse WB between different days.

Link to comment
lukaszgryglicki

Yeah, this script allows choosing arbitrary WB source, so you can make say 5 WB shoots and then for each WB source - apply to next files till the next WB source and so on... I need to test this, possibly tomorrow with different filters set, also without filters (FS).

Link to comment

haha a script for white balance?

it's like looking for a wife :-)))
each photo has different contrast, brightness, color temperature

impossible

Link to comment
lukaszgryglicki

So you say it's impossible, even when I have white balance source for each shoot (x2)?

Man, I'm learning all the time, now let me know when I'm wrong.

 

Link to comment

after 25 years of digital photography I realized that half an aperture is enough to change the white balance parameters a little.
from my recent and poor experience I see that the illogical differences of the curves of the three RGB channels in UV and IR cause unpredictable very strong variations in the WB

Link to comment

This is true. In near-constant lighting, one can get by with using the same white balance for a batch of photos provided they are not too widely spaced in time and camera parameters stay mostly the same. And if you are not too picky.

 

If you ARE picky, you had best make a separate WB for each shot. It is a question of how much tolerance one is willing to allow. 

Link to comment

Another thing to remember in the white balance of UV false colors: You have modified the camera by removing internal filtration. The camera's color output was designed to include those filters. So the color profiles in any converter (either some app or some command line program) really no longer precisely fit your modified camera. You will get a slightly different UV false color set when using the app's included profiles than you would if you created a new color profile for your converted camera. Of course, creating a new color profile for a converted camera has its own difficulties which have been discussed elsewhere.

 

But does any of that matter? IF you wish to "standardize" your UV photo, then what you are aiming for is to get your UV false colors into a blue/yellow/black/white/grey palette, but without worrying whether your particular blue is more cyan-ish or more violet-ish or whatever.  Similarly your yellow outcomes might tend slightly to the yellow-orange or the yellow-olive. There is no well-defined standard for UV false colors. It's impossible. We know nothing at all (at this point) about Bayer dyes and which ones are used in which camera.

 

Don't overthink UV false colors, right?? 🟨🟦

 

 

Link to comment
3 hours ago, photoni said:

haha a script for white balance?

it's like looking for a wife :-)))
each photo has different contrast, brightness, color temperature

impossible

This actually made me laugh outloud. 

Too funny. 

 

Andrea,  I take the left. Always like a more golden false color. 

Link to comment
5 hours ago, Andrea B. said:

Another thing to remember in the white balance of UV false colors: You have modified the camera by removing internal filtration. The camera's color output was designed to include those filters. So the color profiles in any converter (either some app or some command line program) really no longer precisely fit your modified camera. You will get a slightly different UV false color set when using the app's included profiles than you would if you created a new color profile for your converted camera. Of course, creating a new color profile for a converted camera has its own difficulties which have been discussed elsewhere.

 

But does any of that matter? IF you wish to "standardize" your UV photo, then what you are aiming for is to get your UV false colors into a blue/yellow/black/white/grey palette, but without worrying whether your particular blue is more cyan-ish or more violet-ish or whatever.  Similarly your yellow outcomes might tend slightly to the yellow-orange or the yellow-olive. There is no well-defined standard for UV false colors. It's impossible. We know nothing at all (at this point) about Bayer dyes and which ones are used in which camera.

 

Don't overthink UV false colors, right?? 🟨🟦

 

 

 

I think Andrea is on the right path here.

 

I don't think white balance is the final key here. What you need to look at is the common "false" color palette found in your images after they have been auto white balanced. Typically I see maybe green, yellow, violet, blue and overwhelming grey. Once your tool has set a white balance to the target photo you need to create a color palette matrix by grouping those colors and identify tints from that image and compare that to a previous made reference control matrix for out of bound color. We can say with a general guess toll as you have create, good enough, it is always the tint that throws off the final image.

 

The reference matrix can be created with your particular make and model of camera using some type of diffraction grating. We can say that for most lenses in your bag this will be the same spectrum of palette with some variations at the ends. Again, my images have an overwhelming amount of grey in the leaves mostly. Look for out of bound colors after you have applied the best guess white balance from your reference prep photo. This control matrix does not have to be generated every day. It should a one time matrix based on camera. You can base in on a variation of lighting wide or narrow band but it should still land within an expected range of colors. It can be something that you fine tune over time based on averages and end points.

 

Now you have hotspots but I can say as an outlier if you know those colors then they need to be included. 

 

In other words a smart profile.

 

If you really want to kick it up a notch you can interface with a service that can identify the location of a leaf pattern on your image and report back the top left and bottom right corner. This will give you a region of interest to create a common color profile to look for that grey average. Not necessary plant identification though that would be great, but an area that we could guess as that grey. If you did have an interface with a plant ID service you could then create a custom color matrix for each plant in UV. However, that is more of a google project. 

Link to comment

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...