# [SciPy-User] Help optimizing an algorithm

Charles R Harris charlesr.harris@gmail....
Fri Feb 1 21:23:32 CST 2013

```On Wed, Jan 30, 2013 at 10:29 AM, Chris Weisiger <cweisiger@msg.ucsf.edu>wrote:

> We have a camera at our lab that has a nonlinear (but monotonic) response
> to light. I'm attempting to linearize the data output by the camera. I'm
> doing this by sampling the response curve of the camera, generating a
> linear fit of the sample, and mapping new data to the linear fit by way of
> the sample. In other words, we have the following functions:
>

One thing that I have seen with some CCD cameras is that the non-linearity
is in the photon flux to current conversion, which means that the output
from a fixed light source scales linearly with exposure time and the slopes
scale non-linearly with focal plane irradiance. I don't know if that is the
case for CMOS cameras but would be happy to find out ;)

> f(x): the response curve of the camera (maps photon intensity to reported
> counts by the camera)
> g(x): an approximation of f(x), composed of line segments
> h(x): a linear fit of g(x)
>
> We get a new pixel value Y in -- this is counts reported by the camera. We
> invert g() to get the approximate photon intensity for that many counts.
> And then we plug that photon intensity into the linear fit.
>
> Right now I believe I have a working algorithm, but it's very slow (which
> in turn makes testing for validity slow), largely because inverting g()
> involves iterating over each datapoint in the approximation to find the two
> that bracket Y so that I can linearly interpolate between them. Having to
> iterate over every pixel in the image in Python isn't doing me any favors
> either; we typically deal with 528x512 images so that's 270k iterations per
> image.
>

Try 4K x 4K. It helps if the data accessed in inner loops is laid out
contiguously in memory.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20130201/dd54a3fd/attachment.html
```