Example Code

Detect Quality of Focus on a Camera

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • Vision Development Module
  • LabVIEW

    Driver

  • NI Vision Acquisition Software

Code and Documents

Attachment

Overview

This example demonstrates how to calculate the relative edge sharpness of an image to determine the best camera focus. 

 

Description

It may be necessary in some applications to programmatically determine the quality of an image. "Quality" is a subjective term, but it is almost universally accepted that sharp focus is one characteristic of a high quality image. We can measure the sharpness of an image, at least relatively, by taking advantave of filters and convolution in NI Vision.

Basically, we are taking an image in, and then applying various convolution and filtering algorithms. These algorithms effectively differentiate the image, and highlight features in the image where there is a high change in pixel value in a small change of space. In other words, the derivative of a function which represents pixel values with respect to space will be the highest on sharp color (or intensity changes).

 

In our resulting image, the edges will show up as white, and everything else as black. We then take the mean value of the resulting image. If we pan through the various lens settings while on a constant field of view, we can surmise that the highest mean value of the set corresponds to the lens setting that provides the sharpest focus. It is important to remember that this is relative, but it can be helpful for many applications.

 

Requirements

 Software

  • LabVIEW Base Development System 2012 (or compatible)
  • NI Vision Development Module 2012 (or compatible)
  • NI IMAQdx 4.2 driver (or compatible, part of the NI Vision Acquisition Software)
  • If running in a Real-Time context, you will need the LabVIEW Real-Time Module 2012 (or compatible)

 Hardware

  • IMAQdx 4.2 compatible camera. IMAQdx supports this camera standards:
    • USB 3 Vision
    • GigE Vision
    • IEEE-1394 (FireWire)
    • USB 2.0 (DirectShow)

 

Steps to Implement or Execute Code

  1. Download and open the attached VI
  2. Run the program

 

Additional Information or References

Determine Camera Focus Quality - Front Panel.png

 

Determine Camera Focus Quality - Block Diagram.png

 

This example requires IMAQdx. If you do not have a license for IMAQdx, you can still download this example and replace the IMAQdx VIs with IMAQ VIs.

 

**The code for this example has been edited to meet the new Community Example Style Guidelines. The edited copy is marked with the text ‘NIVerified’. Read here for more information about the new Example Guidelines and Community Platform.**
Wes Pierce
Principal Engineer
Pierce Controls

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
surfnska
Member
Member
on

can you convert this to LV 8.6?

Wes_P
Member
Member
on

I added a copy that is saved in LV 8.0.  This means it should be able to be opened in versions 8.0 and higher.

Wes Pierce
Principal Engineer
Pierce Controls
TusDavid
Member
Member
on

the filter vi's are just not working for me. gettin error -1074396080 all the time

grobas
Member
Member
on

I get the same error than TusDavid, did anyone solve it?

Ajay_kumar_A_S
Member
Member
on
Which filter type gave you the best results?