【问题标题】:How to read and display images from a Drive directory using OpenCV and numpy?如何使用 OpenCV 和 numpy 从 Drive 目录读取和显示图像?
【发布时间】:2021-05-10 17:23:35
【问题描述】:

我正在尝试从驱动器上的目录中读取图像,为此我使用 OpenCV,我将图像转换为 32 位浮点数的 numpy 数组,然后显示它,我遇到的问题是图像不显示。这些图像对应于胸部 X 光片,我在 Google Colab 工作。

import matplotlib.pyplot as plt
import cv2
from skimage import transform
import numpy as np

img_r = cv2.imread('/content/drive/MyDrive/images/T-NORMAL- (3).jpeg')

img1 = np.array(img_r).astype('float32')/255
img2 = transform.resize(img1, (150, 150, 3))

plt.imshow(img2)

This is the result obtained from the first code. As seen, it is as if the image was not being well-read.

以为是我没有正确访问目录,所以使用了os.listdir方法,如下代码所示,反正图片没有显示出来。

import matplotlib.pyplot as plt
import os
import numpy as np
import cv2
from skimage import transform


items = os.listdir('/content/drive/MyDrive/imagenes')
print (items)

for each_image in items:
  if each_image.endswith(".jpeg"):
   print (each_image)
   full_path = "/content/drive/MyDrive/imagenes" + each_image
   print (full_path)
   image = cv2.imread(full_path)
   img1 = np.array(image).astype('float32')/255
   img2 = transform.resize(img1, (150, 150, 3))

plt.imshow(img2)

This is what is displayed as a result of the second code.

我希望有人可以帮助我。 提前致谢。

【问题讨论】:

    标签: python opencv google-colaboratory image-preprocessing


    【解决方案1】:

    让我知道这是否有效

    import cv2
    
    img_r = cv2.imread('/content/drive/MyDrive/images/T-NORMAL- (3).jpeg')
    
    #to resize image
    reimg_r = cv2.resize(img_r,(150,150))
    
    cv2.imshow('xrays',reimg_r)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    【讨论】:

    • 我试了下你的代码,把Colab中的cv2.imshow()方法换成cv2_inshow(),结果还是一样,没有图片显示。 img_r = cv2.imread('/content/drive/MyDrive/images/T-NORMAL- (3).jpeg')img1 = np.array(img_r).astype('float32')/255img2 = transform.resize(img1, (150, 150, 3))from google.colab.patches import cv2_imshowcv2_imshow(img2)cv2.waitKey(0)
    • 您确定您阅读的图像是有效的 jpg 图像吗?你能以其他方式查看图像吗,例如使用 Paint(在 windows 上)或 feh(在 linux 上)?
    • 是的,图像在其他应用程序中可以正确读取。我什至尝试读取 X 射线以外的图像,但同样的事情发生了。我一定错过了一些不起作用的东西。
    • 可能文件不是jpeg文件,而是DICOM图像格式。但是,如果您无法在 opencv 中阅读,则您的安装似乎有问题。看看 DICOM 图像转换:lifewire.com/dicom-file-2620657
    • 当然,图像没有 DICOM 格式。我已经用相同格式的图像训练了我的 CNN,我还在另一组 X 射线上测试了该算法,现在我想预测从用户输入的特定图像,但在这种情况下,我什至无法阅读那些图片。
    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 2016-04-26
    • 2015-04-24
    • 1970-01-01
    • 2019-06-15
    相关资源
    最近更新 更多