2

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. The images correspond to chest X-rays and I'm working at 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.

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.

0x55b1E06FF
  • 538
  • 1
  • 9
  • 24

1 Answers1

1

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()
Mario Abbruscato
  • 819
  • 1
  • 7
  • 9
  • I tried your code, the cv2.imshow () method in Colab is replaced by cv2_inshow (), but the result is the same, there's no image displayed. `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))` `from google.colab.patches import cv2_imshow` `cv2_imshow(img2)` `cv2.waitKey(0)` – 0x55b1E06FF Feb 06 '21 at 16:37
  • are you sure the image you read is a valid jpg image ? Can you view the image in another way , for example using Paint (on windows) or feh (on linux) ? – Mario Abbruscato Feb 06 '21 at 16:44
  • Yes, the images are read correctly in other applications. I've even tried reading images other than X-rays and the same thing happens. I must be missing something that is not working. – 0x55b1E06FF Feb 06 '21 at 16:49
  • May be the file is not a jpeg file but a DICOM image format. But if you can't read in opencv something seems wrong in your installation. A look at DICOM images conversion: https://www.lifewire.com/dicom-file-2620657 – Mario Abbruscato Feb 06 '21 at 16:52
  • For sure, the images have no DICOM format. I've trained my CNN with images of the same format, I have also tested the algorithm on another group of x-rays and now I want to predict specific images that are entered from a user, but in this case I cannot even read those images. – 0x55b1E06FF Feb 06 '21 at 16:58
  • In the other cases, I didn't have the need to use openCV. – 0x55b1E06FF Feb 06 '21 at 17:05
  • Try to reinstall python opencv in accordance with your os system. – Mario Abbruscato Feb 06 '21 at 17:11
  • I reinstalled it using: `!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python`, but there's no change – 0x55b1E06FF Feb 06 '21 at 17:47
  • what distro os are you using ? – Mario Abbruscato Feb 06 '21 at 18:17
  • I noticed that the image shows up when I only use `img = cv.imread ('T-NORMAL- (5) .png', 0)`, but when I access my drive directory, `img_r = cv2.imread ( '/ content / drive / MyDrive / images / T-NORMAL- (5) .png', 0) `, the image is no longer displayed and it is the same image. So the problem is with the addressing, any suggestions on that? – 0x55b1E06FF Feb 06 '21 at 19:08
  • I realized that there must be no spaces to access the directories, with that consideration; the initial code works. – 0x55b1E06FF Feb 06 '21 at 20:10
  • Nice , so every thing it's ok now. – Mario Abbruscato Feb 06 '21 at 20:18
  • Yeah, thanks! Until the next question haha! – 0x55b1E06FF Feb 07 '21 at 00:25