You might want to look for things related to "OpenCV".
OpenCV is an image processing library that is designed to do things like you ask and much more. It's complicated and not easy to use.
I used this around 3 years ago to locate the position of sub images within an image. I don't remember exactly what I did. I may have uploaded some examples with source, but being several years old they are probably almost worthless.
A member created a library several years ago to make using OpenCV easier. I don't know the state of this library and have never used it, but you may find it helpful.