Search code examples
pythonhistogramgradientscikit-imagehistogram-of-oriented-gradients

ValueError: negative dimensions are not allowed (HOG)


I am implementing the HOG(Histogram of Oriented Gradient) with below code.

import io
from skimage.io import imread, imshow
from skimage.feature import hog
from skimage import exposure
from skimage import io
import matplotlib

img = imread('cr7.jpeg')

io.imshow(img)
MC = True #Fpr color images
#MC = false #for grayscale images

hogfv, hog_image = hog(img, orientations=9,
    pixels_per_cell=(32,32),
    cells_per_block=(4,4),
    visualize = True ,
    channel_axis=MC)
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0,5))
imshow(hog_image_rescaled)

I don't know why i am getting error of dimension.

Traceback (most recent call last):
  File "main.py", line 22, in <module>
    channel_axis=MC)
  File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/_shared/utils.py", line 427, in fixed_func
    out = func(*new_args, **kwargs)
  File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/_shared/utils.py", line 348, in fixed_func
    return func(*args, **kwargs)
  File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/feature/_hog.py", line 286, in hog
    dtype=float_dtype
ValueError: negative dimensions are not allowed
(base) (env) c100-110@C100-110s-iMac-2 HOG % python main.py
Traceback (most recent call last):
  File "main.py", line 18, in <module>
    channel_axis=MC)
  File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/_shared/utils.py", line 427, in fixed_func
    out = func(*new_args, **kwargs)
  File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/_shared/utils.py", line 348, in fixed_func
    return func(*args, **kwargs)
  File "/Volumes/DATA/Djangoproject/HOG/env/lib/python3.7/site-packages/skimage/feature/_hog.py", line 286, in hog
    dtype=float_dtype
ValueError: negative dimensions are not allowed

Can anyone help me in finding solution to this error.


Solution

  • The error log says there is a problem in "line 22"

    Traceback (most recent call last):
      File "main.py", line 22, in <module>
        channel_axis=MC)
    ...
    ValueError: negative dimensions are not allowed
    

    channel_axis, it's the "channel axis"! So I guess it expects an integer, rather than a bool value.

    It is confirmed in the source code:

    channel_axis : int or None, optional
    

    If None, the image is assumed to be a grayscale (single channel) image. Otherwise, this parameter indicates which axis of the array corresponds to channels.

    I think you were trying to use multichannel, which is deprecated:

    multichannel : boolean, optional
    

    If True, the last image dimension is considered as a color channel, otherwise as spatial. This argument is deprecated: specify channel_axis instead.