다른 명령
- 이미지 비교
이미지 비교 1번째
import cv2 # Load the two images img1 = cv2.imread('image1.jpg') img2 = cv2.imread('image2.jpg') # Calculate the absolute difference between the two images diff = cv2.absdiff(img1, img2) # Display the difference image cv2.imshow('Difference Image', diff) cv2.waitKey(0) cv2.destroyAllWindows()
이미지 비교 2번째
import cv2 # Load the two images img1 = cv2.imread('image1.jpg') img2 = cv2.imread('image2.jpg') # Convert the images to grayscale gray_img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # Calculate the structural similarity index (SSIM) between the two images ssim = cv2.compare_ssim(gray_img1, gray_img2) # Display the SSIM value print("The SSIM value between the two images is: {}".format(ssim))
이미지 비교 3번째
from PIL import Image, ImageChops # Load the images image1 = Image.open("image1.jpg") image2 = Image.open("image2.jpg") # Compare the images diff = ImageChops.difference(image1, image2) # Check if the images are the same if diff.getbbox() is None: print("The images are the same") else: print("The images are different")
이미지비교 3
from PIL import Image import numpy as np from skimage.metrics import structural_similarity as ssim def mse(imageA, imageB): err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2) err /= float(imageA.shape[0] * imageA.shape[1]) return err def compare_images(image_path1, image_path2): image1 = Image.open(image_path1).convert("L") # Convert to grayscale image2 = Image.open(image_path2).convert("L") image1_np = np.array(image1) image2_np = np.array(image2) mse_value = mse(image1_np, image2_np) ssim_value = ssim(image1_np, image2_np) return mse_value, ssim_value if __name__ == "__main__": image_path1 = "path/to/your/image1.jpg" image_path2 = "path/to/your/image2.jpg" mse_value, ssim_value = compare_images(image_path1, image_path2) print(f"MSE: {mse_value}") print(f"SSIM: {ssim_value}")
Replace the image_path1 and image_path2 with the paths to your images. The MSE is a measure of the difference between the two images, with a smaller value indicating a higher similarity. The SSIM is a measure of structural similarity, with a value between -1 and 1, where a higher value indicates higher similarity.
The code above will convert the images to grayscale before comparing them. If you want to compare images in color, remove the .convert("L") and update the ssim function call to ssim(image1_np, image2_np, multichannel=True).
Remember that this approach works best for images of the same size. If you need to compare images with different sizes, you should resize them before performing the comparison.
이미지 비교
import numpy as np import tensorflow as tf from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input from tensorflow.keras.preprocessing import image as kimage def load_and_preprocess_image(image_path, target_size=(224, 224)): img = kimage.load_img(image_path, target_size=target_size) img_array = kimage.img_to_array(img) img_array = np.expand_dims(img_array, axis=0) img_array = preprocess_input(img_array) return img_array def extract_features(img_array, model): features = model.predict(img_array) features = features.flatten() return features def cosine_similarity(vecA, vecB): dot_product = np.dot(vecA, vecB) normA = np.linalg.norm(vecA) normB = np.linalg.norm(vecB) return dot_product / (normA * normB) # Load pre-trained ResNet-50 model without the top layer model = ResNet50(weights="imagenet", include_top=False, pooling='avg') image1_path = "path/to/image1.jpg" image2_path = "path/to/image2.jpg" image1 = load_and_preprocess_image(image1_path) image2 = load_and_preprocess_image(image2_path) features1