1. LC算法
参考论文:Visual Attention Detection in Video Sequences Using Spatiotemporal Cues Yun Zhai and Mubarak Shah. Page 4-5。
算法原理部分见论文的第四第五页。简单说就是计算某个像素在整个图像上的全局对比度,即该像素与图像中其他所有像素在颜色上的距离之和作为该像素的显著值。
When viewers watch a video sequence, they are attracted not only by the interesting events, but also sometimes by the interesting objects in still images. This is referred as the spatial attention. Based on the psychological studies, human perception system is sensitive to the contrast of visual signals, such as color, intensity and texture. Taking this as the underlying assumption, we propose an e±cient method for computing the spatial saliency maps using the color statistics of images. The algorithm is designed with a linear computational complexity with respect to the number of image pixels. The saliency map of an image is built upon the color contrast between image pixels. The saliency value of a pixel in an image is defined as,
where the value of is in the range of [0; 255], and represent the color distance metric。
这里如果直接用公式时间复杂度太高,所以用直方图优化。下面是python版的代码实现:
def cal_dist(hist):
dist = {}
for gray in range(256):
value = 0.0
for k in range(256):
value += hist[k][0] * abs(gray - k)
dist[gray] = value
return dist
def LC(image_gray):
image_height = image_gray.shape[0]
image_width = image_gray.shape[1]
image_gray_copy = np.zeros((image_height, image_width))
hist_array = cv2.calcHist([image_gray], [0], None, [256], [0.0, 256.0]) # 直方图,统计图像中每个灰度值的数量
gray_dist = cal_dist(hist_array) # 灰度值与其他值的距离
# print(gray_dist)
for i in range(image_width):
for j in range(image_height):
temp = image_gray[j][i]
image_gray_copy[j][i] = gray_dist[temp]
image_gray_copy = (image_gray_copy - np.min(image_gray_copy)) / (np.max(image_gray_copy) - np.min(image_gray_copy))
return image_gray_copy
if __name__ == '__main__':
img = cv2.imread('***.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
saliency_image = LC(image_gray)
2. HC算法
参考论文: 2011 CVPR Global Contrast based salient region detection Ming-Ming Cheng
这篇论文有相关代码可以直接下载的,不过需要向作者索取解压密码 ,有pudn账号的朋友可以直接在pudn上下载,不过那个下载的代码是用 opencv的低版本写的,下载后需要自己配置后才能运行,并且似乎只有前一半能运行(显著性检测部分)。
论文提出了HC和RC两种显著性检测的算法,在本质上,HC和上面的LC没有区别,但是HC考虑了彩色信息,而不是像LC那样只用像素的灰度信息,由于彩色图像最多有256 x 256 x 256种颜色,因此直接基于直方图技术的方案不太可行了。但是实际上一幅彩色图像并不会用到那么多种颜色,因此,作者提出了降低颜色数量的方案,将RGB各分量分别映射成12等份,则隐射后的图最多只有12 x 12 x 12种颜色,这样就可以构造一个较小的直方图用来加速,但是由于过渡量化会对结果带来一定的瑕疵。因此作者又用了一个平滑的过程。 最后和LC不同的是,作者的处理时在Lab空间进行的,而由于Lab空间和RGB并不是完全对应的,其量化过程还是在RGB空间完成的。
3. FT算法
参考论文: Frequency-tuned Salient Region Detection, Radhakrishna Achantay, Page 4-5, 2009 CVPR
这篇论文对显著性检测提出了以下5个指标:
Emphasize the largest salient objects.
Uniformly highlight whole salient regions.
Establish well-defined boundaries of salient objects.
Disregard high frequencies arising from texture, noise and blocking artifacts.
Efficiently output full resolution saliency maps.
其求解过程非常简单:
主要利用的是颜色特征和亮度特征。
- 对图像img进行高斯滤波得到gfrgb;
- 将图像imgrgb由RGB颜色空间转换为LAB颜色空间imglab;
- 对转换后的图像imglab 的L,A,B三个通道的图像分别取均值得到lm,am,bm;
- 计算显著值,即对分别对三个通道的均值图像和滤波得到的图像取欧氏距离并求和;
- 利用最大值对显著图归一化。
def FT(src):
lab = cv2.cvtColor(src,cv2.COLOR_BGR2LAB)
gaussian_blur=cv2.GaussianBlur(src,(5,5),0)
mean_lab = np.mean(lab,axis=(0,1))
print(mean_lab.shape)
salient_map = (gaussian_blur - mean_lab)*(gaussian_blur - mean_lab)
salient_map = (salient_map-np.amin(salient_map))/(np.amax(salient_map)-np.amin(salient_map))
return salient_map
4. AC算法
参考论文:Salient Region Detection and Segmentation Radhakrishna Achanta, Francisco Estrada, Patricia Wils, and Sabine SÄusstrunk 2008 , Page 4-5
AC算法也是Achanta等提出的,与FT算法类似,只是在求欧式距离时使用的均值不再是整幅图像的均值,而是选取不同大小邻域内的均值(三种大小)分别求取欧式距离,再相加得到。
这篇论文提出的算法的思想用其论文的一句话表达就是:
saliency is determined as the local contrast of an image region with respect to its neighborhood at various scales.
算法实现过程如下,选取三个邻域大小分别为边长为的正方形区域:
- 读取图像,进行高斯滤波,并转换到lab空间
- 分别求取该点附近邻域lab空间的均值。以及邻域lab空间的均值和邻域lab空间的均值。
- 数据融合,
;
;
; - 显著图
下面是C++版的实现:
void SalientRegionDetectionBasedonAC(Mat &src,int MinR2, int MaxR2,int Scale){
Mat Lab;
cvtColor(src, Lab, CV_BGR2Lab);
int row=src.rows,col=src.cols;
int Sal_org[row][col];
memset(Sal_org,0,sizeof(Sal_org));
Mat Sal=Mat::zeros(src.size(),CV_8UC1 );
Point3_<uchar>* p;
Point3_<uchar>* p1;
int val;
Mat filter;
int max_v=0;
int min_v=1<<28;
for (int k=0;k<Scale;k++){
int len=(MaxR2 - MinR2) * k / (Scale - 1) + MinR2;
blur(Lab, filter, Size(len,len ));
for (int i=0;i<row;i++){
for (int j=0;j<col;j++){
p=Lab.ptr<Point3_<uchar> > (i,j);
p1=filter.ptr<Point3_<uchar> > (i,j);
val=sqrt( (p->x - p1->x)*(p->x - p1->x)+ (p->y - p1->y)*(p->y-p1->y) + (p->z - p1->z)*(p->z - p1->z) );
Sal_org[i][j]+=val;
if(k==Scale-1){
max_v=max(max_v,Sal_org[i][j]);
min_v=min(min_v,Sal_org[i][j]);
}
}
}
}
cout<<max_v<<" "<<min_v<<endl;
int X,Y;
for (Y = 0; Y < row; Y++)
{
for (X = 0; X < col; X++)
{
Sal.at<uchar>(Y,X) = (Sal_org[Y][X] - min_v)*255/(max_v - min_v); // 计算全图每个像素的显著性
//Sal.at<uchar>(Y,X) = (Dist[gray[Y][X]])*255/(max_gray); // 计算全图每个像素的显著性
}
}
imshow("sal",Sal);
waitKey(0);
}
参考链接:
https://cloud.tencent.com/developer/article/1011756
https://blog.csdn.net/cai13160674275/article/details/72991049