Shot Histogram Request

  • 467 Replies
  • 39188 Views
*

Offline philmoz

  • *****
  • 3070
    • Photos
Re: Shot Histogram Request
« Reply #20 on: 24 / November / 2012, 16:31:17 »
Advertisements
static int luaCB_shot_histo_enable( lua_State* L )
{
  shot_histogram_set(luaL_checknumber( L, 1 ));
  return 0;
}

I want to change shot_histo_enable(1) to shot_histo_enable(1,x1,y1,x2,y2) where the new parameters are optional. How do I tell if the number of parameters is 1 or 5?

It looks like the total number of pixel samples can be returned by shot_histogram_set, and this value returned by Lua without breaking any old scripts. Is this correct?

lua_pushnumber( L, total )

Use luaL_optnumber, third parameter is default value if the parameter is not included.
Code: [Select]
  coord x1 =luaL_optnumber(L,2,0);
  coord y1 =luaL_optnumber(L,3,0);
  coord x2 =luaL_optnumber(L,4,100);
  coord y2 =luaL_optnumber(L,5,100);

Phil.
CHDK ports:
  sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
  g12 (1.00c, 1.00e, 1.00f & 1.00g)
  sx130is (1.01d & 1.01f)
  ixus310hs (1.00a & 1.01a)
  sx40hs (1.00d, 1.00g & 1.00i)
  g1x (1.00e, 1.00f & 1.00g)

*

Offline philmoz

  • *****
  • 3070
    • Photos
Re: Shot Histogram Request
« Reply #21 on: 24 / November / 2012, 16:32:32 »
Do you know if build_shot_histogram() is called right after each picture, regardless of whether or not shot_histo is enabled? Thanks.

build_shot_histogram is only called if a script is running and shot_histogram_isenabled returns true.

Phil.
CHDK ports:
  sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
  g12 (1.00c, 1.00e, 1.00f & 1.00g)
  sx130is (1.01d & 1.01f)
  ixus310hs (1.00a & 1.01a)
  sx40hs (1.00d, 1.00g & 1.00i)
  g1x (1.00e, 1.00f & 1.00g)

*

Offline lapser

  • *****
  • 1093
Re: Shot Histogram Request
« Reply #22 on: 24 / November / 2012, 17:22:00 »
build_shot_histogram is only called if a script is running and shot_histogram_isenabled returns true.
Thanks. I was able to add shot_ready() and test it, and it didn't work right anyway. It was a good exercise in learning to add Lua functions though.

Use luaL_optnumber, third parameter is default value if the parameter is not included.
Thanks again Phil. You're a great resource. I'll give it a try.

By the way, the zoom lever on the variable speed zoom sx260 works perfectly now.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline lapser

  • *****
  • 1093
Re: Shot Histogram Request
« Reply #23 on: 24 / November / 2012, 20:31:11 »
I was able to modify get_histo_range to add an optional 3rd parameter:
get_histo_range(from, to [, scale])

It should work with older scripts in Lua or uBasic. Here's the code from my test script:
Code: (lua) [Select]
    step=64
    for i=0,1023,step do
      hrange=get_histo_range(i,i+step)
      hfile:write(string.format("%5d",hrange))
    end
    hfile:write("\n")
    for i=0,1023,step do
      hrange=get_histo_range(i,i+step,1000)
      hfile:write(string.format("%5d",hrange))
    end
    hfile:write("\n")
    for i=0,1023,step do
      hrange=get_histo_range(i,i+step,0)
      hfile:write(string.format("%5d",hrange))
    end
It produces this output:
    0    2    1    1    1    2    5   13    9    6   12    6    3    8   14   11
    8   21   13   12   17   27   54  132   94   62  122   67   37   80  146  114
  107  253  166  154  211  331  657 1587 1130  753 1473  806  455  964 1756 1376

I was able to create "patch.txt" and "patch.diff" test files with CHDK shell, which are attached. Could you make changes using these files?

I'll add the range option to build_shot_histogram, and clean up the code next.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos


*

Offline lapser

  • *****
  • 1093
Re: Shot Histogram Request
« Reply #24 on: 25 / November / 2012, 20:58:34 »
I'm making some progress on the shot_histo modifications. I decided that adding a new function to set the histogram window was best. Here's the new function I added:

total_pixels=set_histo_area(xp1,yp1,xp2,yp2)

The xp and yp parameters are percentages, from 0 to 100, of the total histogram area. They default to 0,0,100,100, so you can call:  pix=set_histo_area() to reset to the defaults. shot_histo_set(1) also resets the full area default values.

