简体   繁体   English

如何使用 OpenCV 和 numpy 从 Drive 目录读取和显示图像?

[英]How to read and display images from a Drive directory using OpenCV and numpy?

I'm trying to read an image from a directory on my drive, for this I use OpenCV, I convert the image to a numpy array of 32-bit floats and then display it, the problem I have is that the image is not displayed.我正在尝试从驱动器上的目录中读取图像,为此我使用 OpenCV,我将图像转换为 32 位浮点数的 numpy 数组,然后显示它,我遇到的问题是图像未显示. The images correspond to chest X-rays and I'm working at Google Colab.这些图像对应于胸部 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.正如所见,就好像图像没有被很好地阅读。

Thinking that I was not accessing the directory correctly, I used the os.listdir method, as shown in the following code, the images are not shown anyway.以为我没有正确访问该目录,我使用了 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.这是第二个代码显示的结果。

I hope someone can help me.我希望有一个人可以帮助我。 Thanks in advance.提前致谢。

Let me know if this works让我知道这个是否奏效

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python和OpenCV从目录中读取图像? - How to read images from a directory with Python and OpenCV? 如何使用 OpenCV 读取图像目录 - How to read in a directory of images with OpenCV 无法从 D 驱动器 [OpenCV] [Python] 读取或写入图像 - Can't read or write images from D Drive [OpenCV] [Python] 从特定目录读取多个图像,使用 python 和 opencv 将它们保存在另一个目录中 - Read multiple images from specific directory, prepossessed and save them another directory using python and opencv 使用Python和OpenCV处理目录中的图像 - Processing Images from a directory using Python and OpenCV 使用Python和OpenCv顺序显示一个目录下的所有图片 - Using Python and OpenCv to sequentially display all the images in a directory 使用 Tkinter 显示来自 numpy 数组的图像 - Using Tkinter to display images from a numpy array 如何在 Tkinter 中将目录中的图像显示为标签? - How to display images from a directory as labels in Tkinter? 使用 numpy 和 PIL 对目录中的多个图像进行平均 - Averaging multiple images from directory using numpy and PIL 如何在 Python 中使用 opencv 从文件夹中读取图像序列(im1、im2、...)? - How can I read a sequence of images (im1, im2, ...) from a folder using opencv in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM