0

I am trying to run this code snippet:

from scipy.stats import wasserstein_distance
from imageio import imread
import numpy as np

def get_histogram(img):
  h, w = img.shape
  hist = [0.0] * 256
  for i in range(h):
    for j in range(w):
      hist[img[i, j]] += 1
  return np.array(hist) / (h * w)

a = imread("./IMG_4835.jpg")
b = imread("./IMG_4836.jpg")
a_hist = get_histogram(a)
b_hist = get_histogram(b)
dist = wasserstein_distance(a_hist, b_hist)
print(dist)

but I get an error at:

h, w = img.shape
b = imread('b.jpg', mode='L')
ValueError: too many values to unpack (expected 2)

The original code used:

from scipy.ndimage import imread

to read the image file but since I was unable to import it, I used imread from another library instead. Could that have anything to do with the error?

x89
  • 2,798
  • 5
  • 46
  • 110
  • What is the value inside of shape? If the image has 3 color channels, the shape will be `[w, h, ch]` (or `[h, w, ch]`, or another permutation, you should check that), so you have 3 values to unpack. – Pietro Apr 10 '21 at 14:07
  • Can you see the updated code? The original version was using "L" mode but the mode parameter is invalid in imread from openCV or imageio. @Pietro – x89 Apr 10 '21 at 14:11
  • This function assumes `img` is a 2d array with values 0 to 255 (`uint8` dtype?). It will fail on a color image with 3d shape. Verify shape and dtype before using. – hpaulj Apr 10 '21 at 14:25
  • What is the result of `print(img.shape, img.dtype)`? `L` mode for jpeg means [8-bit pixels, grayscale](https://imageio.readthedocs.io/en/stable/format_jpeg-pil.html) , but is that correctly set in the numpy array that is returned? – Pietro Apr 10 '21 at 17:07
  • (4032, 3024, 3) uint8 @Pietro Currently, I am not changing images to grey scale in my code. Since there's no L mode in imread that comes from imageio – x89 Apr 10 '21 at 19:21

2 Answers2

3

h,w = img.shape[:2] should fix the problem.

Prefect
  • 1,719
  • 1
  • 7
  • 16
  • Then it gives me ```TypeError: only integer scalar arrays can be converted to a scalar index``` at ```hist[img[i, j]] += 1``` :( – x89 Apr 10 '21 at 14:21
  • The error is quite clear actually, try `hist[int(img[i, j])]`. – Prefect Apr 10 '21 at 16:24
  • already tried that too. But it would give another error on the same line then ```TypeError: only size-1 arrays can be converted to Python scalars``` – x89 Apr 10 '21 at 19:19
0

RGB image has 3 channels, your code works with 2 channels only. You could convert your images to grey:

from skimage.color import rgb2gray
from skimage import img_as_ubyte
img = img_as_ubyte(rgb2gray(img))

Or if you do not care about correct RGB 2 grey: (see https://e2eml.school/convert_rgb_to_grayscale.html)

img = np.mean(img, axis=2).astype(np.uint8)

But it looks like you are reinventing a wheel. To get histogram use:

a_hist, _ = np.histogram(a, bins=256)
b_hist, _ = np.histogram(b, bins=256)
ffsedd
  • 186
  • 9