【问题标题】:loop for image processing in python / attribute error 'NoneType'python中的图像处理循环/属性错误'NoneType'
【发布时间】: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 在哪里?确保也是如此。

标签: python opencv


【解决方案1】:

我想提几个编程错误:

1.属性错误:

确保.py 文件和图像集合存在于同一文件夹中。如果不是,请专门为例如提及路径。 mypath='C:/Users/Susie/Documents/programmes_python/python/images_video

2。值错误:

多通道 图像与 单通道 图像进行比较会引发此错误。确保图像大小相同且通道数相同。

犯错是学习的一步!!!

【讨论】:

  • 我如何确保他们拥有相同数量的频道?我从视频中提取了一堆帧......它们应该具有相同或相似的属性吧?
【解决方案2】:

由于您使用的是灰度图像,因此您需要通过添加 0 参数来指定这一点,如下所示:

frame0 = cv2.imread("frame0.jpg", 0)

查看 OpenCV 文档了解更多详情:https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    相关资源
    最近更新 更多