Last active
March 24, 2023 12:30
-
-
Save fmaor267/f328d947c7124f40c42bb52306c1e5a0 to your computer and use it in GitHub Desktop.
Untitled
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The first commented line is your dabblet’s title | |
*/ | |
background: #f06; | |
background: linear-gradient(45deg, #f06, yellow); | |
min-height: 100%; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
import fitz | |
from tkinter import filedialog | |
from tkinter import Tk | |
from skimage.metrics import structural_similarity as ssim | |
def pdf_to_image(pdf_file): | |
doc = fitz.open(pdf_file) | |
page = doc.load_page(0) | |
pix = page.get_pixmap(matrix=fitz.Matrix(4, 4)) | |
img_buffer = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.height, pix.width, pix.n) | |
return cv2.cvtColor(img_buffer, cv2.COLOR_RGB2BGR) | |
def resize_images_to_same_size(img1, img2): | |
height1, width1 = img1.shape[:2] | |
height2, width2 = img2.shape[:2] | |
if height1 != height2 or width1 != width2: | |
height = min(height1, height2) | |
width = min(width1, width2) | |
img1 = cv2.resize(img1, (width, height)) | |
img2 = cv2.resize(img2, (width, height)) | |
return img1, img2 | |
def process_plan(image): | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
blurred = cv2.GaussianBlur(gray, (5, 5), 0) | |
return blurred | |
def compare_plans_ssim(img1, img2): | |
return ssim(img1, img2) | |
def compare_images_mse(img1, img2): | |
err = np.sum((img1.astype("float") - img2.astype("float")) ** 2) | |
err /= float(img1.shape[0] * img1.shape[1]) | |
return err | |
def compare_plans_fft(img1, img2): | |
f1 = np.fft.fft2(img1) | |
f1_shift = np.fft.fftshift(f1) | |
f2 = np.fft.fft2(img2) | |
f2_shift = np.fft.fftshift(f2) | |
return np.mean(np.abs(f1_shift - f2_shift)) | |
def highlight_changes(img1, img2): | |
# Convert images to grayscale if they are not already | |
if len(img1.shape) == 3: | |
gray_img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) | |
else: | |
gray_img1 = img1 | |
if len(img2.shape) == 3: | |
gray_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) | |
else: | |
gray_img2 = img2 | |
# Calculate absolute difference | |
abs_diff = cv2.absdiff(gray_img1, gray_img2) | |
# Threshold the difference image | |
_, thresh = cv2.threshold(abs_diff, 30, 255, cv2.THRESH_BINARY) | |
# Dilate the thresholded image to highlight differences | |
kernel = np.ones((3, 3), np.uint8) | |
dilated = cv2.dilate(thresh, kernel, iterations=1) | |
# Convert the dilated image to 3-channel | |
color_diff = cv2.cvtColor(dilated, cv2.COLOR_GRAY2BGR) | |
# Convert grayscale images to 3-channel images | |
if len(gray_img1.shape) == 2: | |
gray_img1 = cv2.cvtColor(gray_img1, cv2.COLOR_GRAY2BGR) | |
if len(gray_img2.shape) == 2: | |
gray_img2 = cv2.cvtColor(gray_img2, cv2.COLOR_GRAY2BGR) | |
# Add the color_diff image to the original image | |
result = cv2.addWeighted(gray_img2, 0.5, color_diff, 0.5, 0) | |
return result | |
def calculate_weighted_score(ssim_value, mse_value, fft_value, ssim_weight=0.5, mse_weight=0.3, fft_weight=0.2): | |
normalized_mse = 1 - (mse_value / (mse_value + 1)) | |
normalized_fft = 1 - (fft_value / (fft_value + 1)) | |
combined_score = ssim_weight * ssim_value + mse_weight * normalized_mse + fft_weight * normalized_fft | |
return combined_score | |
def main(): | |
root = Tk() | |
root.withdraw() | |
file_path1 = filedialog.askopenfilename() | |
file_path2 = filedialog.askopenfilename() | |
if file_path1.endswith('.pdf') and file_path2.endswith('.pdf'): | |
img1 = pdf_to_image(file_path1) | |
img2 = pdf_to_image(file_path2) | |
else: | |
img1 = cv2.imread(file_path1) | |
img2 = cv2.imread(file_path2) | |
img1, img2 = resize_images_to_same_size(img1, img2) | |
processed_img1 = process_plan(img1) | |
processed_img2 = process_plan(img2) | |
ssim_value = compare_plans_ssim(processed_img1, processed_img2) | |
mse_value = compare_images_mse(processed_img1, processed_img2) | |
fft_value = compare_plans_fft(processed_img1, processed_img2) | |
print("SSIM: ", ssim_value) | |
print("MSE: ", mse_value) | |
print("FFT Difference: ", fft_value) | |
combined_score = calculate_weighted_score(ssim_value, mse_value, fft_value) | |
print("Combined Score: ", combined_score) | |
result = highlight_changes(processed_img1, processed_img2) | |
cv2.imwrite("result.png", result) | |
print("Result image saved as result.png") | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// alert('Hello world!'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"all"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment