How to detect focus using HQ cam

Raspberry Pi cameras (V1, V2, wide angle etc), HDMI capture modules etc.
Post Reply
Posts: 11
Joined: Fri Mar 19, 2021 5:16 pm

How to detect focus using HQ cam

Post by mr.burns »


I am stereo photographer and currently working with a camera pair of two Ixus860 to take stereo images.
I am only interested in taking images no movies. The images later on I view with this kind of viewer:


Since this camera pair is getting older I wanted to replace it with StereoPI V2. I am a backer of kickstarter campaign and ordered the camera kit which comes with a small LCD screen. I want to create a mobile cam, battery based to carry with me.
For stereo images it is indispensable to get photos with a very large depth of field to get sharpness from foreground to background in the viewer.
Therefore I need lenses with aperture of 8,11,22 to get the highest DoF (Depth of Field) possible.
Currently only for RaspPI HQ cam lenses with reasonable apertures are provided. Other cams like RaspPi cam V1/V2 have unreasonable apertures.
The problem with lenses for HQ cam is they don't have autofocus and I have to set focus manually. And even worse for stereo cam I have two lenses side by side and I need to manually set focus twice. Using my 3D printer I can create some gears and attach them to the lenses. With one gear in the middle of both lenses I can turn one wheel and both lenses will follow. So the mechanical part for focusing I think I can handle.
The problem for me is how can I detect if the focus is set onto a specific object in my scene?
The small screen that comes with the StereoPI kit isn't reasonable to judge focus very well.
Is there any software solution available? I can think of professional cameras where sharp edges appear in a different color (so called focus peaking) so one can detect focus very quickly. Is this possible with StereoPI V2?

Some other thoughts:
Since the form factor of HQ cam is very big compared to V1/V2, is it possible to attach some kind of aperture to V1/V2 camera?
I am not a expert in optics, but may it is possible to attach a small round metal sheet with a small hole in the middle at the front of the V1/V2 lense to get a aperture similar to 8, 11, 22? Can that work?

Posts: 215
Joined: Tue May 21, 2019 12:33 pm
Location: France

Re: How to detect focus using HQ cam

Post by stereomaton »

Did you test with the V2 cameras?
Actually, the depth of field is also related to the physical size of the sensor so that your reasoning is not completely right. You'll see that V2 has a large depth of field suitable for stereo photography. The image quality may or may not be good for you though.

For detecting the sharpest parts, you need to do image processing, which requires to code, for example using the opencv image for stereopi (which removes the convenient interface of SLP). I guess that you could start with edge detection (Laplacian, difference of gaussians, ...) and show highest magnitudes, but you better see what is done in autofocus example for arducam cameras for example
Stereophotographer and hacker
Despite my quite active participation in the forum, I am not in the StereoPi team
StereoPi (v1) Standard Edition + CM3Lite module + a few cameras

User avatar
Site Admin
Posts: 884
Joined: Tue Apr 16, 2019 9:23 am

Re: How to detect focus using HQ cam

Post by Realizator »

Hi, mr.burns and Stereomaton,

I didn't find any easy solution to detect the focus right now. I usually use the PC browser with the camera live view size big enough to set the focus for both lenses of HQ cams and fix them in this position.

My only experiment a year ago with real-time "focus quality detection" was based on the JPG compression feature. You see, the more sharp your image (i.e., the better focus you have), the bigger the JPEG image you get. My simple python code captured JPEG images from the camera and showed you the size in kilobytes. Also, I cut the center of the image to check the focus here, but not for the whole picture.
So while adjusting the focus, you can see the size is either growing (i.e., your adjustment is in the correct direction) or decreasing (i.e., you are "unfocusing" it). Finally, I didn't accept this approach since, at some points, I got "jumps" in size - maybe the JPEG compression algorithm has some specific quality settings under the hood.
Eugene a.k.a. Realizator

Post Reply