【问题标题】:OpenCV Error in resize() Pythonresize() Python 中的 OpenCV 错误
【发布时间】:2017-12-17 10:48:46
【问题描述】:

我想叠加一组相同大小的给定图像(AT&T 面部图像数据库)。为此,我编写了代码,其工作原理如下:

  1. 我已经指定了图像的位置(开始我是 仅考虑 4 张图片)。
  2. imstack 用于读取一个图像(作为基础图像), 将发生中途停留(叠加)。
  3. 运行 for 循环遍历所有图像并将它们添加到 基本图像(imstack)。这种添加是通过使用 addWeighted() 函数,参数为当前图像 (im) 和 alpha 值为 0.5 的基本图像(imstack) 分别。
  4. 在循环运行直到完成后(所有图像都是 叠加在基础图像上)我试图打印更新的imstack 使用 imshow() 作为“compiledimg”。
  5. 我还添加了保存“compiledimg”文件的选项 按“s”。

错误:

imstack=cv2.resize(imstack,(97,113)) cv2.error: /build/opencv-RI6cfE/opencv-2.4.9.1+dfsg1/modules/imgproc/src/imgwarp.cpp:1834: error: (-215) ssize.area() > 0 in function resize

import cv2 
import numpy as np
import os

fnames =['~/Downloads/1.pgm','~/Downloads/2.pgm','~/Downloads/3.pgm']

imstack=cv2.imread('~/Downloads/4.pgm')

imstack=cv2.resize(imstack,(97,113))

for path in fnames:
  im=cv2.imread(os.path.expanduser(path))

  imstack=cv2.addWeighted(imstack,0.5,im,0.5,0)

  imstack=cv2.resize(imstack,(97,113))

cv2.imshow('compiledimg.jpg',imstack)

k = cv2.waitKey(0) & 0xFF

if k == 27:         
  cv2.destroyAllWindows()

elif k == ord('s'): 
  cv2.imwrite('compiledimg.jpg',imstack)
cv2.destroyAllWindows()

【问题讨论】:

  • 确保图像已加载,在imread功能后,打印形状以查看图像是否正确加载(它应该打印(高度,宽度,通道),如果你看到0,0那么它未加载)。验证您正确编写了文件路径,我建议使用没有缩写的完整路径
  • 请注意,您在第二次 imread 中使用 os.path.expanduser 而不是在第一次 imread 中
  • @Dimitrii 那是因为我使用 imstack 作为其他图像将覆盖的基础图像。在我正在阅读图像的第二个 imread 中,每个路径都会发生变化,这就是为什么每次迭代后都会有一个 os.path.expanduser 变化的原因。
  • @appi55 谢谢你的问题。我为第一个 imstack 图像运行了 print,它说它是一个空属性。我对路径进行了一些更改,您提到了它,它帮助我解决了这个错误。虽然现在我面临另一个错误,但祝我好运。

标签: python opencv image-processing


【解决方案1】:

我能够摆脱这个错误。 主要是我犯了两个错误:

  1. 更正了我创建路径的方式。阅读更多内容并最终完成。

  2. 我必须检查读取输入的天气,因为 imstack 是否正在被读取,并且由于之前我的路径是错误的,所以它被作为 NULL 输入,因此出现错误。

其中 4.pgm 是基础图像,2.pgm 是我将使用测试的图像,所以我没有将它包含在将被覆盖的图像的路径中。

即兴部分代码如下:

path='test/'

#Appends all the absolute paths in the list image_paths 
image_paths = [os.path.join(path, f) for f in os.listdir(path) if not f.endswith('2.pgm')]

#initializing a base_image over which other images will be superimposed
base_image=cv2.imread('4.pgm')

#resizing the base image so it matches the size of the database pics (113(rows),97(columns))
base_image=cv2.resize(base_image,(97,113))

#cv2.imshow('lol',base_image) //for testing purposes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-15
    • 2019-12-07
    • 1970-01-01
    • 2020-10-13
    • 2020-08-21
    • 1970-01-01
    • 2020-08-11
    • 2021-02-23
    相关资源
    最近更新 更多