日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python笔记-使用SSIM找两张图不同及使用Opencv显示

發布時間:2025/3/15 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python笔记-使用SSIM找两张图不同及使用Opencv显示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

運行截圖如下:

?

這里有幾點要注意的:

①對比兩張圖片的函數SSIM具體是structural_similarity:

? ? ? ? ? Ⅰ:第一個參數和第二個參數是要對比的兩張圖片;

? ? ? ? ? Ⅱ:參數中有個full,默認為True,如果為True會把兩張圖的相似部分返回,如果為False就不返回了;

? ? ? ? ? Ⅳ:返回值full為False的時候有2個,一個是mssim浮點型的相似度,1代表2張圖一模一樣。0代表完全不一樣,grad返回兩張圖片不同的地方類型為ndarray;

②SSIM返回的ndarray里面的值為[0, 1]的float型,而OpenCV的[0, 255]為uint8型,可以通過這樣的方式轉換:

? ? ? ? ? Ⅰ:diff = (diff * 255).astype("uint8")

③cv2.threshold函數,設置一個閾值和一個顏色,將傳入的圖片沒有達到這個閾值的,轉換為這個顏色。

源碼如下:

from skimage.metrics import structural_similarity import cv2 import numpy as npif __name__ == '__main__':before = cv2.imread('./pic/demo1.png')after = cv2.imread('./pic/demo2.png')# Convert images to grayscalebefore_gray = cv2.cvtColor(before, cv2.COLOR_BGR2GRAY)after_gray = cv2.cvtColor(after, cv2.COLOR_BGR2GRAY)# Compute SSIM between two images(score, diff) = structural_similarity(before_gray, after_gray, full=True)print("Image similarity", score)# The diff image contains the actual image differences between the two images# and is represented as a floating point data type in the range [0,1]# so we must convert the array to 8-bit unsigned integers in the range# [0,255] before we can use it with OpenCVdiff = (diff * 255).astype("uint8")# Threshold the difference image, followed by finding contours to# obtain the regions of the two input images that differthresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)contours = contours[0] if len(contours) == 2 else contours[1]mask = np.zeros(before.shape, dtype='uint8')filled_after = after.copy()for c in contours:area = cv2.contourArea(c)if area > 40:x, y, w, h = cv2.boundingRect(c)cv2.rectangle(before, (x, y), (x + w, y + h), (36, 255, 12), 2)cv2.rectangle(after, (x, y), (x + w, y + h), (36, 255, 12), 2)cv2.drawContours(mask, [c], 0, (0, 255, 0), -1)cv2.drawContours(filled_after, [c], 0, (0, 255, 0), -1)cv2.imshow('before', before)cv2.imshow('after', after)cv2.imshow('diff', diff)cv2.imshow('mask', mask)cv2.imshow('filled after', filled_after)cv2.waitKey(0)

總結

以上是生活随笔為你收集整理的Python笔记-使用SSIM找两张图不同及使用Opencv显示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。