Author Topic: Determining focus distance ...  (Read 504 times)

Offline rhameed

  • Rookie
  • *
  • Posts: 11
Determining focus distance ...
« on: 30 / November / 2011, 13:58:22 »
I am new to the CHDK and looking for some information. Is it possible using the CHDK hacks for Canon DSLRs and/or magic lantern?, it is possible to implement the following function (in line view mode):

1. Move the focus box to all allowed positions one-by-one
2. At each position, perform autofocus and then read the distance information from the lens (assuming the lens supports it)
3. At the end dump this list of distances

I can dive into the hacks to implement the functionality, but first want to understand if we have enough control to implement something of this nature
« Last Edit: 30 / November / 2011, 14:00:58 by rhameed »

Offline eduperez

  • Sr. Member
  • ****
  • Posts: 257
Re: Determining focus distance ...
« Reply #1 on: 30 / November / 2011, 20:11:54 »
Reporting from the 400D hack (I have absolutely no idea about other cameras, by the way): our camera has no live-view or focus-by-contrast (only phase-detection), so you would be limited to the 9 focusing points using your technique; I guess this is not useful at all...

On a side note, if I wanted to create a depth map, I would try the following:

* Use a ML-enabled camera.
* Create a focus-stack, for all possible focusing distances.
* Use "enfuse" to determine which pixels are in-focus on each image, and guess its depth from there.

Just my two cents.

Offline rhameed

  • Rookie
  • *
  • Posts: 11
Re: Determining focus distance ...
« Reply #2 on: 01 / December / 2011, 10:56:19 »
Thanks for your input!. The method that you have described would be my first choice but I didn't know that there is enough support to do that.

I assume when you say ML-enabled, you are referring to cameras supported by Magic Lantern (excuse my ignorance :)). And in this case something like a T2i should do the job, right?

Offline eduperez

  • Sr. Member
  • ****
  • Posts: 257
Re: Determining focus distance ...
« Reply #3 on: 01 / December / 2011, 11:45:05 »
Thanks for your input!. The method that you have described would be my first choice but I didn't know that there is enough support to do that.

I assume when you say ML-enabled, you are referring to cameras supported by Magic Lantern (excuse my ignorance :)). And in this case something like a T2i should do the job, right?

Well, I do not know about anybody else that has done this before, so I would not call it "supported"... after all, you will be using something that was designed for macro photography to do something completely different; but I do not see why it wouldn't work.

And yes, with ML-enabled I was referring to any camera with Magic Lantern installed; in principle, a T2i should do, all you need is the "Stack Focus" functionality. I would be more concerned about the lens, however: first, because you will want to have the smallest depth of field possible; but also because not all lenses send the focusing distance to the camera (I think you could still make a "relative depth map", but not an "absolute depth map").

Then comes the software part: enfuse can merge stacks, and can export the masks it uses internally; in theory, with the masks and the focusing distance of each file (it can be obtained from the EXIF information) you have all you need to do a depth field.

Give me a couple of days, and I will try to make a quick test at home.

Offline rhameed

  • Rookie
  • *
  • Posts: 11
Re: Determining focus distance ...
« Reply #4 on: 01 / December / 2011, 14:36:49 »
Yes the lens part is important - based on the research that I did most lenses report distance at a fairly coarse resolution (the whole focus range might be divided into as low as 7-10 'distance bins'). And the resolution is specially worse at longer distances. The higher-end lenses seem to be able to report at a finer scale. Someone at exiftool forum was able to give me the exact focus distances steps for the EF-S 17-55mm lenses which seems to have a good enough resolution for my purposes. And yes I don't need an exact depth map, a sort of relative one with coarse distance information is fine.

I looked up a bit on focus stacking using magic lantern and it is not clear to me that what is granularity of a focus step during focus stacking . In theory it may not even be required to use the distance information from the lens. The focus step count is in itself a measure of the distance if calibrated properly.

Offline eduperez

  • Sr. Member
  • ****
  • Posts: 257
Re: Determining focus distance ...
« Reply #5 on: 01 / December / 2011, 15:07:09 »
Yes the lens part is important - based on the research that I did most lenses report distance at a fairly coarse resolution (the whole focus range might be divided into as low as 7-10 'distance bins'). And the resolution is specially worse at longer distances. The higher-end lenses seem to be able to report at a finer scale. Someone at exiftool forum was able to give me the exact focus distances steps for the EF-S 17-55mm lenses which seems to have a good enough resolution for my purposes. And yes I don't need an exact depth map, a sort of relative one with coarse distance information is fine.

I looked up a bit on focus stacking using magic lantern and it is not clear to me that what is granularity of a focus step during focus stacking . In theory it may not even be required to use the distance information from the lens. The focus step count is in itself a measure of the distance if calibrated properly.

The 17-55 is a 2.8 lens, and I guess you will be using it on the long end of the zoom. Have you considered the 50mm f/1.8 instead? Much cheaper, almost the same focal length, and a much wider aperture; the depth of field is very narrow with this lens (I plan to do my tests with this it, by the way).

