Finding the difference between two images, one of them is rotated

I had such a problem. Two images are given as input. The task of the program is to find differences in these images and display the result. When two images are taken in the same way and at the same angle, the program does a great job. But, if one of the pictures is rotated by several degrees, then the result is incorrect. I am attaching a photo of the correct operation of the program and the result when one of the pictures is rotated by several degrees.


Any thoughts how I can fix that?

context: c++ - Rotated images comparing - Stack Overflow