Image Comparison
lastmod 2025-01-03created 2025-01-031 edits
Notes on image comparison.
Comparing Two Images in GUI Applications
Recommended methods for improving readability and efficiency when comparing two images and displaying them in a GUI.
There are various approaches depending on the purpose of the comparison (difference analysis, similarity checking, etc.), but the main methods are as follows.
Side-by-Side Comparison (left-right or top-bottom comparison)
- Method: Place the two images next to each other. Arrange them left-right or top-bottom so the user compares the differences visually.
- Advantage: The most intuitive, and it is easy to see the changed parts directly.
- Implementation: Place the two images on a canvas with OpenCV, PyQt, Tkinter, etc.
- Additional features:
- Use a slider to adjust the transparency of one image, or zoom in on a specific region.
- Use the mouse to sync positions between the images and compare the same point at the same time.
import cv2
import numpy as np
from tkinter import *
from PIL import Image, ImageTk
# 이미지 불러오기
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
# 크기 조정 (동일 크기로)
img1 = cv2.resize(img1, (600, 400))
img2 = cv2.resize(img2, (600, 400))
# GUI 설정
root = Tk()
root.title("Image Comparison")
# 이미지 변환
img1_tk = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)))
img2_tk = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)))
# 라벨 배치
Label(root, image=img1_tk).grid(row=0, column=0)
Label(root, image=img2_tk).grid(row=0, column=1)
root.mainloop()
Difference Overlay
- Method: Compare the two images pixel by pixel and highlight the differences. Mark the differing parts in a color (red, etc.) to clearly reveal the differences between the two images.
- Advantage: Differences can be checked quickly, and subtle changes are detected effectively.
- Implementation: Compute the difference between the images with cv2.absdiff() or cv2.subtract().
- Additional features:
- Treat only values above a certain threshold as differences and remove noise.
- Add labels or borders to the differing parts.
diff = cv2.absdiff(img1, img2)
gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray_diff, 30, 255, cv2.THRESH_BINARY)
# 차이 부분 강조
highlight = cv2.addWeighted(img1, 0.6, img2, 0.6, 0)
highlight[thresh > 0] = (0, 0, 255)
cv2.imshow("Difference Highlight", highlight)
cv2.waitKey(0)
cv2.destroyAllWindows()
Slider Blending
- Method: Compare by moving a slider to adjust the transparency between the two images. Dragging the slider to one side gradually transitions one image into the other.
- Advantage: Differences can be checked in real time, and the user can directly inspect the parts they want.
- Implementation: Implement a slider with OpenCV and Tkinter or PyQt to adjust the image weights.
def update(val):
alpha = val / 100
blended = cv2.addWeighted(img1, alpha, img2, 1 - alpha, 0)
cv2.imshow('Blended Image', blended)
cv2.namedWindow('Blended Image')
cv2.createTrackbar('Blend', 'Blended Image', 0, 100, update)
update(0)
cv2.waitKey(0)
cv2.destroyAllWindows()
Blinking
- Method: Display the two images alternately, blinking between them.
- Advantage: Subtle differences show up immediately; effective when the two images are nearly identical.
- Implementation: Use a timer to switch images at a fixed interval.
while True:
cv2.imshow('Comparison', img1)
cv2.waitKey(500) # 500ms 후 전환
cv2.imshow('Comparison', img2)
cv2.waitKey(500)
Swipe Line
- Method: Partially cover one image with a mouse drag or slider to compare the same region of the two images.
- Advantage: Accurate comparison is possible while directly checking the same region.
- Implementation: Detect slider or mouse events and show one image cropped.
def swipe(val):
x = val
swipe_img = img2.copy()
swipe_img[:, :x] = img1[:, :x]
cv2.imshow('Swipe Comparison', swipe_img)
cv2.namedWindow('Swipe Comparison')
cv2.createTrackbar('Swipe', 'Swipe Comparison', 0, img1.shape[1], swipe)
swipe(0)
cv2.waitKey(0)
cv2.destroyAllWindows()
Conclusion
- For simple comparison, the Side-by-Side method is suitable.
- For difference analysis, the Difference Overlay method is the most effective.
- If intuitive interaction is needed, the Slider Blending or Swipe Line method is recommended.
See also
Favorite site
Pages that link here