# Image difference after Image registration and alignment

**URL:** <https://forum.opencv.org/t/image-difference-after-image-registration-and-alignment/10553>\
**Category:** Python\
**Created:** [October 14, 2022, 9:50am UTC](https://forum.opencv.org/t/image-difference-after-image-registration-and-alignment/10553 "2022-10-14T09:50:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanu](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@Tanu](https://forum.opencv.org/u/Tanu)\
**Post date:** [October 14, 2022, 9:50am UTC](https://forum.opencv.org/t/image-difference-after-image-registration-and-alignment/10553/1 "2022-10-14T09:50:25Z")

</div>

I am aligning two images using opencv and numpy in Python using the logic from below link .

> **[Feature Based Image Alignment using OpenCV (C++/Python)](https://learnopencv.com/image-alignment-feature-based-using-opencv-c-python/)**
>
> Tutorial on feature-based image alignment using OpenCV. Also learn keypoint detection/matching, Homography & image warping. C++/Python code is shared for study.

I am able to get the alignment working but when I try to find image differences using absDiff all objects are shown as different even though there are only few differences.

 ![Image1](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/f2127d2015e5ff90663b7a546bc6a60828823157.jpeg)  
 ![Image2](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/377f374cfca19f1ce997e2275df7a71cf6ef94c1.jpeg)  
 ![AlignedImage](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/fe12d09ef016f5beb371bbcff6bb118e734ec878.jpeg)

Can someone please help me find the reason ?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [October 14, 2022, 1:25pm UTC](https://forum.opencv.org/t/image-difference-after-image-registration-and-alignment/10553/2 "2022-10-14T13:25:46Z")

</div>

code and output too please.

---

<div class="post-metadata">

**Author:** ![Tanu](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@Tanu](https://forum.opencv.org/u/Tanu)\
**Post date:** [October 14, 2022, 5:00pm UTC](https://forum.opencv.org/t/image-difference-after-image-registration-and-alignment/10553/3 "2022-10-14T17:00:30Z")

</div>

```auto
im1 = cv2.imread("Image1.png")
im2 = cv2.imread("Image2.png")
	
im1Gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
im2Gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)

orb = cv2.ORB_create(MAX_FEATURES)
keypoints1, descriptors1 = orb.detectAndCompute(im1Gray, None)
keypoints2, descriptors2 = orb.detectAndCompute(im2Gray, None)

matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
matches = matcher.match(descriptors1, descriptors2, None)

matches = sorted(matches, key=lambda x: x.distance)

numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
matches = matches[:numGoodMatches]

imMatches = cv2.drawMatches(im1, keypoints1, im2, keypoints2, matches, None)
cv2.imwrite("matches.jpg", imMatches)

points1 = np.zeros((len(matches), 2), dtype=np.float32)
points2 = np.zeros((len(matches), 2), dtype=np.float32)

for i, match in enumerate(matches):
  points1[i, :] = keypoints1[match.queryIdx].pt
  points2[i, :] = keypoints2[match.trainIdx].pt

h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)

height, width, channels = im2.shape
im1Reg = cv2.warpPerspective(im1, h, (width, height))

cv2.imwrite("AlignedImage.png", im1Reg);

```

```auto
# **Code to find difference between images**

imageA = cv2.imread("AlignedPython.png")
imageB = cv2.imread("Image1.png")

grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

diff = cv2.absdiff(grayA, grayB)

thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

for c in cnts:
  (x, y, w, h) = cv2.boundingRect(c);
  cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)
  cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)
	
cv2.imwrite("bailey-Difference.png", imageA)

```

 ![bailey-Difference](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/6b854077ed8079867c776f38bcc19a179c9afe38.jpeg)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [October 14, 2022, 10:42pm UTC](https://forum.opencv.org/t/image-difference-after-image-registration-and-alignment/10553/4 "2022-10-14T22:42:10Z")

</div>

ok so the result isn’t _subpixel exact_. that is to be expected. you’ve got a PDF drawn on a screen at unknown and arbitrary scale, and translation.

have you ever just displayed the difference image? what magnitudes do you see?

otsu will find something if there is _anything at all_. you should work with fixed thresholds here.

consider using morphology operations.

consider swapping `findContours` for `connectedComponentsWithStats`. that’ll give you bounding boxes directly, and you can use the label map to colorize gently.

you use `warpPerspective` without a specific interpolation mode, so you’re getting the cheapest, which is nearest neighbor.

you only rely on keypoint matches and findHomography. you should run ECC refinement on that homography too.

---

<div class="post-metadata">

**Author:** ![Tanu](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@Tanu](https://forum.opencv.org/u/Tanu)\
**Post date:** [October 27, 2022, 4:50pm UTC](https://forum.opencv.org/t/image-difference-after-image-registration-and-alignment/10553/5 "2022-10-27T16:50:17Z")

</div>

Thanks for the inputs. Will try with these.

Below is the difference image I get with absDiff method.

 ![diff](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/70b602eb6438226754192ae24055d51859a6da79.jpeg)

---

<div class="post-metadata">

**Author:** ![Tanu](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@Tanu](https://forum.opencv.org/u/Tanu)\
**Post date:** [October 27, 2022, 5:03pm UTC](https://forum.opencv.org/t/image-difference-after-image-registration-and-alignment/10553/6 "2022-10-27T17:03:10Z")

</div>

I have two images which are similar but one image is having lighter shade of color compared to other. When I try to get difference between two images everything is shown as different even though objects are just having different shades of gray color. Is there any way to ignore the color / shade difference while finding difference?

Code:

imageA = cv2.imread(“Image1.png”)  
imageB = cv2.imread(“Image2.png”)

grayA = cv2.cvtColor(imageA, cv2.COLOR\_BGR2GRAY)  
grayB = cv2.cvtColor(imageB, cv2.COLOR\_BGR2GRAY)

diff = cv2.absdiff(grayA, grayB)

thresh = cv2.threshold(diff, 0, 255, cv2.THRESH\_BINARY\_INV | cv2.THRESH\_OTSU)[1]  
cnts = cv2.findContours(thresh.copy(), cv2.RETR\_EXTERNAL, cv2.CHAIN\_APPROX\_SIMPLE)  
cnts = imutils.grab\_contours(cnts)

for c in cnts:  
(x, y, w, h) = cv2.boundingRect(c);  
cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)

cv2.imwrite(“result.png”, imageA)

 ![Image1](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/9/995eaba30302e06083f89d0dab4f4e9283ad5ffe.jpeg)

 ![Image2](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/7c6faa4442fc202e04a779f9982a5ea577947947.png)

 ![bailey-Difference](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/4/4ea9d887a772386ad2b7b24e918b1e27d2ad5478.jpeg)

Appreciate the help.
