使用AI找出两张图片之间的差异

我在寻找一种使用AI来找出两张图片之间差异的方法。

这是我的大学项目,我的教授要求我创建一个程序,使用AI来检测和找出两对图片之间的差异。

我使用了孪生网络来部署它,以计算差异,如果差异大于阈值,我使用以下代码来显示差异:

input_images = np.array([[img1, img2]])difference_image = np.abs(input_images[0, 0] - input_images[0, 1])plt.imshow(difference_image)

但我的教授没有接受,他给了我一个提示,要我使用Conv2D将图片分割成更小的形状,然后比较这些形状,如果有差异,就用边界框高亮显示这些差异。

有谁能帮助我部署这个代码吗?

我之前的代码是:

import numpy as npimport matplotlib.pyplot as pltfrom tensorflow import kerasfrom tensorflow.keras import layersimg1 = plt.imread('1-1.jpg')img2 = plt.imread('1-2.jpg')input_shape = img1.shape  # Assuming images are of the same shape# Function to create    # def create_siamese_model(input_shape):input_image_1 = layers.Input(shape=input_shape, name='input_image_1')input_image_2 = layers.Input(shape=input_shape, name='input_image_2')# Base networkbase_network = keras.Sequential([    layers.Conv2D(40, (3, 3), activation='relu', input_shape=input_shape),    layers.MaxPooling2D(pool_size=(2, 2)),    layers.Flatten(),    layers.Dense(256, activation='relu')])# Encoded representations of input imagesencoded_image_1 = base_network(input_image_1)encoded_image_2 = base_network(input_image_2)# L1 distance layerl1_distance = layers.Lambda(lambda tensors: keras.backend.abs(tensors[0] - tensors[1]))([encoded_image_1, encoded_image_2])# Output layeroutput_layer = layers.Dense(15, activation='sigmoid')(l1_distance)model = keras.Model(inputs=[input_image_1, input_image_2], outputs=output_layer)input_images = np.array([[img1, img2]])predictions = model.predict([input_images[:, 0], input_images[:, 1]])threshold=0.5if predictions[0, 0] > threshold:    # Highlight differences if the prediction is above the threshold    difference_image = np.abs(input_images[0, 0] - input_images[0, 1])    difference_image    plt.imshow(difference_image)    plt.show()

回答:

我找到了一种使用CNN网络来找出两张图片之间差异的方法,代码如下:

# Importing necessary librariesimport tensorflow as tfimport matplotlib.pyplot as plt# Specify the file paths for the two imagesimage_path1 = '1.jpg'image_path2 = '2    .jpg'# Read and decode images, then normalize pixel values to the range [0, 1]img1 = tf.io.read_file(image_path1)img1 = tf.image.decode_image(img1, channels=1)img1 = tf.cast(img1, tf.float32) / 255.0img2 = tf.io.read_file(image_path2)img2 = tf.image.decode_image(img2, channels=1)img2 = tf.cast(img2, tf.float32) / 255.0# Add a batch dimension to the imagesimg1 = tf.expand_dims(img1, axis=0)img2 = tf.expand_dims(img2, axis=0)# Create a Conv2D layer with specified parametersconv2d_layer = tf.keras.layers.Conv2D(filters=1, kernel_size=(3, 3), activation='relu', padding='same')# Apply the Conv2D layer to both imagesoutput1 = conv2d_layer(img1)output2 = conv2d_layer(img2)# Calculate the absolute difference between the Conv2D outputsdiff = tf.abs(output1 - output2)# Plotting the images and Conv2D outputs for visualizationplt.figure(figsize=(10, 5))plt.subplot(1, 4, 1)plt.imshow(tf.squeeze(img1), cmap='gray')plt.title('Image 1')plt.axis('off')plt.subplot(1, 4, 2)plt.imshow(tf.squeeze(img2), cmap='gray')plt.title('Image 2')plt.axis('off')plt.subplot(1, 4, 3)plt.imshow(tf.squeeze(output1), cmap='gray')plt.title('Conv2D Image 1')plt.axis('off')plt.subplot(1, 4, 4)plt.imshow(tf.squeeze(diff), cmap='gray')plt.title('Absolute Difference')plt.axis('off')# Display the plotplt.show()

这段代码使用CNN网络来计算两张图片数组之间的距离

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注