Offline rhameed

  • Rookie
  • *
  • Posts: 11
Re: Determining focus distance ...
« Reply #6 on: 01 / December / 2011, 15:51:14 »
While you are right about the narrower DOF of the 50mm 1.8 lens, the issue is that it is not likely to report the focus distance at a fine enough resolution. From what I have learnt the focus distance is determined by little sensors placed on various points on the focus ring and cheaper lenses tend to have fewer of these sensors and thus less precise distance info. Even though as you said the shallower DOF of the prime should make it easier to judge in-fcous versus out-of-focus areas at any given focus distance.

Offline eduperez

  • Sr. Member
  • ****
  • Posts: 257
Re: Determining focus distance ...
« Reply #7 on: 01 / December / 2011, 16:31:07 »
Sorry, I misunderstood you where going to use the position in the stack to determine the distance.

Offline a1ex

  • Hero Member
  • *****
  • Posts: 581
  • ML dev
    • Magic Lantern for 550D, 60D, 600D...
Re: Determining focus distance ...
« Reply #8 on: 01 / December / 2011, 17:05:20 »
You want to extract depth information from some static scene, right?

As suggested before, focus stacking from Magic Lantern may help. The smallest focus step is the lowest focus increment in EOS utility, in LiveView (this is very small, usually the focus change is barely noticeable, even with 50/1.8 lens).

You can also write down the focus distance (as reported by the lens) and interpolate it using raw focus steps, to get a better resolution. The relationship between raw focus steps and physical distance is nonlinear.

CHDK Forum

Re: Determining focus distance ...
« Reply #8 on: 01 / December / 2011, 17:05:20 »

Offline Microfunguy

  • Developers
  • Guru Member
  • ****
  • Posts: 3027
    • StereoData Maker
Re: Determining focus distance ...
« Reply #9 on: 02 / December / 2011, 00:44:22 »
Is the scene static ?

I am sure you will be aware of many of the papers published regarding depth maps from a single camera.

Here is one http://vision.ai.uiuc.edu/?p=320

Offline rhameed

  • Rookie
  • *
  • Posts: 11
Re: Determining focus distance ...
« Reply #10 on: 02 / December / 2011, 03:33:07 »
You want to extract depth information from some static scene, right?

As suggested before, focus stacking from Magic Lantern may help. The smallest focus step is the lowest focus increment in EOS utility, in LiveView (this is very small, usually the focus change is barely noticeable, even with 50/1.8 lens).

You can also write down the focus distance (as reported by the lens) and interpolate it using raw focus steps, to get a better resolution. The relationship between raw focus steps and physical distance is nonlinear.

Thanks for other good ideas. and yes it is going to be depth information for a static scene.

Using a combination of focus step count as well as distance information can be a good approach.

Also Its true that relationship between focus steps and focus distance is non-linear. However *IF* the focus steps correspond to constant increments in lens-to-sensor distance then we know that the relationship is given by  the lens equation 1/u + 1/v = 1/f (obviously DSLR lenses being complex multi-element designs don't follow this exactly but it should work fairly well for non-macro distances)
 


Offline rhameed

  • Rookie
  • *
  • Posts: 11
Re: Determining focus distance ...
« Reply #11 on: 02 / December / 2011, 03:47:30 »
Is the scene static ?

I am sure you will be aware of many of the papers published regarding depth maps from a single camera.

Here is one http://vision.ai.uiuc.edu/?p=320


This work is interesting. However it is still based on the principle of stereo matching and requires extra hardware support to capture multiple images with different views of the scene using the same camera. Unfortunately I don't have access to that hardware :). Also as I said, the aim is not to create a highly detailed and precise depth map. The coarse depth estimate from the camera is going to be just one of many inputs which my system will use to understand the scene.

Offline eduperez

  • Sr. Member
  • ****
  • Posts: 257
Re: Determining focus distance ...
« Reply #12 on: 19 / December / 2011, 03:13:06 »
Here we go...

http://dl.dropbox.com/u/14091554/DepthMap.zip

I took a series of photographs with a 400D, using a 50mm at f/1.8, and moving the focus ring by hand between each shot. I downloaded the images to the computer, and extracted the embedded JPEG in the RAW files; the used the following command to merge the images:

Code: [Select]
enfuse -o result.jpg --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --save-masks *.jpg
(the resulting image leaves a lot to be desired, probably a good example of how bad enfuse may work sometimes)

Mask files were unreadable by almost any software I tried, and decided to convert them to JPEG:

Code: [Select]
for FILE in *.tif; do convert $FILE $FILE.jpg; done
Then I finally opened all files into GIMP as layers, changed the levels of each layer, so each one was a bit darker that the previous one, and put them all into "Addition" mode. The final result resembles what I would expect as a depth map, but reflects the bad job made by enfuse.

Hope this helps.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal