Precision of RGB Gain

Dear all,

I try to understand how works the gain in OV7725 camera and i am lost without speaking about analog gain.

Firstly, there is the main gain at register address 0x00 which can be used with these both python functions where the gain is in db :

  • sensor.set_auto_gain( False, gain_db=(1 to 32db) )
  • sensor.get_gain_db()

Secondly, there are the 3 white balance gains at register address 0x01 to 0x03 which can be used with these both python functions where the gain is in db :

  • sensor.set_auto_whitebal(False, rgb_gain_db=(-42 to 6db for three colors)
  • sensor.get_rgb_gain_db()

I red the c language code, and i did not succeed to understand the equation between db gains and register values ?
Why the ranges are differents for same 8 bits registers ?

I tested to use the main gain in db and i did not succeed to have a good precision.
Here is my code test :

import sensor, image, time
sensor.reset()                     
sensor.set_pixformat(sensor.GRAYSCALE) 
sensor.set_framesize(sensor.VGA)   
sensor.skip_frames(time = 500)   
sensor.set_auto_whitebal(False)
sensor.set_auto_exposure(False)
gainstart=1
gainstep=0.5
gainend=32
gaindb = gainstart
while(True):
   img = sensor.snapshot()  
   sensor.set_auto_gain(False,gain_db=gaindb)
   time.sleep(100)
   getgaindb=sensor.get_gain_db()
   diff = abs(gaindb - getgaindb)
   print(diff, gaindb, getgaindb)
   gaindb += gainstep
   if gaindb > gainend :
      gaindb = gainstart

where i can have difference of 2db …

Could you help me to use these gains ?
Thanks,

Hi, I turned the values into db because they are unitless otherwise and mean different things on every camera. Please do not expect the values to 100% matchup always for writing and reading. There is quite a bit of precision loss because of how the conversion step works.

Please see the C code for what is actually going on. You can directly read the registers if you don’t like the driver wrapper I provided. This will give you the exact values.

Thanks,

I tried to modify the gain register at address 0x00 and read the db gain with sensor.get_gain_db().

Here is the code, and it is not linear between register and the db gain.

import sensor, image, time
sensor.reset()                     
sensor.set_pixformat(sensor.GRAYSCALE) 
sensor.set_framesize(sensor.VGA)   
sensor.skip_frames(time = 500)   
sensor.set_auto_whitebal(False)
sensor.set_auto_exposure(False)
sensor.set_auto_gain(False)
reggain = 0
while(True):
   img = sensor.snapshot()  
   sensor.__write_reg(0x00, reggain)
   time.sleep(100)   
   getgaindb=sensor.get_gain_db()
   print( reggain, "%.1f" % getgaindb)
   reggain += 1
   if reggain > 255 :
      reggain = 0

I red the c language code, and did an excel sheet to understand this :
4 MSB seems to be a factor of 4 LSB like this :

  • High = 1 << total_number_of_bit_to_1_of_the_4x_MSB (between 1 to 4, but it is not ordered)
  • Low = ( 4xLSB / 16 ) + 1
  • db = 20 x ln ( High x Low ) / ln (10)

I dont get the equation and how to use this register without your db way.
I did not find any appnote from omnivision about how to use the 0v7725 gain register ?

Thanks,

There’s no app note from Omnivision. I’m guessing that’s how it works based on an appnote from the OV2640 and OV9965 as that’s how OmniVision likes to control their gain registers. It may not actually be correct. Please assume the value you put into the reg is completely opaque.

Regarding auto_gain/exposure, I have some fixes to the OV7725 driver coming soon that improve the image quality.

Omnivision likes to make the top 4 bits of registers a integer gain and the bottom 4 bits a fractional gain of 1/16.