0

0

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

王林

王林

发布时间:2024-02-13 16:30:05

|

3240人浏览过

|

来源于stackoverflow

转载

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

问题内容

我正在寻找一种使用 ai 发现两个图像之间差异的方法。

这是我的大学项目,我的教授要求我创建一个程序来使用人工智能检测并发现两对图像中的差异。

我使用 siamese network 部署它,来计算差异,如果差异大于阈值,我使用以下代码来显示差异:

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

正确答案


我找到了一种使用 cnn 网络来查找两幅图像之间差异的方法 代码:

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

这段代码使用cnn网络来计算两个图像数组之间的距离

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
人工智能在生活中的应用
人工智能在生活中的应用

人工智能在生活中的应用有语音助手、无人驾驶、金融服务、医疗诊断、智能家居、智能推荐、自然语言处理和游戏设计等。本专题为大家提供人工智能相关的文章、下载、课程内容,供大家免费下载体验。

401

2023.08.17

人工智能的基本概念是什么
人工智能的基本概念是什么

人工智能的英文缩写为AI,是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门新的技术科学;该领域的研究包括机器人、语言识别、图像识别、自然语言处理和专家系统等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

289

2024.01.09

人工智能不能取代人类的原因是什么
人工智能不能取代人类的原因是什么

人工智能不能取代人类的原因包括情感与意识、创造力与想象力、伦理与道德、社会交往与沟通能力、灵活性与适应性、持续学习和自我提升等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

620

2024.09.10

Python 人工智能
Python 人工智能

本专题聚焦 Python 在人工智能与机器学习领域的核心应用,系统讲解数据预处理、特征工程、监督与无监督学习、模型训练与评估、超参数调优等关键知识。通过实战案例(如房价预测、图像分类、文本情感分析),帮助学习者全面掌握 Python 机器学习模型的构建与实战能力。

32

2025.10.21

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

4

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

7

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

42

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号