【发布时间】:2018-10-03 15:10:37
【问题描述】:
我有近 900 张 .jpg 图像(通过将视频分割成帧获得,每张图像为 116ko),我想使用“均方误差”将我的每一帧与第一帧 (= frame0) 进行比较。
加载两张图片并比较它们很容易,但是(至少对我而言)编写一个加载和比较我的图片的循环要困难得多。 我的目标只是列出一个包含所有“均方误差”(MSE)值的列表。
问题:
我不需要同时加载或打开所有图像,但我不知道如何在处理期间加载图像。
我的图片与我的.py 文件位于同一文件夹中。
当我运行我的代码时,它会返回:
Traceback(最近一次调用最后一次): 文件“C:\Users\Susie\Documents\programmes_python\python\images_video\comparaison-images2.py”,第 46 行,在 比较图像(frame0,frame0) 文件“C:\Users\Susie\Documents\programmes_python\python\images_video\comparaison-images2.py”,第 24 行,在 compare_images s = ssim(图像A,图像B) 文件“C:\Python27\lib\site-packages\skimage\measure_structural_similarity.py”,第 151 行,在 compare_ssim 中 “win_size 超出图像范围。如果输入是多通道” ValueError:win_size 超出图像范围。如果输入是多通道(彩色)图像,则设置 multichannel=True。
# import the necessary packages
from skimage.measure import compare_ssim as ssim
import numpy as np
import cv2
from os import listdir
from os.path import isfile, join
def mse(imageA, imageB):
# the 'Mean Squared Error' between the two images is the
# sum of the squared difference between the two images
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
# return the MSE, the lower the error, the more "similar"
# the two images are
return err
def compare_images(imageA, imageB):
# compute the mean squared error and structural similarity
# index for the images
m = mse(imageA, imageB)
s = ssim(imageA, imageB)
mse_list.append(m)
ssim_list.append(s)
print (mse_list)
print (ssim_list)
# load the images and convert the images to grayscale
frame0 = cv2.imread("frame0.jpg")
mypath='C:\Users\Susie\Documents\programmes_python\python\images_video'
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = np.empty(len(onlyfiles), dtype=object)
for n in range(1, len(onlyfiles)):
images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
images[n] = cv2.cvtColor(images[n], cv2.COLOR_BGR2GRAY)
# compare the images
mse_list = []
ssim_list = []
compare_images(frame0, frame0)
for n in range(1, len(onlyfiles)):
compare_images(frame0, images[n])
【问题讨论】:
-
您应该放置完整的堆栈跟踪,以便人们至少知道哪一行出现故障。该错误很可能是一个未正确加载的图像(可能它不存在)并且 imread 没有返回任何内容,然后您尝试使用它并给出错误。
-
将路径更改为:
mypath='C:/Users/Susie/Documents/programmes_python/python/images_video' -
谢谢,我的图片好像都没有找到。我更改了 .py 文件的位置(将其保存在与图像相同的文件中),现在它不会返回属性错误。
-
@JeruLuke 它用这个新路径返回了相同的属性错误。
-
@SusieB 如果您的
.py与图像位于同一位置,那么它应该不是您所说的问题。顺便问一下frame0.jpg在哪里?确保也是如此。