Davido
Davido

Reputation: 103

Skimage rgb2gray giving errors, the input array must have size 3 along

I am trying to open a JPG image with skimage.color.rgb2gray. I can read the image with imageio.imread but I get an error with skimage.color.rgb2gray:

import os  # for changing path, etc
import imageio  # for reading image files
import matplotlib.pyplot as plt  # for displaying figure/image data
import pandas as pd  # for data handling/analysis
import plotly  # for interactive plots
import plotly.express as px  # for displaying data on overlay
import plotly.graph_objects as go  # for interactive plots
import scipy.stats as ss  # for statistical testing
import scikit_posthocs as sp  # for posthoc testing

from skimage import measure, morphology
from skimage.color import rgb2gray
from skimage.filters import (gaussian, threshold_yen)
from skimage.measure import regionprops_table

path = 'C:/Users/David/Desktop'
os.chdir(path)
image_name = 'Blood.jpg'
image = imageio.imread(image_name)
img = rgb2gray(image)
img = gaussian(img, sigma=1)

plt.imshow(img, cmap='gray')
plt.show()

Errors

Traceback (most recent call last):
  File "C:\Users\David\PycharmProjects\tryinngImageProcessing\main.py", line 20, in <module>
    img = rgb2gray(image)
  File "C:\Users\David\PycharmProjects\AllPackages\lib\site-packages\skimage\_shared\utils.py", line 338, in fixed_func
    return func(*args, **kwargs)
  File "C:\Users\David\PycharmProjects\AllPackages\lib\site-packages\skimage\color\colorconv.py", line 875, in rgb2gray
    rgb = _prepare_colorarray(rgb)
  File "C:\Users\David\PycharmProjects\AllPackages\lib\site-packages\skimage\color\colorconv.py", line 140, in _prepare_colorarray
    raise ValueError(msg)
ValueError: the input array must have size 3 along `channel_axis`, got (416, 554, 4)

Upvotes: 4

Views: 15186

Answers (2)

ANIRUDH KABRA
ANIRUDH KABRA

Reputation: 21

Just solved the problem by using a different approach to convert the image into grayscale

To convert the RGB image into Grayscale use OpenCV

import cv2

image = cv2.imread(//image_path)
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

And to see the image use matplotlib

import matplotlib.pyplot as plt

plt.imshow(image) # Original Image
plt.imshow(grayscale, cmap="gray"); # Grayscale Image

Upvotes: 2

endive1783
endive1783

Reputation: 1059

A quick fix would be keeping only the first 3 coordinates

image_name = 'Blood.jpg'
image = imageio.imread(image_name)[:,:,:3]
img = rgb2gray(image)

If you actually want to use the alpha channel you could read the image using this bit of code suggested in How to load png images with 4 channels?

def read_transparent_png(filename):
    image_4channel = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
    alpha_channel = image_4channel[:,:,3]
    rgb_channels = image_4channel[:,:,:3]

    # White Background Image
    white_background_image = np.ones_like(rgb_channels, dtype=np.uint8) * 255

    # Alpha factor
    alpha_factor = alpha_channel[:,:,np.newaxis].astype(np.float32) / 255.0
    alpha_factor = np.concatenate((alpha_factor,alpha_factor,alpha_factor), axis=2)

    # Transparent Image Rendered on White Background
    base = rgb_channels.astype(np.float32) * alpha_factor
    white = white_background_image.astype(np.float32) * (1 - alpha_factor)
    final_image = base + white
    return final_image.astype(np.uint8)

It gives you a 3 channel image without throwing away the transparency channel.

image_name = 'Blood.jpg'
image = read_transparent_png(image_name) #shape is [416, 554, 3]
img = rgb2gray(image)

Upvotes: 5

Related Questions