From looking at the Bayer filter pictures, it looks likes if I use an odd number step size when sampling the raw pixels, I'll alternate between 2 colors and get a better estimate of luminace. I'm planning to add up all the pixel values and return the average as a 2nd parameter from get_histo_range, possibly converted to an APEX96 value if I can figure it out. A brightness of 50% of full white will return an APEX96 value of 0. It should be interesting.

range,average_brightness=get_histo_range(from, to, scale)

Any ideas or suggestions would be appreciated.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline philmoz

  • *****
  • 3070
    • Photos
Re: Shot Histogram Request
« Reply #25 on: 27 / November / 2012, 03:30:53 »
From looking at the Bayer filter pictures, it looks likes if I use an odd number step size when sampling the raw pixels, I'll alternate between 2 colors and get a better estimate of luminace.

You would probably be better to just sample the green channel.
My understanding is that the red and blue pixels are less sensitive (probably due to the color filter) and require a different tone curve. Sticking with just one channel should be more consistent across the image.

Quote
I'm planning to add up all the pixel values and return the average as a 2nd parameter from get_histo_range, possibly converted to an APEX96 value if I can figure it out. A brightness of 50% of full white will return an APEX96 value of 0.

What does the average of the raw sensor pixels tell you?
Why would an APEX number be any better than a value from 0 -1023?

Phil.
CHDK ports:
  sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
  g12 (1.00c, 1.00e, 1.00f & 1.00g)
  sx130is (1.01d & 1.01f)
  ixus310hs (1.00a & 1.01a)
  sx40hs (1.00d, 1.00g & 1.00i)
  g1x (1.00e, 1.00f & 1.00g)

*

Offline lapser

  • *****
  • 1093
Re: Shot Histogram Request
« Reply #26 on: 27 / November / 2012, 06:46:06 »
Quote
I'm planning to add up all the pixel values and return the average as a 2nd parameter from get_histo_range, possibly converted to an APEX96 value if I can figure it out. A brightness of 50% of full white will return an APEX96 value of 0.
What does the average of the raw sensor pixels tell you?
Why would an APEX number be any better than a value from 0 -1023?
Phil.
I'm hoping to be able to use the average to set the shutter speed for the next shot in a time lapse. Since I need to enter an Apex96 value for shutter speed (and ISO), it would be best if the value I return is also Apex96. The absolute value of the average won't mean much, but how it changes between shots will tell me how to adjust the exposure for the next shot. It will take some experimenting to figure it out. I added a new function called:
get_histo_sum() to return the value. It's less confusing than returning a second value in get_histo_range(..)

I did get my new function working:

total_samples=set_histo_area(10,10,90,50)

I added some test code to get_raw_pixel(x,y) that stores a line off 0xff at the sampled pixel location. Then, I look at the pattern of dots to see if I have the right area. Also, by setting the step size to 1 (by mistake), I turned the entire area white.

One thing I noticed is that the old shot_histo used the entire sensor area minus a margin, so the dot pattern of samples went off screen to the left. I used the sensor active area values, and my sample area appears to be correctly centered.

I've attached a test picture which shows the area produced by the Lua statement above and my step size 1 bug. The lens distortion correction in the jpg makes it look like a sheet hanging on the line.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline lapser

  • *****
  • 1093
Re: Shot Histogram Request
« Reply #27 on: 30 / November / 2012, 04:06:52 »
Here's two test images that show the progress I'm making on shot_histo. They demonstrate the ability to set the area where the histogram is taken from. After I sampled each pixel and added it to the histogram, I called set_raw_pixel to set it to the WHITE_LEVEL. The resulting colored dots show the Bayer pattern in the area sampled. The normal step size is around 31 pixels, but I sample more pixels as the area gets smaller, keeping the total around 12,000 samples.

The first picture shows the sampled area in the center at about 30% of the full image. The 12,000 sampled pixels are more dense in the smaller area.

The second picture shows samples in the entire picture, so the histogram will reflect the entire image. However, I also add up the values of all the pixels to get a sum that roughly corresponds to exposure. I'm hoping to be able to adjust exposure using this value. The center dark area was also sampled for the histogram. But the sum of the pixels is computed from only the center area. That is, you can specify an area within the histogram where the sum will be calculated; a kind of spot metering.

[EDIT] Right Click, save or open full resolution image in new tab, click to expand so you can see the dot pattern.



« Last Edit: 01 / December / 2012, 10:20:17 by lapser »
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos


*

Offline lapser

  • *****
  • 1093
Shot Histogram Sum experiment success!
« Reply #28 on: 30 / November / 2012, 22:11:57 »
For this experiment, I had the camera calculate a shutter speed with half press. Then, I did a series of shots of a blank wall with shoot_full_only, varying the shutter speed from -192 to +192 relative to the calculated shutter speed. The change in tV exposure is -32 ev each time.

