【问题标题】:OpenCV identical images return different resultsOpenCV 相同的图像返回不同的结果
【发布时间】:2020-07-12 16:53:01
【问题描述】:

我正在尝试分析目录中的一组图像,但只有一个图像有效。所有图像的大小大致相同 (3MB),并且来自相同的原始图像。所有这些都可以在 GIMP 和照片中查看,但 cv2.imshow 仅适用于 slice_0_15.jpg
这里发生了什么? 使用 Python 3.8.3 版,OpenCV 4.2.0 版

for file in os.listdir("scanned_extrudate"): #go through all 18 slices in the directory
    print("Analyzing file " + str(file))
    image = cv2.imread(file)

    if image is None:
        print("Error: no image selected")

    else:
        rows,cols,_ = image.shape #dimensions of image (eg 200x300)
        print("Image: " + str(rows) + " rows, " + str(cols) + " columns")

输出:

Analyzing file slice_0_0.jpg
Error: no image selected
Analyzing file slice_0_1.jpg
Error: no image selected
Analyzing file slice_0_10.jpg
Error: no image selected
Analyzing file slice_0_11.jpg
Error: no image selected
Analyzing file slice_0_12.jpg
Error: no image selected
Analyzing file slice_0_13.jpg
Error: no image selected
Analyzing file slice_0_14.jpg
Error: no image selected
Analyzing file slice_0_15.jpg
Image: 19935 rows, 928 columns
Analyzing file slice_0_16.jpg
Error: no image selected
Analyzing file slice_0_17.jpg
Error: no image selected
Analyzing file slice_0_2.jpg
Error: no image selected
Analyzing file slice_0_3.jpg
Error: no image selected
Analyzing file slice_0_4.jpg
Error: no image selected
Analyzing file slice_0_5.jpg
Error: no image selected
Analyzing file slice_0_6.jpg
Error: no image selected
Analyzing file slice_0_7.jpg
Error: no image selected
Analyzing file slice_0_8.jpg
Error: no image selected
Analyzing file slice_0_9.jpg
Error: no image selected

当我在终端中输入“文件 *”时,所有图像的结果都相同:(唯一的区别是尺寸)

slice_0_0.jpg:  JPEG image data, JFIF standard 1.01, resolution (DPI), density 2400x2400, segment length 16, Exif Standard: [TIFF image data, little-endian, direntries=7, xresolution=98, yresolution=106, resolutionunit=2, software=GIMP 2.10.14, datetime=2020:07:10 13:34:33], progressive, precision 8, 1088x19935, frames 1

【问题讨论】:

  • 图像是如何生成的?对 slice_0_15.jpg 未完成的其余部分做了什么,反之亦然?你能重新生成图像吗?您的代码如何与随机 JPG 一起工作?

标签: python image opencv


【解决方案1】:

这是因为您没有读取 scanned_extrudate 文件内的图像。您看到的是寻找正确目录的图像,但您试图读取错误的目录。这应该可以解决您的问题:

image = cv2.imread("/ur/directory/to/scanned_extrudate/" + file)

【讨论】:

  • 你是对的,我试图从错误的目录中读取,但这段代码对我不起作用。 @lenik 的 os.path.join(FOLDER,file) 最终工作
【解决方案2】:

试试这个:

import os

FOLDER = "scanned_extrudate"

for file in os.listdir(FOLDER): #go through all 18 slices in the directory
    print("Analyzing file " + str(file))
    image = cv2.imread(os.path.join( FOLDER, file))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多