3

I'm trying to develop object detection algorithm. I plan to compare 2 image with different focus length. One image that correct focus on the object and one image that correct focus on background.

By reading about autofocus algorithm. I think it can done with contrast detection passive autofocus algorithm. It work on light intensity on the sensor.

But I don't sure that light intensity value from the image file has the same value as from the sensor. (it not a RAW image file. a jpeg image.) Is the light intensity value in jpeg image were the same as on the sensor? Can I use it to detect focus correctness with contrast detection? Is there a better way to detect which area of image were correct focus on the image?

1
  • 1
    Can you please upload sample images?
    – G453
    Feb 28, 2014 at 8:44

2 Answers 2

1

I have tried to process the images a bit and I saw some progress. THis is what I did using opencv:

enter image description here

You can probably try to match and subtract these images using translation from matchTemplate() on the original gray images; and then assemble pieces using the convex hull of the results as initialization mask for grab cut and plugging in color images. In case you aren’t familiar with the grab cut, chceck out my answer to this question.

But may be a simpler method will work here as well. You can try to apply a strong blur to your gradient images instead of precise matching and see what the difference give you in this case. The images below demonstrate the idea when I turned the difference in the binary masks.

enter image description here enter image description here

2
  • Unfortunately that the object will shift when the focus were change then we can't matched and compare it preciously. Something maybe done by compare the brightness of these first result. I don't sure when. But I will update it result again when I have a time to try.
    – hisoft
    Mar 2, 2014 at 10:28
  • May be not precisely, but the last two images are quite OK to distinguish between blurred background and foreground.
    – Vlad
    Mar 3, 2014 at 1:32
0

It will be helpful to see your images. It I understood you correctly you try to separate background from foreground using focus (or blur) cue. Contrast in the image depends on focus but it also depend on the contrast of the target. So if the target is clouds you will never get sharp edges or high contrast. Finally jpeg image that use little compression should not affect the critical properties of your algorithm.

I would try to get a number of images at all possible focus lengths in a row and then build a graph of the contrast as a function of focal length (or even better focusing distance). The peak in this graph will give you the distance to the object regardless of object's own contrast. Note, however, that the accuracy of such visual cues goes down sharply with viewing distance.

This is what I expect you to obtain when measuring the sum of absolute gradient in a small window:

enter image description here

The next step for you will be to combine areas that are in focus with the areas that are solid color that is has no particular peak in the graph but none the less belong to the same object. Sometimes getting a convex hull of the focused areas can help to pinpoint the raw boundary of the object.

3
  • The image will look like this. object focused And background focused You understand it correctly. In this sample image I want to know area of the focused object (a can) and a background.
    – hisoft
    Mar 1, 2014 at 6:07
  • That mean I have to comparing it with all area to know which area have a correct focus and which area were not?
    – hisoft
    Mar 1, 2014 at 6:10
  • I loaded your images and compared them. Unfortunately they were shifted during refocusing so I cannot subtract their gradient to find the area that is more focused. Will come back to you with more thought/results.
    – Vlad
    Mar 1, 2014 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.