Read pixel values from script - Script Writing - CHDK Forum

Read pixel values from script

  • 7 Replies
  • 7035 Views
Read pixel values from script
« on: 02 / November / 2009, 14:37:21 »
Advertisements
Hi,

I'd like to read pixel values (before any compression is applied, i.e. 10-bit values from the sensor) and show it's values (R G B) in the LCD screen.  Is it possible to read a pixel value at position x,y?

Thanks.

*

Offline fudgey

  • *****
  • 1705
  • a570is
Re: Read pixel values from script
« Reply #1 on: 02 / November / 2009, 14:59:10 »
It's possible, but you'll have to modify CHDK source code and build a custom version for your purposes. The easiest way may be to modify core/shot_histogram.c since that's already tied to Lua and accesses the RAW buffer after each shot.

Note that you can't read the sensor without shooting a photo (CHDK doesn't handle the sensor at all, it waits for the camera to do that and reads the RAW image from RAM). If you need something more real time, you could take a look at the histogram code or zebra (or motion detector if you want to use scripts) i.e. use the LCD viewfinder image.

Also note that sensor RAW is most definitely not equal to the JPEG before lossy compression. It lacks a huge amount of image processing, meaning those values you will be getting are far from the colors you see in a JPEG. There really isn't even such thing as an RGB value of pixel position [x,y] for the RAW image because the sensor is a Bayer matrix of single color pixels (50% of them green, 25% red, 25% blue).

Re: Read pixel values from script
« Reply #2 on: 02 / November / 2009, 15:05:34 »
fudgey,

Thanks for the quick reply.  Two questions:

1. Is there a way the code in shot_histogram.c (I checked it yesterday...)  can access variables defined via he UI... i.e. I need to tell that code the x,y position of interest.

2. Is there an example to create a function to access the pixel and expose it to uBasic?

rgs.

*

Offline reyalp

  • ******
  • 14125
Re: Read pixel values from script
« Reply #3 on: 02 / November / 2009, 17:03:24 »
2. Is there an example to create a function to access the pixel and expose it to uBasic?
As fudgey suggested, you use the shot_histogram code as a starting point. You will have to implement it in the CHDK C code yourself, or convince someone else to.

Since the script itself doesn't run in the raw hook, you have to put C code there (core/raw.c) to read the pixels you are interested in, store them somewhere (a global variable is fine, especially for a quick personal hack), and then make another function to retrieve the values in script.

I would personally use lua for this kind of thing over ubasic, but there's no reason you couldn't implement it in ubasic.
Don't forget what the H stands for.

Re: Read pixel values from script
« Reply #4 on: 02 / November / 2009, 17:24:46 »
a global variable is fine, especially for a quick personal hack

Just the slightest hint there that there are better ways, what did you have in mind  ?

*

Offline reyalp

  • ******
  • 14125
Re: Read pixel values from script
« Reply #5 on: 02 / November / 2009, 18:11:24 »
Just the slightest hint there that there are better ways, what did you have in mind  ?
I would put it all in a file like shot_historgram with functions to capture the pixel values and read them back to script.
All the variables would be file scope statics.

If it was expected to deal with more than a few pixels, the memory pixel values would be dynamically allocated, again like shot_histogram (malloc when feature is requested, free'd when no longer needed).

Reading the OP a little more carefully, he wants to display them in the GUI, not use them in script. Same thing, you just read the values back from one of the gui draw functions. Again, if this was for something more than a quick hack, you'd want a customizable OSD element like all the others.
Don't forget what the H stands for.

Re: Read pixel values from script
« Reply #6 on: 03 / November / 2009, 05:34:42 »
malloc when feature is requested, free'd when no longer needed

This is something I am not clear about ..
Does malloc reserve memory in the memory cache and a copy in regular memory and write to both ?
If so, can the cached memory be 'out-of-date' and how would you know ?
When would you use uncached memory allocation ?

*

Offline reyalp

  • ******
  • 14125
Re: Read pixel values from script
« Reply #7 on: 03 / November / 2009, 16:28:15 »
This is something I am not clear about ..
Does malloc reserve memory in the memory cache and a copy in regular memory and write to both ?
If so, can the cached memory be 'out-of-date' and how would you know ?
When would you use uncached memory allocation ?

I've to the caching aspect here: http://chdk.setepontos.com/index.php/topic,1910.msg42829.html#msg42829

The reason to use malloc/free at all (assuming you were dealing with a substantial number of pixels) is simply to save RAM when that feature is not in use.
Don't forget what the H stands for.

 

Related Topics


SimplePortal © 2008-2014, SimplePortal