简体   繁体   English

(scikit图像)HOG可视化图像在保存时显示为黑色

[英](scikit-image) HOG visualization image appears black when saved

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 得到这个HOG可视化图像

在此处输入图片说明

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. 当我尝试保存该图像(.pdf或.jpg)时,生成的图像是纯黑色的。 Converting this image to PIL format and examining it with 将此图像转换为PIL格式并使用

    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 将图像转换为灰度图像,我得到以下error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cvtColor error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cvtColor error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cvtColor . 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). 作为附加信息,运行hog_img.shape返回(1632, 1224) hog_img.shape (1632, 1224) ,它只是图像的大小,我最初将其解释为意味着图像已经是灰度的(因为它似乎缺少彩色尺寸)渠道)。 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. 我收到错误error: (-215) src.type() == CV_8UC1 in function adaptiveThreshold答案似乎表明这意味着图像不在灰度中。

Finally, another bit of useful information is that running print(hog_img.dtype) on the image returns float64 . 最后,另一有用信息是在映像上运行print(hog_img.dtype)返回float64

I will continue to debug, in the meantime 在此期间,我将继续进行调试

Thanks for any thoughts :) 感谢您的任何想法:)

  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)). 使用hog_img_inv = cv2.bitwise_not(hog_img)反转图像并使用plt.figure(figsize=(15,10)) plt.imshow(hog_img_uint8_inv)显示这些行实际上在那里但很模糊(我已经包括了此处显示图像的完整性,但是您可以大麦看到它(但是请相信我,它在那里)。 I will have to do some more processing of the image to get the lines more distinguishable. 我将不得不对图像进行更多处理,以使线条更清晰。 在此处输入图片说明

  2. Running print(hog_img.dtype) showed that the dtype was float64 when (I think) it should have been uint8 . 运行print(hog_img.dtype)表明print(hog_img.dtype)应该是uint8 ,而print(hog_img.dtype)float64 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). 我通过运行hog_img_uint8 = hog_img.astype(np.uint8)修复了该问题,该问题似乎已解决了将图像传递给其他算法(例如cv2.adaptiveThreshold)的问题。

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. 使用matplotlib进行可视化没有问题。 saving the image with opencv (or skimage) saves only a black image... 使用opencv(或skimage)保存图像仅保存黑色图像...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM