|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | +from visual_comparison.exceptions import ImageComparisonException,ImageNotFoundException |
| 4 | +from skimage.metrics import structural_similarity as ssim |
| 5 | + |
| 6 | +class ImageComparisonUtil: |
| 7 | + @staticmethod |
| 8 | + def read_image(path): |
| 9 | + try: |
| 10 | + image = cv2.imread(path) |
| 11 | + if image is None: |
| 12 | + raise ImageNotFoundException(f"Image with path '{path}' not found") |
| 13 | + return image |
| 14 | + except Exception as e: |
| 15 | + raise ImageComparisonException(f"Cannot read image from the file, path={path}", e) |
| 16 | + |
| 17 | + @staticmethod |
| 18 | + def save_image(path, image): |
| 19 | + try: |
| 20 | + cv2.imwrite(path, image) |
| 21 | + except Exception as e: |
| 22 | + raise ImageComparisonException(f"Cannot save image to path={path}", e) |
| 23 | + |
| 24 | + # show difference as B/W Image |
| 25 | + @staticmethod |
| 26 | + def compare_images_bw(expected_image, actual_image, result_destination=None): |
| 27 | + # Convert images to grayscale |
| 28 | + expected_gray = cv2.cvtColor(expected_image, cv2.COLOR_BGR2GRAY) |
| 29 | + actual_gray = cv2.cvtColor(actual_image, cv2.COLOR_BGR2GRAY) |
| 30 | + |
| 31 | + # Calculate Structural Similarity Index (SSI) |
| 32 | + similarity_index = ssim(expected_gray, actual_gray) |
| 33 | + |
| 34 | + # If result destination is provided, save the difference image |
| 35 | + if result_destination: |
| 36 | + diff_image = cv2.absdiff(expected_gray, actual_gray) |
| 37 | + ImageComparisonUtil.save_image(result_destination, diff_image) |
| 38 | + |
| 39 | + return similarity_index |
| 40 | + |
| 41 | + # show difference as individual red boxes |
| 42 | + @staticmethod |
| 43 | + def compare_images_sep(expected_image, actual_image, result_destination=None): |
| 44 | + # Convert images to grayscale |
| 45 | + expected_gray = cv2.cvtColor(expected_image, cv2.COLOR_BGR2GRAY) |
| 46 | + actual_gray = cv2.cvtColor(actual_image, cv2.COLOR_BGR2GRAY) |
| 47 | + |
| 48 | + # Calculate Structural Similarity Index (SSI) |
| 49 | + similarity_index = ssim(expected_gray, actual_gray) |
| 50 | + |
| 51 | + # If result destination is provided, save the difference image |
| 52 | + if result_destination: |
| 53 | + # Calculate absolute difference image |
| 54 | + diff_image = cv2.absdiff(expected_gray, actual_gray) |
| 55 | + |
| 56 | + # Threshold the difference image |
| 57 | + _, thresholded_diff = cv2.threshold(diff_image, 30, 255, cv2.THRESH_BINARY) |
| 58 | + |
| 59 | + # Find contours of differences |
| 60 | + contours, _ = cv2.findContours(thresholded_diff, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| 61 | + |
| 62 | + # Draw rectangles around differences |
| 63 | + for contour in contours: |
| 64 | + x, y, w, h = cv2.boundingRect(contour) |
| 65 | + cv2.rectangle(actual_image, (x, y), (x + w, y + h), (0, 0, 255), 2) # Red rectangles |
| 66 | + |
| 67 | + # Save the resulting image |
| 68 | + cv2.imwrite(result_destination, actual_image) |
| 69 | + |
| 70 | + return similarity_index |
| 71 | + |
| 72 | + # show difference as a complete rectangular box |
| 73 | + @staticmethod |
| 74 | + def compare_images(expected_image, actual_image, result_destination=None): |
| 75 | + # Convert images to grayscale |
| 76 | + expected_gray = cv2.cvtColor(expected_image, cv2.COLOR_BGR2GRAY) |
| 77 | + actual_gray = cv2.cvtColor(actual_image, cv2.COLOR_BGR2GRAY) |
| 78 | + |
| 79 | + # Calculate Structural Similarity Index (SSI) |
| 80 | + similarity_index = ssim(expected_gray, actual_gray) |
| 81 | + |
| 82 | + # If result destination is provided, save the difference image |
| 83 | + if result_destination: |
| 84 | + # Calculate absolute difference image |
| 85 | + diff_image = cv2.absdiff(expected_gray, actual_gray) |
| 86 | + |
| 87 | + # Threshold the difference image |
| 88 | + _, thresholded_diff = cv2.threshold(diff_image, 30, 255, cv2.THRESH_BINARY) |
| 89 | + |
| 90 | + # Find contours of differences |
| 91 | + contours, _ = cv2.findContours(thresholded_diff, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| 92 | + |
| 93 | + # Combine all bounding rectangles into one |
| 94 | + combined_rect = cv2.boundingRect(np.concatenate(contours)) |
| 95 | + x, y, w, h = combined_rect |
| 96 | + |
| 97 | + # Draw a rectangle around the combined differences |
| 98 | + cv2.rectangle(actual_image, (x, y), (x + w, y + h), (0, 0, 255), 2) # Red rectangle |
| 99 | + |
| 100 | + # Save the resulting image |
| 101 | + cv2.imwrite(result_destination, actual_image) |
| 102 | + |
| 103 | + return similarity_index |
| 104 | + |
| 105 | + @staticmethod |
| 106 | + def check_mismatch(expected_image, actual_image): |
| 107 | + similarity_index = ImageComparisonUtil.compare_images(expected_image, actual_image) |
| 108 | + # If similarity index is less than 1.0, there is a mismatch |
| 109 | + if similarity_index < 1.0: |
| 110 | + return True |
| 111 | + return False |
| 112 | + |
| 113 | + |
| 114 | + @staticmethod |
| 115 | + def check_match(expected_image, actual_image): |
| 116 | + similarity_index = ImageComparisonUtil.compare_images(expected_image, actual_image) |
| 117 | + # If similarity index is 1.0, images are identical |
| 118 | + if similarity_index == 1.0: |
| 119 | + return True |
| 120 | + return False |
0 commit comments