用Python写一个基于直方图对比度的显著性检测的代码
时间: 2024-05-30 13:16:23 浏览: 120
import cv2
import numpy as np
def saliency_detection(image):
# 计算直方图
hist = cv2.calcHist([image], [0], None, [256], [0, 256])
# 归一化直方图
hist = cv2.normalize(hist, hist, 0, 1, cv2.NORM_MINMAX)
# 计算累积直方图
cum_hist = np.cumsum(hist)
# 计算显著性图
saliency_map = np.zeros_like(image)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
saliency_map[i, j] = cum_hist[image[i, j]] * 255
# 归一化显著性图
saliency_map = cv2.normalize(saliency_map, saliency_map, 0, 255, cv2.NORM_MINMAX)
return saliency_map
# 读取图像
image = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)
# 显著性检测
saliency_map = saliency_detection(image)
# 显示原图和显著性图
cv2.imshow('Image', image)
cv2.imshow('Saliency Map', saliency_map)
cv2.waitKey(0)
cv2.destroyAllWindows()
阅读全文