如何获得显着图的阈值图像?

问题描述

我目前有下面一张图片的显着性图,光谱显着性和细粒度显着性图像如下通过以下代码获得:

import cv2

imgpath = r'Content Image.jpg'
image = cv2.imread(imgpath)

width = 350
height = 450
dim = (width,height)
 
# resize image
resized = cv2.resize(image,dim,interpolation = cv2.INTER_AREA)

saliency = cv2.saliency.StaticSaliencySpectralResidual_create()
(success,saliencyMap) = saliency.computeSaliency(resized)
saliencyMap = (saliencyMap * 255).astype("uint8")
cv2.imshow("Image",resized)
cv2.imshow("Output",saliencyMap)
cv2.waitKey(0)
cv2.destroyAllWindows()

saliency = cv2.saliency.StaticSaliencyFineGrained_create()
(success,saliencyMap) = saliency.computeSaliency(resized)

enter image description here

enter image description here

enter image description here

所有这些都是有道理的,我明白为什么会得到它们。

但是,当我尝试使用以下代码获取阈值图时:

ret,threshMap = cv2.threshold(saliencyMap.astype("uint8"),120,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imshow("Image",saliencyMap)
cv2.imshow("Thresh",threshMap)
cv2.waitKey(0)

我得到以下图像:

enter image description here

不太确定为什么会出现这种情况,因为我很确定我已经关注了我在网上找到的所有内容,非常感谢任何帮助。

解决方法

saliencyMap 的值介于 0 和 1 之间。您需要将这些值重新调整为 0-255 范围。 然后,决定是否需要 otsu 阈值或手动阈值。给定值 120 对 Otsu 二值化方法没有影响,因为它本身会自动确定阈值。

ret,threshMap = cv2.threshold((saliencyMap * 255).astype('uint8'),255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imshow("Image",resized)
cv2.imshow("Output",saliencyMap)
cv2.imshow("Thresh",threshMap)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出大津二值化:

otsu thresholded

或手动输入120阈值

ret,120,cv2.THRESH_BINARY)

Manual 120 value threshold