لطفا دو کد زیر را در نظر بگیرید و کاری کنید که دو تصویر در نهایت که روی هم میافتند اختلاف کمی داشته باشند:                                                                                                                                                                                                              کد اول:                                                                                                                                                                                                                                                                                                                                                                                                                       import cv2
import numpy as np

# Load the images (image 1 and image 2)
image_path_1 = '/content/bird/hw3/1.jpg'
image_path_2 = '/content/bird/hw3/2.jpg'

# Convert both images to grayscale
gray_image_1 = cv2.imread(image_path_1, cv2.IMREAD_GRAYSCALE)
gray_image_2 = cv2.imread(image_path_2, cv2.IMREAD_GRAYSCALE)

# Initialize SIFT detector
sift = cv2.SIFT_create()

# Detect keypoints and descriptors for both images
keypoints_1, descriptors_1 = sift.detectAndCompute(gray_image_1, None)
keypoints_2, descriptors_2 = sift.detectAndCompute(gray_image_2, None)

# Match keypoints using BFMatcher or KNN
bf = cv2.BFMatcher(cv2.NORM_L2)
matches = bf.knnMatch(descriptors_1, descriptors_2, k=2)

# Apply ratio test as per Lowe's paper to filter good matches
good_matches = []
for m, n in matches:
    if m.distance < 0.75 * n.distance:
        good_matches.append(m)

# Extract location of good matches
if len(good_matches) >= 3:  # At least 3 matches needed for affine transform
    src_pts = np.float32([keypoints_1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
    dst_pts = np.float32([keypoints_2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)

    # Compute affine transformation using RANSAC
    M, mask = cv2.estimateAffinePartial2D(src_pts, dst_pts, method=cv2.RANSAC, ransacReprojThreshold=5.0)

    # Get the inliers (mask will contain 1 for inliers and 0 for outliers)
    inliers = mask.ravel().tolist()

    # Print the result
    print(f"Affine transformation matrix:\n{M}")
    print(f"Number of inliers: {np.sum(mask)} out of {len(good_matches)} matches")

    # Draw matches (optional)
    matched_image = cv2.drawMatches(gray_image_1, keypoints_1, gray_image_2, keypoints_2, good_matches, None, 
                                    matchesMask=inliers, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
    plt.imshow(matched_image)
    plt.show()
else:
    print("Not enough matches are found - at least 3 are required.")                                                                                                                                                                                                                                                                                                                                                                                                                    
کد دوم:                                                                                                                                                                                                                                                                                                                                                                                                                         import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load the images (image 1 and image 2)
image_path_1 = '/content/bird/hw3/1.jpg'
image_path_2 = '/content/bird/hw3/2.jpg'

# Read both images in grayscale
gray_image_1 = cv2.imread(image_path_1, cv2.IMREAD_GRAYSCALE)
gray_image_2 = cv2.imread(image_path_2, cv2.IMREAD_GRAYSCALE)

# Affine transformation matrix obtained from RANSAC
M = np.array([[   1.0799486,     0.57204358, -498.85471686],
              [  -0.57204358,    1.0799486,   276.55610336]])

# Get the size of the second image (target image)
height, width = gray_image_2.shape

# Apply the affine transformation to image 1
transformed_image_1 = cv2.warpAffine(gray_image_1, M, (width, height))

# Calculate the difference between the transformed image 1 and image 2
# Here, we use absolute difference to compute pixel-wise difference
difference_image = cv2.absdiff(transformed_image_1, gray_image_2)

# Display the transformed image and difference image
plt.figure(figsize=(12, 6))

plt.subplot(1, 3, 1)
plt.title("Image 1 (Transformed)")
plt.imshow(transformed_image_1, cmap='gray')

plt.subplot(1, 3, 2)
plt.title("Image 2")
plt.imshow(gray_image_2, cmap='gray')

plt.subplot(1, 3, 3)
plt.title("Difference Image")
plt.imshow(difference_image, cmap='gray')

plt.show()

# Calculate and print the overall difference (sum of absolute differences)
total_difference = np.sum(difference_image)
print(f"Total difference between the two images: {total_difference}")