Heim  >  Artikel  >  Backend-Entwicklung  >  Verwenden Sie KI, um die Unterschiede zwischen zwei Bildern zu finden

Verwenden Sie KI, um die Unterschiede zwischen zwei Bildern zu finden

王林
王林nach vorne
2024-02-13 16:30:051230Durchsuche

使用 AI 找出两幅图像之间的差异

Frageninhalt

Ich suche nach einer Möglichkeit, mithilfe von KI den Unterschied zwischen zwei Bildern zu ermitteln.

Dies ist mein College-Projekt. Mein Professor hat mich gebeten, ein Programm zu entwickeln, um mithilfe künstlicher Intelligenz Unterschiede in zwei Bildpaaren zu erkennen und zu finden.

Ich stelle es über ein siamesisches Netzwerk bereit, um die Differenz zu berechnen. Wenn die Differenz größer als der Schwellenwert ist, verwende ich den folgenden Code, um die Differenz anzuzeigen:

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

Aber mein Professor akzeptiert es nicht Er forderte mich auf, conv2d zu verwenden, um das Bild in kleinere Formen aufzuteilen und dann die Formen zu vergleichen und mithilfe von Begrenzungsrahmen hervorzuheben, wenn es Unterschiede gibt.

Kann jemand bei der Bereitstellung dieses Codes helfen?

Mein vorheriger Code war:

import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers

img1 = 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 network
base_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 images
encoded_image_1 = base_network(input_image_1)
encoded_image_2 = base_network(input_image_2)

# L1 distance layer
l1_distance = layers.Lambda(lambda tensors: keras.backend.abs(tensors[0] - tensors[1]))([encoded_image_1, encoded_image_2])

# Output layer
output_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.5

if 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()

Richtige Antwort


Ich habe einen Weg gefunden, mithilfe eines CNN-Netzwerks den Unterschied zwischen zwei Bildern zu ermitteln Code:

# Importing necessary libraries
import tensorflow as tf
import matplotlib.pyplot as plt

# Specify the file paths for the two images
image_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.0

img2 = 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 images
img1 = tf.expand_dims(img1, axis=0)
img2 = tf.expand_dims(img2, axis=0)

# Create a Conv2D layer with specified parameters
conv2d_layer = tf.keras.layers.Conv2D(filters=1, kernel_size=(3, 3), activation='relu', padding='same')

# Apply the Conv2D layer to both images
output1 = conv2d_layer(img1)
output2 = conv2d_layer(img2)

# Calculate the absolute difference between the Conv2D outputs
diff = tf.abs(output1 - output2)

# Plotting the images and Conv2D outputs for visualization
plt.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 plot
plt.show()

Dieser Code verwendet ein CNN-Netzwerk, um den Abstand zwischen zwei Bildarrays zu berechnen

Das obige ist der detaillierte Inhalt vonVerwenden Sie KI, um die Unterschiede zwischen zwei Bildern zu finden. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:stackoverflow.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen