[英]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.