This could be done using Vision Builder for Automated Inspection. If you are interested in it I would suggest looking at the product page found here. You can use this software to implement the algorithm you described in your post.
Cameron T Applications Engineer National Instruments