I have two images for the same object from the almost same perspective (no more than 1-2 degrees or 1-2 mm is acceptable). I want to change the perspective of one of them to match exactly the other so I can make subtraction between them successfully to get a complete black result if there is no change of the content of the object. many thanks
I think all these answers fail to provide a descent methodology.
You should consider "the closeness" of the images as the availability of the initial pose and cast this as a registration problem. The solution can typically be obtained through a least-squares adjustment, i.e., by minimizing the distances of the model points (from first image) to their corresponding image points. In case of a perspective distortion, you could optimize the 9 parameters of the homography matrix. Methods like Lukas Kanade, ESM or hyperplane tracking are some ways to do that. In essence, the procedure is similar to ICP, where an iterative procedure is applied.
Note that, to begin with, one could always resort to a simple approach, where you try different combinations of rigid motion $[-2,2]$ degrees for rotation and $[-5,5]$ pixels for translation. Then transform your template with all possible poses (in this case ~$40-50$). Then interpolate the sum of squared differences or normalized cross correlation scores in the pose space (6DOF freedom or 9DOF homography) to get a subpixel accurate refinement. This should give you a rough yet a better alignment. Keep in mind that you could also use this scheme iteratively.
If you have absolutely no clue on how to do any of these, check out this link.
What @creator means that it is not possible. You have two images say matrix A and B. You want to calculate say C=B-A so that A-B+C=0. If you do not have a model to go from A to C how can you find C? In other words you need B to get A-B if you do not know or do not have model to go from A to B, you cannot.