After each shot, I added up 12,000 evenly spaced pixel samples from the inner 50% of the raw image buffer, alternating pixel colors in the Bayer patern, (RGBG or similar). I took the sum of all pixels (12 bits each in this camera), and did a LOG base 2, multiplied by 96 (I call HSUM96). The printed values on the right are the HSUM96 minus the HSUM96 of the previous picture. The change in exposure is always -32 (1/3 ev). The HSUM96 is close to that, and the offset appears linear, so I should be able to find a constant to correct for it.

It looks like the idea of adding up the sum of pixels is a success, and should prove useful in setting the exposure of the next shot based on the sampled pixel sum from the last shot. By eliminating the half-shoot before each picture, this saves about 2 seconds between shots. Maximum speed apears to be about 1.5 shots per second this way.
 
Code: [Select]
  ev+- | deltaHSUM96
  -192     0
  -160   -33
  -128   -29
   -96   -29
   -64   -29
   -32   -27
     0   -27
    32   -26
    64   -23
    96   -23
   128   -22
   160   -18
   192   -19

  -192   304
  -160   -30
  -128   -31
   -96   -28
   -64   -29
   -32   -29
     0   -25
    32   -26
    64   -25
    96   -21
   128   -21
   160   -20
   192   -16

  -192   301
  -160   -30
  -128   -26
   -96   -31
   -64   -30
   -32   -26
     0   -27
    32   -26
    64   -23
    96   -24
   128   -22
   160   -18
   192   -18

  -192   300
  -160   -28
  -128   -31
   -96   -30
   -64   -27
   -32   -29
     0   -27
    32   -24
    64   -25
    96   -23
   128   -21
   160   -20
   192   -17

  -192   303
  -160   -30
  -128   -31
   -96   -27
   -64   -29
   -32   -28
     0   -24
    32   -27
    64   -25
    96   -21
   128   -22
   160   -20
   192   -17

Fri Nov 30 18:09:04 2012
  -192     0
  -160   -32
  -128   -31
   -96   -27
   -64   -30
   -32   -28
     0   -25
    32   -26
    64   -25
    96   -20
   128   -22
   160   -19
   192   -17

Fri Nov 30 18:09:29 2012
  -192     0
  -160   -28
  -128   -30
   -96   -31
   -64   -26
   -32   -28
     0   -27
    32   -25
    64   -25
    96   -23
   128   -19
   160   -20
   192   -19

  -192   301
  -160   -28
  -128   -31
   -96   -30
   -64   -27
   -32   -27
     0   -28
    32   -23
    64   -25
    96   -23
   128   -20
   160   -20
   192   -19
« Last Edit: 02 / December / 2012, 13:42:25 by lapser »
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline lapser

  • *****
  • 1093
Re: Shot Histogram Request
« Reply #29 on: 01 / December / 2012, 00:27:00 »
I found the problem in the first experiment. I corrected for the non-zero black level in each pixel.
changed:  p=get_raw_pixel(x,y);
to:
p=((get_raw_pixel(x,y)-CAM_BLACK_LEVEL)*CAM_WHITE_LEVEL)/(CAM_WHITE_LEVEL-CAM_BLACK_LEVEL);

Here's the results. Remember the right column should all be -32, and it looks like it's pretty close now. It's certainly close enough to use for setting exposure without a half shoot. The small error now is probably from truncation. I can fix that by moving the black level correction out of the pixel level loop and correcting the total sum. Correcting the histogram gets trickier, but it should be possible. That will speed up the 12,000 points loop too.
Code: [Select]
  ev+- | deltaHSUM96
  -192     0
  -160   -29
  -128   -29
   -96   -30
   -64   -32
   -32   -29
     0   -32
    32   -32
    64   -29
    96   -32
   128   -32
   160   -30
   192   -32
   
  -192   366
  -160   -32
  -128   -29
   -96   -31
   -64   -32
   -32   -30
     0   -32
    32   -32
    64   -30
    96   -32
   128   -33
   160   -30
   192   -32
   
  -192   375
  -160   -32
  -128   -30
   -96   -32
   -64   -33
   -32   -29
     0   -32
    32   -33
    64   -30
    96   -32
   128   -32
   160   -29
   192   -33
   
  -192   378
  -160   -32
  -128   -33
   -96   -29
   -64   -33
   -32   -33
     0   -29
    32   -32
    64   -32
    96   -30
   128   -32
   160   -32
   192   -30
« Last Edit: 02 / December / 2012, 13:43:31 by lapser »
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

 

Related Topics