2

I'm writing a program that find differences between images. For now, I'm finding features with AKAZE, so I've the common point of the 2 images. The problem is that these 2 images have only a part in common. How can I extract the common part from both images? For better explanation: I need to extract the common part from the first image and then from the second, so I can do absdiff for finding difference. I'm programming in c++

Thanks to all!

bjorn
  • 338
  • 6
  • 18

1 Answers1

1

You should warp the first image onto the second. You can use findHomography and perspectiveTransform functions given by the correspondence of your keypoints. You can find most of the code you need here.

Update


Incidentally, I had to do basically the same stuff today. It's tested on gray images (Mat1b), but should require only minor changes to apply to rgb images (Mat3b). Here the relevant parts of the code:

Mat1b A = imread("...");
Mat1b B = imread("...");

vector<Point2f> ptsA; 
vector<Point2f> ptsB;

// Fill ptsA, ptsB with the points given by the match of your descriptors.

Mat H = findHomography(ptsA, ptsB, CV_RANSAC); // With ransac is more robust to outliers

Mat1b warpedA;
warpPerspective(A, warpedA, H, B.size());

// Now compute diff
Mat1b res;
absdiff(warpedA, B, res);

// res is what you are looking for!
Miki
  • 40,887
  • 13
  • 123
  • 202
  • I'll give a try and let you know! – bjorn Jul 07 '15 at 07:42
  • I've tried but it does not work. How can I crop out the similar part? I've seen that `Mat()` takes as input a Rect, while with `perspectiveTransform` I get 4 point. How can I use that points to crop out image? – bjorn Jul 07 '15 at 10:04
  • @Dp89 Look at [warpPerspective](http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html?highlight=warpperspective#cv.WarpPerspective) – Miki Jul 07 '15 at 10:28
  • @Dp89 updated the answer with a piece of code, might be helpful ;D – Miki Jul 07 '15 at 18:39
  • I had the same solution by myself :P (what a coincidence!). One last question: what happens if there's no common part between two images? – bjorn Jul 08 '15 at 09:42
  • It should crash! So be sure that `ptsA` and `ptsB` size is at least 4 (minimum number of corresponding points to compute the homography). – Miki Jul 08 '15 at 09:45
  • Ok. I've asked that because in my program I can't assume that there's a common part, so I've to manage that case. Thanks a lot! – bjorn Jul 08 '15 at 19:28
  • @Miki Very nice solution! The result is B picture with black rect as intersection. But (1) is there a way to get just coordinates of this rect? Or to (2) get compound picture of A and B? – Alexander Ukhov May 01 '18 at 10:01
  • @alex I can't help you now... To get a proper answer I recommend to ask a new question with a clear problem statement – Miki May 01 '18 at 10:36