1

I am new to computer vision and image processing and am using this code

from skimage.feature import hog
hog_list, hog_img = hog(test_img_gray, 
                        orientations=8, 
                        pixels_per_cell=(16, 16), cells_per_block=(1, 1),
                        block_norm='L1', 
                        visualise=True,
                        feature_vector=True)
plt.figure(figsize=(15,10))
plt.imshow(hog_img)

to get this HOG visualization image

enter image description here

I have 2 questions at this point:

  1. When I try to save this image (as a .pdf or .jpg) the resulting image is pure black. Converting this image to PIL format and examining it with

    hog_img_pil = Image.fromarray(hog_img) hog_img_pil.show()

still shows the image as pure black. Why is this happening and how can I fix it?

  1. When I try to run this code

    hog_img = cv2.cvtColor(hog_img, cv2.COLOR_BGR2GRAY)

to convert the image to grayscale I get the error error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cvtColor. What do I need to do to get this image in grayscale and why would this be happening?

As additional information, running hog_img.shape returns (1632, 1224) which is just the size of the image, which I had initially interpreted to mean that the image is already is already in grayscale (since it appears to be lacking a dimension for color channel). However, when I then tried to run

test_img_bw = cv2.adaptiveThreshold(
    src=hog_img, 
    maxValue=255, 
    adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 
    thresholdType=cv2.THRESH_BINARY, 
    blockSize=115, C=4)

I got the error error: (-215) src.type() == CV_8UC1 in function adaptiveThreshold which this answer seems to indicate means that the image is not in grayscale.

Finally, another bit of useful information is that running print(hog_img.dtype) on the image returns float64.

I will continue to debug, in the meantime

Thanks for any thoughts :)

lampShadesDrifter
  • 3,925
  • 8
  • 40
  • 102
  • 1
    it's pretty obvious that all your problems are caused by having the wrong datatype. you have to convert your data from float64 to something the algorithms can handle. refer to the reference documentations and the assertion errors. – Piglet Aug 11 '17 at 10:23

2 Answers2

0
  1. Inverting the image with hog_img_inv = cv2.bitwise_not(hog_img) and using plt.figure(figsize=(15,10)) plt.imshow(hog_img_uint8_inv) showed that the lines were in fact there but are very faint (I've included the image here for comletness, but you can barley see it (but trust me, it's there)). I will have to do some more processing of the image to get the lines more distinguishable. enter image description here

  2. Running print(hog_img.dtype) showed that the dtype was float64 when (I think) it should have been uint8. I fixed this by running hog_img_uint8 = hog_img.astype(np.uint8) which seems to have fixed the problem with passing the image to other algorithms (eg. cv2.adaptiveThreshold).

lampShadesDrifter
  • 3,925
  • 8
  • 40
  • 102
0

If had the same problem. But if you look inside the docu, they also use this code for better visualisation:

# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02))

But I still have the same problem. Visualisation with matplotlib is no problem. saving the image with opencv (or skimage) saves only a black image...

Pablo
  • 131
  • 1
  • 4