There are many ways to achieve this. You could use an image based, direct similarity metric such as Sum of Absolute Distances, Sum of Squared Distances or Normalized Cross Correlation. For a general overview look here. These approaches generalize to
template matching where a convolutional procedure is conducted, such as the one here.
Template matching methods could tolerate rigid transformations (rotation, translation) and scaling. However, for the case of affine deformations (e.g. the second image set in the question), they would not suffice. It is then better to benefit from feature based matching techniques, where some repeatable keypoints are first extracted from the image, and invariant descriptors of the local neighborhoods are computed and store. This is done for both images. Finally the descriptor are compared using a nearest neighbor search. SIFT is one of those robust descriptors.
Note that, while in the first scenario, the template transformation can be defined simply by a translation, the second case is not like that. You should either solve for affine parameters, or compute a general perspective homography to represent the pose of the second image w.r.t the first one.
The problem is very general, and often times analyzed under the roof of object detection. You could review this literature and choose a method suited to your problems. OpenCV implements most of the techniques, I described in this answer.