towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Permanently turn AWB off

Wed Mar 26, 2014 1:33 am

6by9 wrote:Capture a JPEG. less foo.jpg (it'll whinge about being a binary file - ignore it)
I used

Code: Select all

grep -ao -m1 "ev=.*ISP" foo.jpg
for the whole tag, to just get those values this should work

Code: Select all

grep -ao -m1 "gain_r=..... gain_b=....." foo.jpg
If you want to automate this with enhanced grep:

Code: Select all

referenceWB=$(grep -Paom1 'gain_[rb]=\K(.....)' reference.jpg | paste -sd,)
raspistill -w 2592 -h 1944 -tl 2000 -t 15000 -awb off -awbg $referenceWB -o foo%d.jpg

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7304
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Permanently turn AWB off

Wed Mar 26, 2014 7:59 am

towolf wrote:Sweet. That fixes this issue: https://github.com/raspberrypi/userland/issues/140
Not quite released yet, but yes.
I hadn't seen that bug report (James normally keeps track of the stuff on github and points me at the interesting ones), but it's a useful one to catch. The scary thing is that it has been in the code for about 5 years and no one had noticed until now!
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23682
Joined: Sat Jul 30, 2011 7:41 pm

Re: Permanently turn AWB off

Wed Mar 26, 2014 9:26 am

I'll try and sort out all the raspistill issues today.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

ethanol100
Posts: 585
Joined: Wed Oct 02, 2013 12:28 pm

Re: Permanently turn AWB off

Wed Mar 26, 2014 10:02 am

Thank you, this is a really nice new feature to set the awb gain levels.

Can we get something similar with the "-ex" switch? Setting it to off and specifying the analogue and digital gain together with the shutterspeed "-ss" would create a nice manual mode.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7304
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Permanently turn AWB off

Wed Mar 26, 2014 11:02 am

ethanol100 wrote:Can we get something similar with the "-ex" switch? Setting it to off and specifying the analogue and digital gain together with the shutterspeed "-ss" would create a nice manual mode.
You almost get that with -ss and -iso.
The AE algorithm is significantly more involved than the AWB algo as it has more "outputs" - exposure time, analogue gain, digital gain, and in theory aperture control. I don't know how easy it would be to provide full access to all those settings, and haven't really got time to look into it more at the moment - sorry.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

ethanol100
Posts: 585
Joined: Wed Oct 02, 2013 12:28 pm

Re: Permanently turn AWB off

Wed Mar 26, 2014 11:24 am

6by9 wrote: You almost get that with -ss and -iso.
Yes, I'm using it like this, but you see some oscillations at the beginning due to the gain ranges. But it is ok for me, the awb was the real problem.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7304
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Permanently turn AWB off

Thu Mar 27, 2014 12:45 pm

Firmware and V4L2 changes released with commit https://github.com/raspberrypi/firmware ... 2966e82624.
That means the V4L2 commands listed above of

Code: Select all

v4l2-ctl --set-ctrl=white_balance_auto_preset=0
v4l2-ctl --set-ctrl=red_balance=1500
v4l2-ctl --set-ctrl=blue_balance=1500
should now work to allow you to tweak the gains.

James is on the case for Raspistill, but he had to wait for the userland headers to get updated first. It shouldn't take him long to sort it (although that'll then be another pull request back from https://github.com/JamesH65/userland to the main userland tree! Nothing like keeping Dom busy). For those too desperate to try it out, my patch to implement it is:

Code: Select all

From ab423a2648a9c9ac115e04619c0841db28ef0587 Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dsteve@broadcom.com>
Date: Tue, 18 Mar 2014 16:33:34 +0000
Subject: [PATCH] Raspicam AWB gains

Change-Id: I9d7d57e4d2e114263f41b2b52f33737d41497216
---
 .../linux/apps/raspicam/RaspiCamControl.c          |   42 +++++++++++++++++++-
 .../linux/apps/raspicam/RaspiCamControl.h          |    3 ++
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/host_applications/linux/apps/raspicam/RaspiCamControl.c b/host_applications/linux/apps/raspicam/RaspiCamControl.c
index 959745f..e4a8f0a 100755
--- a/host_applications/linux/apps/raspicam/RaspiCamControl.c
+++ b/host_applications/linux/apps/raspicam/RaspiCamControl.c
@@ -131,6 +131,7 @@ static const int metering_mode_map_size = sizeof(metering_mode_map)/sizeof(meter
 #define CommandVFlip       14
 #define CommandROI         15
 #define CommandShutterSpeed 16
+#define CommandAwbGains    17
 
 static COMMAND_LIST  cmdline_commands[] =
 {
@@ -150,7 +151,8 @@ static COMMAND_LIST  cmdline_commands[] =
    {CommandHFlip,       "-hflip",     "hf", "Set horizontal flip", 0},
    {CommandVFlip,       "-vflip",     "vf", "Set vertical flip", 0},
    {CommandROI,         "-roi",       "roi","Set region of interest (x,y,w,d as normalised coordinates [0.0-1.0])", 1},
-   {CommandShutterSpeed,"-shutter",   "ss", "Set shutter speed in microseconds", 1}
+   {CommandShutterSpeed,"-shutter",   "ss", "Set shutter speed in microseconds", 1},
+   {CommandAwbGains,    "-awbgains",  "awbg", "Set AWB gains", 1}
 };
 
 static int cmdline_commands_size = sizeof(cmdline_commands) / sizeof(cmdline_commands[0]);
@@ -575,11 +577,30 @@ int raspicamcontrol_parse_cmdline(RASPICAM_CAMERA_PARAMETERS *params, const char
    }
 
    case CommandShutterSpeed : // Shutter speed needs single number parameter
+   {
       sscanf(arg2, "%d", &params->shutter_speed);
       used = 2;
       break;
+   }
+
+   case CommandAwbGains :
+      {
+      double r,b;
+      int args;
+
+      args = sscanf(arg2, "%lf,%lf", &r,&b);
 
+      if (args != 2 || r > 8.0 || b > 8.0)
+      {
+         return 0;
+      }
 
+      params->awb_gains_r = r;
+      params->awb_gains_b = b;
+
+      used = 2;
+      break;
+      }
    }
 
    return used;
@@ -712,6 +733,8 @@ void raspicamcontrol_set_defaults(RASPICAM_CAMERA_PARAMETERS *params)
    params->roi.x = params->roi.y = 0.0;
    params->roi.w = params->roi.h = 1.0;
    params->shutter_speed = 0;          // 0 = auto
+   params->awb_gains_r = 0;      // Only have any function if AWB OFF is used.
+   params->awb_gains_b = 0;
 }
 
 /**
@@ -765,6 +788,7 @@ int raspicamcontrol_set_all_parameters(MMAL_COMPONENT_T *camera, const RASPICAM_
    result += raspicamcontrol_set_exposure_mode(camera, params->exposureMode);
    result += raspicamcontrol_set_metering_mode(camera, params->exposureMeterMode);
    result += raspicamcontrol_set_awb_mode(camera, params->awbMode);
+   result += raspicamcontrol_set_awb_gains(camera, params->awb_gains_r, params->awb_gains_b);
    result += raspicamcontrol_set_imageFX(camera, params->imageEffect);
    result += raspicamcontrol_set_colourFX(camera, &params->colourEffects);
    //result += raspicamcontrol_set_thumbnail_parameters(camera, &params->thumbnailConfig);  TODO Not working for some reason
@@ -1004,6 +1028,22 @@ int raspicamcontrol_set_awb_mode(MMAL_COMPONENT_T *camera, MMAL_PARAM_AWBMODE_T
    return mmal_status_to_int(mmal_port_parameter_set(camera->control, &param.hdr));
 }
 
+int raspicamcontrol_set_awb_gains(MMAL_COMPONENT_T *camera, float r_gain, float b_gain)
+{
+   MMAL_PARAMETER_AWB_GAINS_T param = {{MMAL_PARAMETER_CUSTOM_AWB_GAINS,sizeof(param)}, {0,0}, {0,0}};
+
+   if (!camera)
+      return 1;
+
+   if (!r_gain || !b_gain)
+      return 0;
+
+   param.r_gain.num = (unsigned int)(r_gain * 65536);
+   param.b_gain.num = (unsigned int)(b_gain * 65536);
+   param.r_gain.den = param.b_gain.den = 65536;
+   return mmal_status_to_int(mmal_port_parameter_set(camera->control, &param.hdr));
+}
+
 /**
  * Set the image effect for the images
  * @param camera Pointer to camera component
diff --git a/host_applications/linux/apps/raspicam/RaspiCamControl.h b/host_applications/linux/apps/raspicam/RaspiCamControl.h
index 4619a4a..d120421 100644
--- a/host_applications/linux/apps/raspicam/RaspiCamControl.h
+++ b/host_applications/linux/apps/raspicam/RaspiCamControl.h
@@ -130,6 +130,8 @@ typedef struct
    int vflip;                 /// 0 or 1
    PARAM_FLOAT_RECT_T  roi;   /// region of interest to use on the sensor. Normalised [0,1] values in the rect
    int shutter_speed;         /// 0 = auto, otherwise the shutter speed in ms
+   float awb_gains_r;         /// AWB red gain
+   float awb_gains_b;         /// AWB blue gain
 } RASPICAM_CAMERA_PARAMETERS;
 
 
@@ -158,6 +160,7 @@ int raspicamcontrol_set_video_stabilisation(MMAL_COMPONENT_T *camera, int vstabi
 int raspicamcontrol_set_exposure_compensation(MMAL_COMPONENT_T *camera, int exp_comp);
 int raspicamcontrol_set_exposure_mode(MMAL_COMPONENT_T *camera, MMAL_PARAM_EXPOSUREMODE_T mode);
 int raspicamcontrol_set_awb_mode(MMAL_COMPONENT_T *camera, MMAL_PARAM_AWBMODE_T awb_mode);
+int raspicamcontrol_set_awb_gains(MMAL_COMPONENT_T *camera, float r_gain, float b_gain);
 int raspicamcontrol_set_imageFX(MMAL_COMPONENT_T *camera, MMAL_PARAM_IMAGEFX_T imageFX);
 int raspicamcontrol_set_colourFX(MMAL_COMPONENT_T *camera, const MMAL_PARAM_COLOURFX_T *colourFX);
 int raspicamcontrol_set_rotation(MMAL_COMPONENT_T *camera, int rotation);
-- 
1.7.10.4
(the forums won't allow me to attach a .patch, .txt, or a file with no extension - ho hum).
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

ethanol100
Posts: 585
Joined: Wed Oct 02, 2013 12:28 pm

Re: Permanently turn AWB off

Thu Mar 27, 2014 1:11 pm

Thank you for your patch!

With the changes in /opt/vc/include/interface/mmal/mmal_parameters_camera.h it was quite easy to modify the source (have tested it last night on my own, I was to curious about it). Works really well, now I can record a red wall and it stays red :)

Edit: Just want to add a note that ist is working well in both raspistill and raspivid. And have just noticed the last two commits. Now interface/mmal/mmal_parameters_camera.h is up to date.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23682
Joined: Sat Jul 30, 2011 7:41 pm

Re: Permanently turn AWB off

Thu Mar 27, 2014 1:35 pm

Just sent a PR to Dom for this. can be found on my github page...

https://github.com/JamesH65/userland/tree/fixed_awb_fix
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

Dillow
Posts: 27
Joined: Tue Feb 11, 2014 10:25 pm

Re: Permanently turn AWB off

Fri Mar 28, 2014 3:40 pm

This is great! Any ideas as to when it makes it into a release?

ethanol100
Posts: 585
Joined: Wed Oct 02, 2013 12:28 pm

Re: Permanently turn AWB off

Fri Mar 28, 2014 4:50 pm

The changes have been merged today in the main userland repo(https://github.com/raspberrypi/userland/pull/158). But you will need to wait until a new firmware will be released. You can check https://twitter.com/RPF_Dev_Updates, there every new released "[firmware]" will be posted.

Usually it should be updated quite fast(order of some days).

But you can try to build it yourself, it is easy and only take about 30 min on the pi ;)

Dillow
Posts: 27
Joined: Tue Feb 11, 2014 10:25 pm

Re: Permanently turn AWB off

Fri Mar 28, 2014 5:00 pm

Thanks I plan to try it out. As soon as I figure out how to build it. :D

ethanol100
Posts: 585
Joined: Wed Oct 02, 2013 12:28 pm

Re: Permanently turn AWB off

Fri Mar 28, 2014 5:08 pm

Should be just this three steps:

Code: Select all

git clone https://github.com/raspberrypi/userland
cd userland
./buildme
(If it fails, i.e. cmake missing or some other complains, you can try to install the dependencies with "apt-get install build-essential git cmake" and rerun the ./buildme)

Dillow
Posts: 27
Joined: Tue Feb 11, 2014 10:25 pm

Re: Permanently turn AWB off

Fri Mar 28, 2014 5:23 pm

Cool thanks you saved me some major time looking this up,have not installed the compiler on a pi before.

Dillow
Posts: 27
Joined: Tue Feb 11, 2014 10:25 pm

Re: Permanently turn AWB off

Mon Mar 31, 2014 6:46 pm

Did the -awbg switch get removed from the latest firmware? I just did a api-update and it is now a invalid option according to raspistill.

ethanol100
Posts: 585
Joined: Wed Oct 02, 2013 12:28 pm

Re: Permanently turn AWB off

Mon Mar 31, 2014 6:57 pm

It has not yet been included. The last firmware has only updated the "blobb", not the userland programs. But rpi-update always overwrites everything in /opt/vc. You can rerun ./buildme to install your compiled version again.
(If you have keeped the userland folder it will just copy the already compiled binaries, this will be quick.)

Dillow
Posts: 27
Joined: Tue Feb 11, 2014 10:25 pm

Re: Permanently turn AWB off

Thu Apr 10, 2014 8:26 pm

I see this has been released and is working great Thanks again this is very useful for getting consistent WB results in controlled environments.

It does not affect me as I am not using previews but I did notice the Previews are still doing their own AWB. That may be on purpose just figured I would mention it.

mitchei
Posts: 1
Joined: Sun Apr 06, 2014 7:51 pm

Re: Permanently turn AWB off

Sat Apr 12, 2014 8:19 am

Thank you , I just tried the -awbg option and it works great. But as tried to grab the gain values from a just taken picture as towolf explained, it failed. As I looked into the JPG, the saved gain values of both were always "1.000". It seems that the -awb option there fore doesn't matters, I tested it with sun and auto.

Is there option that must be set saving the gain values correctly to JPG?

Dillow
Posts: 27
Joined: Tue Feb 11, 2014 10:25 pm

Re: Permanently turn AWB off

Sat Apr 12, 2014 1:51 pm

mitchei wrote:Thank you , I just tried the -awbg option and it works great. But as tried to grab the gain values from a just taken picture as towolf explained, it failed. As I looked into the JPG, the saved gain values of both were always "1.000". It seems that the -awb option there fore doesn't matters, I tested it with sun and auto.

Is there option that must be set saving the gain values correctly to JPG?
There was a bug causing the EXIF data not to be updated but these guys fixed it. Did you do a Software and Firmware update?

EduardK
Posts: 2
Joined: Wed Apr 23, 2014 5:31 pm

Re: Permanently turn AWB off

Fri Apr 25, 2014 8:58 am

Hey Guys
This is my first post on the forum so please be gentle if its a dumb question or in the wrong place.

I would like to be able to turn off (pre set) all the auto adjustment that the pi camera does when it starts recording. The reason for this is that I need to do color analysis on a scene where the change that i'm looking for takes place in the first 2 seconds after the light is turned on.

I have tried the abw and awbg command when calling raspivid as shown:

Code: Select all

raspivid -o foo.h264 -t 5000 -w 1280 -h 720 -awb off awbg 1.5
but then I get an "invalid command line option (awbg)" error
and when I remove the awbg statement and run as follows:

Code: Select all

raspivid -o vid_test/vid.h264 -t 5000 -w 1280 -h 720 -awb off
It records a pinch black video.

If I understand this thread then the problem has been sorted, but so far I have had no luck duplicating the results. My firmware was updated last week so all the changes should be in my release.

Any help would be very much appreciated.
E

ethanol100
Posts: 585
Joined: Wed Oct 02, 2013 12:28 pm

Re: Permanently turn AWB off

Fri Apr 25, 2014 11:09 am

You can try the following:

Code: Select all

raspivid -t 5000 -w 1280 -h 720 -awb off -awbg 1.5,1.2 -ISO 100 -ss 10000 -o foo.h264
You need to set the awb gains for the blue and the red channel(i.e., 1.5 for the red channel and 1.2 for the blue channel). Additionally to fix the exposure you can set the shutter speed with the -ss switch(here for example 10000us=10ms=0.01s) and the ranges of the analog and digital gain of the sensor can be influenced with fixing the ISO value. You will see some fluctuations in brightness at the beginning due to the camera choosing the "best" analog and digital camera gains.

Mikkel Kragh
Posts: 1
Joined: Tue Apr 29, 2014 6:18 am

Re: Permanently turn AWB off

Tue Apr 29, 2014 6:37 am

Dillow wrote:I see this has been released and is working great Thanks again this is very useful for getting consistent WB results in controlled environments.

It does not affect me as I am not using previews but I did notice the Previews are still doing their own AWB. That may be on purpose just figured I would mention it.
My experience is that disabling the preview (raspivid -n) disables the AWB settings. So in order to obtain the correct AWB, I need to use previews.

EduardK
Posts: 2
Joined: Wed Apr 23, 2014 5:31 pm

Re: Permanently turn AWB off

Tue Apr 29, 2014 5:50 pm

ethanol100 wrote:You can try the following:

Code: Select all

raspivid -t 5000 -w 1280 -h 720 -awb off -awbg 1.5,1.2 -ISO 100 -ss 10000 -o foo.h264
You need to set the awb gains for the blue and the red channel(i.e., 1.5 for the red channel and 1.2 for the blue channel). Additionally to fix the exposure you can set the shutter speed with the -ss switch(here for example 10000us=10ms=0.01s) and the ranges of the analog and digital gain of the sensor can be influenced with fixing the ISO value. You will see some fluctuations in brightness at the beginning due to the camera choosing the "best" analog and digital camera gains.
I still get "Invalid command line option (-awbg)". I have also tried adding sudo before the command.
Could this have something to do with the fact that I have Pi Camera installed? (not using it now though)
Or that I'm controlling the pi through SSH?

Kernel: Linux raspberrypi 3.10.34+ #661 PREEMPT Thu Mar 27 00:36:02 GMT 2014 armv6l GNU/
Firmware: Mar 30 2014 15:59:18
Copyright (c) 2012 Broadcom
version 8f13fa508997a043a3d78822e3f67ec044b4e7bf (clean) (release)

Any ideas?

ethanol100
Posts: 585
Joined: Wed Oct 02, 2013 12:28 pm

Re: Permanently turn AWB off

Wed Apr 30, 2014 6:34 am

EduardK wrote: I still get "Invalid command line option (-awbg)". I have also tried adding sudo before the command.
Could this have something to do with the fact that I have Pi Camera installed? (not using it now though)
Or that I'm controlling the pi through SSH?

Kernel: Linux raspberrypi 3.10.34+ #661 PREEMPT Thu Mar 27 00:36:02 GMT 2014 armv6l GNU/
Firmware: Mar 30 2014 15:59:18
Copyright (c) 2012 Broadcom
version 8f13fa508997a043a3d78822e3f67ec044b4e7bf (clean) (release)

Any ideas?
You need to update your firmware. Try running rpi-update.

Stigmen
Posts: 2
Joined: Tue Jul 08, 2014 7:29 am

Re: Permanently turn AWB off

Fri Aug 15, 2014 7:35 pm

Kudos to you guys, you saved my measurements :)

Return to “Camera board”