(scikit-image) HOG 可视化图像在保存时显示为黑色
(scikit-image) HOG visualization image appears black when saved
我是计算机视觉和图像处理的新手,正在使用此代码
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)
得到这张HOG可视化图像
此时我有两个问题:
当我尝试保存此图像(作为 .pdf 或 .jpg)时,生成的图像是纯黑色的。将此图像转换为 PIL 格式并使用
检查它
hog_img_pil = Image.fromarray(hog_img)
hog_img_pil.show()
图像仍显示为纯黑色。为什么会发生这种情况,我该如何解决?
当我尝试运行这段代码时
hog_img = cv2.cvtColor(hog_img, cv2.COLOR_BGR2GRAY)
将图像转换为灰度时出现错误 error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cvtColor
。我需要做什么才能获得此灰度图像,为什么会发生这种情况?
作为附加信息,运行ning hog_img.shape
returns (1632, 1224)
这只是图像的大小,我最初将其解释为图像是已经是灰度(因为它似乎缺少颜色通道的维度)。但是,当我随后尝试 运行
test_img_bw = cv2.adaptiveThreshold(
src=hog_img,
maxValue=255,
adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
thresholdType=cv2.THRESH_BINARY,
blockSize=115, C=4)
我收到错误 error: (-215) src.type() == CV_8UC1 in function adaptiveThreshold
,this 答案似乎表明图像不是灰度图像。
最后,另一个有用的信息是图像 运行ning print(hog_img.dtype)
returns float64
.
我会继续调试,同时
感谢任何想法:)
用hog_img_inv = cv2.bitwise_not(hog_img)
反转图像并使用
plt.figure(figsize=(15,10))
plt.imshow(hog_img_uint8_inv)
显示线条实际上在那里但非常微弱(为了方便起见,我在此处包含了图像,但你可以大麦看到它(但相信我,它就在那里))。我将不得不对图像进行更多处理以使线条更容易区分。
运行 print(hog_img.dtype)
表明 dtype 是 float64
而(我认为)它应该是 uint8
。我通过 运行 hog_img_uint8 = hog_img.astype(np.uint8)
解决了这个问题,这似乎解决了将图像传递给其他算法的问题(例如 cv2.adaptiveThreshold)。
如果有同样的问题。但是如果你看一下 docu,他们也使用这个代码来更好地可视化:
# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02))
但是我还是遇到了同样的问题。用matplotlib可视化没问题。用opencv(或skimage)保存图像只会保存黑色图像...
我是计算机视觉和图像处理的新手,正在使用此代码
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)
得到这张HOG可视化图像
此时我有两个问题:
当我尝试保存此图像(作为 .pdf 或 .jpg)时,生成的图像是纯黑色的。将此图像转换为 PIL 格式并使用
检查它hog_img_pil = Image.fromarray(hog_img) hog_img_pil.show()
图像仍显示为纯黑色。为什么会发生这种情况,我该如何解决?
当我尝试运行这段代码时
hog_img = cv2.cvtColor(hog_img, cv2.COLOR_BGR2GRAY)
将图像转换为灰度时出现错误 error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cvtColor
。我需要做什么才能获得此灰度图像,为什么会发生这种情况?
作为附加信息,运行ning hog_img.shape
returns (1632, 1224)
这只是图像的大小,我最初将其解释为图像是已经是灰度(因为它似乎缺少颜色通道的维度)。但是,当我随后尝试 运行
test_img_bw = cv2.adaptiveThreshold(
src=hog_img,
maxValue=255,
adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
thresholdType=cv2.THRESH_BINARY,
blockSize=115, C=4)
我收到错误 error: (-215) src.type() == CV_8UC1 in function adaptiveThreshold
,this 答案似乎表明图像不是灰度图像。
最后,另一个有用的信息是图像 运行ning print(hog_img.dtype)
returns float64
.
我会继续调试,同时
感谢任何想法:)
用
hog_img_inv = cv2.bitwise_not(hog_img)
反转图像并使用plt.figure(figsize=(15,10)) plt.imshow(hog_img_uint8_inv)
显示线条实际上在那里但非常微弱(为了方便起见,我在此处包含了图像,但你可以大麦看到它(但相信我,它就在那里))。我将不得不对图像进行更多处理以使线条更容易区分。运行
print(hog_img.dtype)
表明 dtype 是float64
而(我认为)它应该是uint8
。我通过 运行hog_img_uint8 = hog_img.astype(np.uint8)
解决了这个问题,这似乎解决了将图像传递给其他算法的问题(例如 cv2.adaptiveThreshold)。
如果有同样的问题。但是如果你看一下 docu,他们也使用这个代码来更好地可视化:
# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02))
但是我还是遇到了同样的问题。用matplotlib可视化没问题。用opencv(或skimage)保存图像只会保存黑色图像...