✍42.图片识别相关cv2
CV2 模块安装
pip3 install opencv-python
skimage.measure.compare_ssim has been moved to skimage.metrics.structural_similarity. It will be removed from skimage.measure in version 0.18.
识别相似相同图片
import os
import asyncio
from skimage.measure import compare_ssim as ssim
# from skimage import measure
# ssim = measure.compare_ssim
import matplotlib.pyplot as plt
import numpy as np
import cv2
import time
def mse(imageA, imageB):
# 计算两张图片的MSE指标
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
# 返回结果,该值越小越好
return err
# async def compare_images(imageA, imageB, title):
def compare_images(imageA, imageB, title):
# 分别计算输入图片的MSE和SSIM指标值的大小
m = mse(imageA, imageB)
s = ssim(imageA, imageB)
# if s >= 0.5 and m <= 1530:
if s >= 0.5:
# 创建figure
fig = plt.figure(title)
plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s))
# 显示第一张图片
ax = fig.add_subplot(1, 2, 1)
plt.imshow(imageA, cmap=plt.cm.gray)
plt.axis("on") # 显示坐标
# 显示第二张图片
ax = fig.add_subplot(1, 2, 2)
plt.imshow(imageB, cmap=plt.cm.gray)
plt.axis("on")
plt.tight_layout()
plt.show()
def start_compar(image_path, images_dir):
# task = []
# 读取图片
original1 = cv2.imread(image_path)
original = cv2.cvtColor(original1, cv2.COLOR_BGR2GRAY)
images_list = os.listdir(images_dir)
print("正在查找...")
for image_name in images_list:
images_path = os.path.join(images_dir,image_name)
contrast1 = cv2.imread(images_path)
# 将彩色图转换为灰度图
try:
contrast = cv2.cvtColor(contrast1, cv2.COLOR_BGR2GRAY)
except Exception as e:
print(e)
else:
# 比较图片
compare_images(original, contrast, "Original vs Enhance")
# task.append(compare_images(original, contrast, "Original vs Enhance"))
# loop.run_until_complete(asyncio.wait(task))
loop = asyncio.get_event_loop()
if __name__ == '__main__':
start_time = time.time()
start_compar("M2_CMP#EE1154#24#7#36#25283619817#2_aug1748.jpg", r"D:\training_image\M_CMP_new_original\original\36_2")
print(time.time()-start_time)
'''
2782zh
使用asyncio异步整体执行要61s,不使用整体执行要106s
但如果是前面几张的照片,不使用asyncio可以很快识别出来,而使用的话需要将循环走完才开始识别
使用多线程与不使用一样106s,也很快识别
使用线程池85s,但只能匹配到一张相似相同图片(?)
(以上开启多线程或异步没有明显提升的原因是因为该程式为多运算型的任务,额外的小号计算机资源反而更慢)
'''
源代码
- MSE,即均方误差,它是一个用来计算两张图片相似度的一个指标值,该值越小表示两张图像越相似
- SSMI指标,该指标能够更好的反应出两张图片的相似度,该指标的范围是[-1,1],当SSIM=-1时表示两张图片完全不相似,当SSIM=1时表示两张图片非常相似。即该值越接近1说明两张图片越相似
from skimage.measure import compare_ssim as ssim
import matplotlib.pyplot as plt
import numpy as np
import cv2
def mse(imageA, imageB):
# 计算两张图片的MSE指标
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
# 返回结果,该值越小越好
return err
def compare_images(imageA, imageB, title):
# 分别计算输入图片的MSE和SSIM指标值的大小
m = mse(imageA, imageB)
s = ssim(imageA, imageB)
# 创建figure
fig = plt.figure(title)
plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s))
# 显示第一张图片
ax = fig.add_subplot(1, 2, 1)
plt.imshow(imageA, cmap = plt.cm.gray)
plt.axis("off")
# 显示第二张图片
ax = fig.add_subplot(1, 2, 2)
plt.imshow(imageB, cmap = plt.cm.gray)
plt.axis("off")
plt.tight_layout()
plt.show()
# 读取图片
original1 = cv2.imread("test3.jpg")
contrast1 = cv2.imread("test3_adjust1.jpg")
shopped1 = cv2.imread("test2.jpg")
# 将彩色图转换为灰度图
original = cv2.cvtColor(original1, cv2.COLOR_BGR2GRAY)
contrast = cv2.cvtColor(contrast1, cv2.COLOR_BGR2GRAY)
shopped = cv2.cvtColor(shopped1, cv2.COLOR_BGR2GRAY)
# 初始化figure对象
fig = plt.figure("Images")
images = ("Original", original), ("Enhance", contrast), ("Others", shopped)
# 遍历每张图片
for (i, (name, image)) in enumerate(images):
# 显示图片
ax = fig.add_subplot(1, 3, i + 1)
ax.set_title(name)
plt.imshow(image, cmap = plt.cm.gray)
plt.axis("off")
plt.tight_layout()
plt.show()
# 比较图片
compare_images(original, original, "Original vs Original")
compare_images(original, contrast, "Original vs Enhance")
compare_images(original, shopped, "Original vs Others")