Detect changes in Focus?

I’ve been developing a 3D printed weatherproof enclosure for the Raspberry Pi HQ camera. It includes a stepper-motor driven mechanism for adjusting the lens focus and aperture remotely. The mechanism works very well, but I still have one problem left to solve and I am hoping that OpenCV might provide a solution.

I need to automatically detect and set the limits of travel so that the stepper motors don’t try to force the adjustment rings further than they can go. When you adjust the focus the entire image changes until you hit a stop, and then it will stop changing. So I’m wondering if I can use OpenCV to detect when that happens. The stepper motor limits would usually be set only when the system is powered on.

It seems to me that OpenCV would have to use the entire image to determine if the focus is changing, because the camera could be pointed at an object that’s moving. It also can’t be fooled so that it keeps operating the motors if the camera is operating in total darkness, or if the lens cap is on.

Could this be done relatively easily? If so, can you point to some resources that will help me implement this? I don’t have much experience with OpenCV, but I can usually figure things out if I get started on the right path.

BTW, next step after this will be figuring out how to use OpenCV to autofocus the camera.

stepper: that better be a very weak stepper or else it’s eventually gonna rip things apart. perhaps introduce a defined breaking point somewhere, or slippage of belts, or some friction coupling.

as for assessing the sharpness of a picture: “laplacian pyramid”.

you want to look at the frequency domain of the picture, assess how much “energy” is in the highest frequency range, also adjacent ones (in case you’re too far away from sharp focus).

I’d call this easy and it should work well for pictures of a decently lit focusable scene with texture (blank white walls do not count).

it’s gonna be hard/tricky if you expect it to work in darkness and under a moving scene. you need to take care to recognize when assessing focus is impossible.

this will only allow you to keep something in focus or move towards focus. it WILL NOT give you ANY information about the physical limits of your focus dial (that you must not run over with a stepper if breaking things is to be avoided).

to be very explicit: you should absolutely expect your stepper to rip this to shreds the moment you least expect it, unless the whole mechanics is designed to make that impossible. I’d very much recommend a friction coupling. needn’t be “mechanical engineering” grade. if the motor shaft is round and smooth and has no key/flat, and you have some plastic part sitting on it, with no grub screw digging into the shaft, that metal-plastic interface should have appropriate friction. some clamping from the plastic part ought to be good for adjusting the friction.

I have quite a lot of experience with using and building CNC machines. I’m using very small and cheap 28BYJ-48 stepper motors that don’t have much torque.

The gearing that I’m currently using does allow them to apply a fairly large amount of torque to the adjustment rings. If necessary, I can adjust the gear ratio so the motors “slip” (lose steps) more easily when they hit a stop. This would limit the forces on the adjustment rings even more.

An alternative to OpenCV, would be to use an INA219 current sensor. A sharp increase in current draw would indicate that a travel limit has been reached. The motor would then be quickly stopped and a limit set.

But using an INA219 adds complexity and I’m trying to design this project so that others can easily build it. If I can avoid using one, then the entire cost, besides the camera and 3D printed parts, will be about $12-$15.

The camera is part of a robotic rover that will eventually use computer vision and machine learning extensively, so it seems like it would make sense to use OpenCV to set the stepper motor limits, if I can.

1 Like