color analysis script

  • 13 Replies
  • 1669 Views
color analysis script
« on: 04 / October / 2013, 08:39:27 »
Advertisements
Hi everybody,
I am currently trying to find a way to analyze the color of pictures directly on a camera. What I would need is that the cam displays the average color intensities per pixel in the red, blue, and green channels. Since I have no experience with CDHK yet, I wanted to ask the experts in this forum, if writing such a script is feasible and if somebody without prior knowledge (just a bit of programming experience in C++ but that's already a while back) can realistically do it on a reasonable time scale.
Also, would it be necessary to use RAW images, i.e. do you know how color intensities are changed when the picture is transformed from RAW -> JPG?`
Please excuse my lack of knowledge; I am very grateful for any answers.
Thank you,
Radlo

*

Offline reyalp

  • ******
  • 11084
Re: color analysis script
« Reply #1 on: 04 / October / 2013, 16:43:10 »
Hi everybody,
I am currently trying to find a way to analyze the color of pictures directly on a camera. What I would need is that the cam displays the average color intensities per pixel in the red, blue, and green channels
. Since I have no experience with CDHK yet, I wanted to ask the experts in this forum, if writing such a script is feasible and if somebody without prior knowledge (just a bit of programming experience in C++ but that's already a while back) can realistically do it on a reasonable time scale.
There really isn't a way to do this from script at the moment. You could do it with C code in CHDK.

If you want to do this with files that are saved on the camera, you would most likely need to use raw, we don't have a way to use the jpeg decoder to load an image (you could technically write your own software jpeg loader or port an existing one, but it would be slow and there is very little memory available to chdk) If you are OK with just using the much lower resolution YUV data shown on the screen (as CHDK histogram does), you could do that.

If you want to do it in the shooting process, you can access the raw data before it is saved. You can also access the display data.

http://chdk.wikia.com/wiki/Frame_buffers has some information.

Quote
Also, would it be necessary to use RAW images, i.e. do you know how color intensities are changed when the picture is transformed from RAW -> JPG?`
This is a quite complicated subject. CHDK DNG does contain most of the information needed to render something like a camera jpeg, but the quality of calibration varies a lot.
Don't forget what the H stands for.

Re: color analysis script
« Reply #2 on: 04 / October / 2013, 19:38:51 »
I am currently trying to find a way to analyze the color of pictures directly on a camera. What I would need is that the cam displays the average color intensities per pixel in the red, blue, and green channels.
It would be interesting to know what you are trying to do.  If you want to display the average color intensities PER PIXEL ... well,  there are millions of pixels in even the least expensive Canon P&S camera.  The camera's little LCD display will certainly be crowded.

With a better idea of your intended application,  we might be able to offer more specific help.
Ported :   A1200    SD940   G10    Powershot N    G16

Re: color analysis script
« Reply #3 on: 05 / October / 2013, 06:01:00 »
sorry, I wasn't clear in my first post. I would like to use the camera to analyze the color of objects. But I don't need to know the color of each pixel. The idea is that you take a close-up picture of the object and the camera tells you the average intensity in the red, blue, and green channel. For example, if you take a picture of something red, it would just tell you RGB = (200, 10, 20). So it would be completely fine if it tells you this value during the shooting process, it does not need to work on saved images.


Re: color analysis script
« Reply #4 on: 05 / October / 2013, 06:11:12 »
Checkout the readYUV  uBasic command supported by SDM :-

http://stereo.jpn.org/eng/sdm/ubasic.htm

David

*

Offline Davo

  • ***
  • 144
Re: color analysis script
« Reply #5 on: 05 / October / 2013, 06:22:15 »
I think you need to restrict your color area of interest to a small section of the screen. Otherwise, a picture of a black ball on a white floor will give results for grey.

I can imagine turning your camera into a live spot color meter by averaging the color values of the pixels in a small square in the centre of the LCD. Check up how the histogram is coded - it must do something similar I guess.
« Last Edit: 05 / October / 2013, 06:24:07 by Davo »

Re: color analysis script
« Reply #6 on: 05 / October / 2013, 06:25:43 »
I think you need to restrict your color area of interest to a small section of the screen.

I can imagine turning your camera into a live spot color meter

That is exactly what the SDM readYUV command does.

*

Offline Davo

  • ***
  • 144
Re: color analysis script
« Reply #7 on: 05 / October / 2013, 07:28:51 »
I think you need to restrict your color area of interest to a small section of the screen.

I can imagine turning your camera into a live spot color meter

That is exactly what the SDM readYUV command does.

My bad - I had never heard of that command.

Anyhoos, I forgot to mention that I recently started CHDK scripting from a C# background. I found Lua to be really easy to pick up and NotePad++ is great for writing the code.


Re: color analysis script
« Reply #8 on: 05 / October / 2013, 08:14:14 »
That is exactly what the SDM readYUV command does.
My bad - I had never heard of that command.
Not "your bad" at all. You've never heard of it because its not a script function in CHDK.  It's actually one of the many 'special' functions added to the SDM package's version of uBASIC.    You can do exactly the same thing in CHDK with the md_get_cell_val() function,  which calls the same underlying code as the SDM readYUV function.  However, the use of md_get_cell val() gives you a lot more flexability in what and where the "spot meter" actually measures  and it is available in both Lua and uBASIC versions.

Quote
Anyhoos, I forgot to mention that I recently started CHDK scripting from a C# background. I found Lua to be really easy to pick up and NotePad++ is great for writing the code.
I think you will find that writing scripts in uBASIC becomes painful once you're familiar with  Lua.  If you haven't seen it already, you wil probably find this handy : http://chdk.wikia.com/wiki/CHDK_Scripting_Cross_Reference_Page
Ported :   A1200    SD940   G10    Powershot N    G16

Re: color analysis script
« Reply #9 on: 05 / October / 2013, 18:36:29 »
Here's a simple Lua script that demonstrates using the motion detection buffer to do RGB analysis.

The script takes two parameter representing the number of measurement zones you want to use.  You move the meter point (shown by with a blue box) around with the arrow keys on the camera's function dial.  RGB values are displayed on the screen in real time.  With a slight modification, the script could show YUV instead.



Code: [Select]
--[[
Spot Meter v1.1
@title SpotMeter1.1
@param a Columns
  @default a 5
@param b md_rows
  @default b 5
--]]

require("drawings")

function restore()
    set_console_autoredraw(1)
    print("done")
end

function meter(mode,x,y)  -- measure mode Y=1, U=0, V=2, R=3, G=4, B=5
    md_detect_motion( md_cols, md_rows, mode, 100, 100, 250, 0, 0, 0, 0, 0, 0, 0, 0, 1, 10)
    return(md_get_cell_val(x,y))
end

set_console_autoredraw(0)
cls()

md_cols = a
md_rows = b
screenwidth=360
screenheight=240
md_colsize=screenwidth/md_cols
md_rowsize=screenheight/md_rows

box_X = (md_cols+1)/2
box_Y = (md_rows+1)/2
box = draw.add("rect", (box_X-1)*md_colsize, (box_Y-1)*md_rowsize, (box_X-1)*md_colsize+md_colsize, (box_Y-1)*md_rowsize+md_rowsize, "blue", 2)
txtXpos =  (screenwidth/2)-35
txtYpos =  screenheight-35
rvalue=draw.add("string", txtXpos,      txtYpos, "000",   "black", "red")
gvalue=draw.add("string", txtXpos+30,   txtYpos, "000",   "black", "green")
bvalue=draw.add("string", txtXpos+60,   txtYpos, "000",   "black", "blue")


if ( get_mode() == false ) then   -- switch to shooting mode
    sleep(1000)
    set_record(1)
    while ( get_mode() == false) do sleep(100) end
end

repeat
   i=9
   repeat
      i=i+1
      if (i >= 10) then
          i=0
          red   = meter(3,box_X,box_Y)
          green = meter(4,box_X,box_Y)
          blue  = meter(5,box_X,box_Y)
          draw.replace(rvalue,"string", txtXpos,      txtYpos, string.format("%03d",red),    "white", "red")
          draw.replace(gvalue,"string", txtXpos+30,   txtYpos, string.format("%03d",green),  "white", "green")
          draw.replace(bvalue,"string", txtXpos+60,   txtYpos, string.format("%03d",blue),   "white", "blue")
      end
      draw.overdraw()
      wait_click(100)
   until not( is_key("no_key"))

   if     ( is_key("left")  and (box_X>1))    then box_X=box_X-1
   elseif ( is_key("right") and (box_X<md_cols)) then box_X=box_X+1
   elseif ( is_key("up")    and (box_Y>1))    then box_Y=box_Y-1
   elseif ( is_key("down")  and (box_Y<md_rows)) then box_Y=box_Y+1
   elseif ( is_key("set")) then
     box_X = (md_cols+1)/2
     box_Y = (md_rows+1)/2
   end
   draw.replace(box,"rect", (box_X-1)*md_colsize, (box_Y-1)*md_rowsize, (box_X-1)*md_colsize+md_colsize, (box_Y-1)*md_rowsize+md_rowsize, "blue", 2)
   draw.redraw()
until ( is_key("menu") )
restore()

Pressing Set moves the meter box to the screen center.  Pressing Menu makes the script exit.
Ported :   A1200    SD940   G10    Powershot N    G16

 

Related Topics