from:http://blog.csdn.net/onezeros/article/details/6299745

先看几张效果图吧

效果图:

可以直接测试的代码:

头文件:

[cpp] view plain copy
  1. // Saliency.h: interface for the Saliency class.
  2. //
  3. //
  4. //===========================================================================
  5. //  Copyright (c) 2009 Radhakrishna Achanta [EPFL]
  6. //===========================================================================
  7. #if !defined(_SALIENCY_H_INCLUDED_)
  8. #define _SALIENCY_H_INCLUDED_
  9. #include <vector>
  10. #include <cfloat>
  11. using namespace std;
  12. class Saliency
  13. {
  14. public:
  15. Saliency();
  16. virtual ~Saliency();
  17. public:
  18. void GetSaliencyMap(
  19. const vector<unsigned int>&               inputimg,//INPUT: ARGB buffer in row-major order
  20. const int&                      width,
  21. const int&                      height,
  22. vector<double>&                   salmap,//OUTPUT: Floating point buffer in row-major order
  23. const bool&                     normalizeflag = true);//false if normalization is not needed
  24. private:
  25. void RGB2LAB(
  26. const vector<unsigned int>&               ubuff,
  27. vector<double>&                   lvec,
  28. vector<double>&                   avec,
  29. vector<double>&                   bvec);
  30. void GaussianSmooth(
  31. const vector<double>&         inputImg,
  32. const int&                      width,
  33. const int&                      height,
  34. const vector<double>&         kernel,
  35. vector<double>&                   smoothImg);
  36. //==============================================================================
  37. /// Normalize
  38. //==============================================================================
  39. void Normalize(
  40. const vector<double>&         input,
  41. const int&                      width,
  42. const int&                      height,
  43. vector<double>&                   output,
  44. const int&                      normrange = 255)
  45. {
  46. double maxval(0);
  47. double minval(DBL_MAX);
  48. {int i(0);
  49. for( int y = 0; y < height; y++ )
  50. {
  51. for( int x = 0; x < width; x++ )
  52. {
  53. if( maxval < input[i] ) maxval = input[i];
  54. if( minval > input[i] ) minval = input[i];
  55. i++;
  56. }
  57. }}
  58. double range = maxval-minval;
  59. if( 0 == range ) range = 1;
  60. int i(0);
  61. output.clear();
  62. output.resize(width*height);
  63. for( int y = 0; y < height; y++ )
  64. {
  65. for( int x = 0; x < width; x++ )
  66. {
  67. output[i] = ((normrange*(input[i]-minval))/range);
  68. i++;
  69. }
  70. }
  71. }
  72. };
  73. #endif // !defined(_SALIENCY_H_INCLUDED_)

cpp:

[cpp] view plain copy
  1. // Saliency.cpp: implementation of the Saliency class.
  2. //
  3. //
  4. //===========================================================================
  5. //  Copyright (c) 2009 Radhakrishna Achanta [EPFL]
  6. //===========================================================================
  7. #include "Saliency.h"
  8. #include <cmath>
  9. //
  10. // Construction/Destruction
  11. //
  12. Saliency::Saliency()
  13. {
  14. }
  15. Saliency::~Saliency()
  16. {
  17. }
  18. //===========================================================================
  19. /// RGB2LAB
  20. //===========================================================================
  21. void Saliency::RGB2LAB(
  22. const vector<unsigned int>&               ubuff,
  23. vector<double>&                   lvec,
  24. vector<double>&                   avec,
  25. vector<double>&                   bvec)
  26. {
  27. int sz = int(ubuff.size());
  28. lvec.resize(sz);
  29. avec.resize(sz);
  30. bvec.resize(sz);
  31. for( int j = 0; j < sz; j++ )
  32. {
  33. int r = (ubuff[j] >> 16) & 0xFF;
  34. int g = (ubuff[j] >>  8) & 0xFF;
  35. int b = (ubuff[j]      ) & 0xFF;
  36. double xval = 0.412453 * r + 0.357580 * g + 0.180423 * b;
  37. double yval = 0.212671 * r + 0.715160 * g + 0.072169 * b;
  38. double zVal = 0.019334 * r + 0.119193 * g + 0.950227 * b;
  39. xval /= (255.0 * 0.950456);
  40. yval /=  255.0;
  41. zVal /= (255.0 * 1.088754);
  42. double fX, fY, fZ;
  43. double lval, aval, bval;
  44. if (yval > 0.008856)
  45. {
  46. fY = pow(yval, 1.0 / 3.0);
  47. lval = 116.0 * fY - 16.0;
  48. }
  49. else
  50. {
  51. fY = 7.787 * yval + 16.0 / 116.0;
  52. lval = 903.3 * yval;
  53. }
  54. if (xval > 0.008856)
  55. fX = pow(xval, 1.0 / 3.0);
  56. else
  57. fX = 7.787 * xval + 16.0 / 116.0;
  58. if (zVal > 0.008856)
  59. fZ = pow(zVal, 1.0 / 3.0);
  60. else
  61. fZ = 7.787 * zVal + 16.0 / 116.0;
  62. aval = 500.0 * (fX - fY)+128.0;
  63. bval = 200.0 * (fY - fZ)+128.0;
  64. lvec[j] = lval;
  65. avec[j] = aval;
  66. bvec[j] = bval;
  67. }
  68. }
  69. //==============================================================================
  70. /// GaussianSmooth
  71. ///
  72. /// Blur an image with a separable binomial kernel passed in.
  73. //==============================================================================
  74. void Saliency::GaussianSmooth(
  75. const vector<double>&         inputImg,
  76. const int&                      width,
  77. const int&                      height,
  78. const vector<double>&         kernel,
  79. vector<double>&                   smoothImg)
  80. {
  81. int center = int(kernel.size())/2;
  82. int sz = width*height;
  83. smoothImg.clear();
  84. smoothImg.resize(sz);
  85. vector<double> tempim(sz);
  86. int rows = height;
  87. int cols = width;
  88. //--------------------------------------------------------------------------
  89. // Blur in the x direction.
  90. //---------------------------------------------------------------------------
  91. {int index(0);
  92. for( int r = 0; r < rows; r++ )
  93. {
  94. for( int c = 0; c < cols; c++ )
  95. {
  96. double kernelsum(0);
  97. double sum(0);
  98. for( int cc = (-center); cc <= center; cc++ )
  99. {
  100. if(((c+cc) >= 0) && ((c+cc) < cols))
  101. {
  102. sum += inputImg[r*cols+(c+cc)] * kernel[center+cc];
  103. kernelsum += kernel[center+cc];
  104. }
  105. }
  106. tempim[index] = sum/kernelsum;
  107. index++;
  108. }
  109. }}
  110. //--------------------------------------------------------------------------
  111. // Blur in the y direction.
  112. //---------------------------------------------------------------------------
  113. {int index = 0;
  114. for( int r = 0; r < rows; r++ )
  115. {
  116. for( int c = 0; c < cols; c++ )
  117. {
  118. double kernelsum(0);
  119. double sum(0);
  120. for( int rr = (-center); rr <= center; rr++ )
  121. {
  122. if(((r+rr) >= 0) && ((r+rr) < rows))
  123. {
  124. sum += tempim[(r+rr)*cols+c] * kernel[center+rr];
  125. kernelsum += kernel[center+rr];
  126. }
  127. }
  128. smoothImg[index] = sum/kernelsum;
  129. index++;
  130. }
  131. }}
  132. }
  133. //===========================================================================
  134. /// GetSaliencyMap
  135. ///
  136. /// Outputs a saliency map with a value assigned per pixel. The values are
  137. /// normalized in the interval [0,255] if normflag is set true (default value).
  138. //===========================================================================
  139. void Saliency::GetSaliencyMap(
  140. const vector<unsigned int>&       inputimg,
  141. const int&                      width,
  142. const int&                      height,
  143. vector<double>&                   salmap,
  144. const bool&                     normflag)
  145. {
  146. int sz = width*height;
  147. salmap.clear();
  148. salmap.resize(sz);
  149. vector<double> lvec(0), avec(0), bvec(0);
  150. RGB2LAB(inputimg, lvec, avec, bvec);
  151. //--------------------------
  152. // Obtain Lab average values
  153. //--------------------------
  154. double avgl(0), avga(0), avgb(0);
  155. {for( int i = 0; i < sz; i++ )
  156. {
  157. avgl += lvec[i];
  158. avga += avec[i];
  159. avgb += bvec[i];
  160. }}
  161. avgl /= sz;
  162. avga /= sz;
  163. avgb /= sz;
  164. vector<double> slvec(0), savec(0), sbvec(0);
  165. //----------------------------------------------------
  166. // The kernel can be [1 2 1] or [1 4 6 4 1] as needed.
  167. // The code below show usage of [1 2 1] kernel.
  168. //----------------------------------------------------
  169. vector<double> kernel(0);
  170. kernel.push_back(1.0);
  171. kernel.push_back(2.0);
  172. kernel.push_back(1.0);
  173. GaussianSmooth(lvec, width, height, kernel, slvec);
  174. GaussianSmooth(avec, width, height, kernel, savec);
  175. GaussianSmooth(bvec, width, height, kernel, sbvec);
  176. {for( int i = 0; i < sz; i++ )
  177. {
  178. salmap[i] = (slvec[i]-avgl)*(slvec[i]-avgl) +
  179. (savec[i]-avga)*(savec[i]-avga) +
  180. (sbvec[i]-avgb)*(sbvec[i]-avgb);
  181. }}
  182. if( true == normflag )
  183. {
  184. vector<double> normalized(0);
  185. Normalize(salmap, width, height, normalized);
  186. swap(salmap, normalized);
  187. }
  188. }

关于代码的使用说明:

[cpp] view plain copy
  1. This file explains the usage of Saliency.h and Saliency.cpp files. The former contains the declaration of the Saliency class and its member functions and the later contains the respective definitions.
  2. Sample usage:
  3. #include "Saliency.h"
  4. void main()
  5. {
  6. // Assume we already have an unsigned integer buffer inputImg of
  7. // inputWidth and inputHeight (in row-major order).
  8. // Each unsigned integer has 32 bits and contains pixel data in ARGB
  9. // format. I.e. From left to right, the first 8 bits contain alpha
  10. // channel value and are not used in our case. The next 8 bits
  11. // contain R channel value; the next 8 bits contain G channel value;
  12. // the last 8 bits contain the B channel value.
  13. //
  14. // Now create a Saliency object and call the GetSaliencyMap function on it.
  15. Saliency sal;
  16. vector<double> salmap(0);
  17. sal.GetSaliencyMap(inputImg, inputWidth, inputHeight, salmap);
  18. // salmap is a floating point output (in row major order)
  19. }

我自己写的测试主程序:

可以指定一个文件夹,程序保存该文件夹下所有jpg文件的处理结果

[cpp] view plain copy
  1. #include "Saliency.h"
  2. #include <cv.h>
  3. #include <cxcore.h>
  4. #include <highgui.h>
  5. #include "windows.h"
  6. #include <iostream>
  7. #include <cassert>
  8. using namespace std;
  9. int main(int argc,char** argv)
  10. {
  11. WIN32_FIND_DATAA FileData;
  12. HANDLE hFind;
  13. hFind = FindFirstFileA((LPCSTR)"Imgs/*.jpg",&FileData);
  14. if (hFind == INVALID_HANDLE_VALUE) {
  15. printf ("Invalid File Handle. GetLastError reports %d/n",
  16. GetLastError ());
  17. return (0);
  18. }
  19. Saliency sal;
  20. vector<double> salmap(0);
  21. while (FindNextFileA(hFind, &FileData)) {
  22. cout<<FileData.cFileName<<endl;
  23. string name("Imgs/");
  24. name.append(FileData.cFileName);
  25. IplImage* img=cvLoadImage(name.c_str());
  26. if (!img) {
  27. cout<<"failed to load image"<<endl;
  28. break;
  29. }
  30. assert(img->nChannels==3);
  31. vector<unsigned int >imgInput;
  32. vector<double> imgSal;
  33. //IplImage to vector
  34. for (int h=0;h<img->height;h++) {
  35. unsigned char*p=(unsigned char*)img->imageData+h*img->widthStep;
  36. for (int w=0;w<img->width;w++) {
  37. unsigned int t=0;
  38. t+=*p++;
  39. t<<=8;
  40. t+=*p++;
  41. t<<=8;
  42. t+=*p++;
  43. imgInput.push_back(t);
  44. }
  45. }
  46. sal.GetSaliencyMap(imgInput, img->width, img->height, imgSal);
  47. //vector to IplImage
  48. int index=0;
  49. IplImage* imgout=cvCreateImage(cvGetSize(img),IPL_DEPTH_64F ,1);
  50. for (int h=0;h<imgout->height;h++) {
  51. double*p=(double*)(imgout->imageData+h*imgout->widthStep);
  52. for (int w=0;w<imgout->width;w++) {
  53. *p++=imgSal[index++];
  54. }
  55. }
  56. name.append(".saliency.jpg");
  57. cvSaveImage(name.c_str(),imgout);
  58. cvReleaseImage(&img);
  59. cvReleaseImage(&imgout);
  60. }
  61. FindClose(&hFind);
  62. return 0;
  63. }

该代码的主页:http://ivrg.epfl.ch/supplementary_material/RK_ICIP2010/index.html

清华的最新研究:http://cg.cs.tsinghua.edu.cn/people/~cmm/saliency/

图像显著区域检测代码及其效果图 saliency region detection相关推荐

  1. 基于马尔科夫吸收概率的显着区域检测_MAP(Saliency Region Detection Based on Markov Absorption Probabilities)

    详见卢湖川主页 在本文中,我们通过利用显着性检测与马尔科夫吸收概率之间的关系,提出一种新颖的自下而上的突出物体检测方法. 首先,我们通过把加权图上部分图像边界作为背景(仅使用左侧和顶侧作为背景),通过 ...

  2. python图像清晰度_图像的模糊检测方法

    关键字:blur detection Function:图像的清晰度检测英文表达为 image blue detection; 以此为关键字可以找到很多有关清晰度检测的demo和算法. 图像的清晰度检 ...

  3. 显著性图matlab,cvpr14_saliency_code 2014上的关于图像显著性区域的检测matlab代码。 271万源代码下载- www.pudn.com...

    文件名称: cvpr14_saliency_code下载  收藏√  [ 5  4  3  2  1 ] 开发工具: matlab 文件大小: 4413 KB 上传时间: 2014-09-07 下载次 ...

  4. 图像中的天空区域检测!

    一.引言 天空区域作为图像中的背景信息,为机器人导航.自动驾驶等领域的图像理解提供了重要依据,因此如何检测图像中的天空区域非常重要,本文提供了一个基于传统视觉算法(非机器学习方法)的提取天空区域的方法 ...

  5. 车牌识别算法实现及其代码实现之一:车牌区域检测

    本文地址:http://blog.csdn.net/shanglianlm/article/details/78005815 本文主要处理汽车车牌的识别过程,包括三个步骤,一:车牌区域检测,本文利用车 ...

  6. OpenCV与图像处理学习八——图像边缘提取(Canny检测代码)

    OpenCV与图像处理学习八--图像边缘提取(Canny检测代码) 一.图像梯度 1.1 梯度 1.2 图像梯度 二.梯度图与梯度算子 2.1模板卷积 2.2 梯度图 2.3 梯度算子 2.3.1 R ...

  7. android代码查找图像,Android平台上利用opencv进行图像的边沿检测

    原标题:Android平台上利用opencv进行图像的边沿检测 近开始接触opencv for Android,从网上down了图像的边沿检测的代码. 测试图片: 在Android2.3.1模拟器上跑 ...

  8. 视觉显著性 matlab,转载图像/视觉显著性检测技术发展情况梳理(Saliency Detection、Visual Attention)...

    图像/视觉显著性检测技术发展情况梳理(Saliency Detection.Visual Attention) Sason@CSDN 转载:http://blog.csdn.net/anshan198 ...

  9. 深度学习实战 | 智慧工地安全帽和危险区域检测系统(代码已开源!)

    文章目录 原创声明 前言 Smart_Construction 指标 yolov5s 为基础训练,`epoch = 50` yolov5m 为基础训练,`epoch = 100` yolov5l 为基 ...

最新文章

  1. 分享jQuery对象和Javascript对象之间的转换代码
  2. 超强实时人像抠图算法开源,随心所欲背景替换!
  3. android python 纠正图片,Python脚本替换Android资源(包名,图片,文件内容)
  4. SAP Spartacus 自定义 Component 的使用 - SimpleResponsiveBannerComponent
  5. CoreCLR源码探索(五) GC内存收集器的内部实现 调试篇
  6. [深度学习-实践]Transformer模型训练IMDB-tensorflow2 keras
  7. 7-12 藏头诗 (15 分)
  8. [bug解决] TensorFlow安装错误:ERROR Cannot uninstall ‘wrapt‘
  9. css3 弹性盒模型 变化
  10. 清华姚班、斯坦福博士、普林斯顿NLP组创始人 陈丹琦 获小诺奖之称的斯隆奖!...
  11. python 数据挖掘 简书_Python数据挖掘与分析----Pandas常用知识
  12. 2021-09-10二叉树的层序遍历
  13. 员工效率低下,责任在管理层的数学解释和分析
  14. 编程为什么有趣?浅谈编程的快乐。
  15. 高效能人士的7个习惯
  16. Android中的Drawable(一)
  17. Ubuntu 16.04 解决WPS for Linux提示“系统缺失字体symbol、wingdings、wingdings 2、wingdings 3、webding”的问题
  18. Windows定时关机小程序
  19. 抖音音乐怎么下载 mp3格式转换器如何使用
  20. Pipeline(流水线)模式

热门文章

  1. 多项式乘法c语言,急!!!!c语言:求n次多项式的加法和乘法
  2. C中printf函数的实现原理
  3. OVS bridge和port(三十三)
  4. linux恢复出厂设置_怎么恢复tp-link路由器出厂设置 恢复tp-link出厂设置方法【详解】...
  5. python词频统计_python统计词频的三种方法
  6. c语言做小学生测验程序,[转载]程序设计方法学课程设计--小学生算术四则运算测试程序(C)...
  7. java 定义类变量初始化吗_Java的变量有哪些类型?变量如何定义?如何初始化?请说明理由并举例_学小易找答案...
  8. GDB attach 用法
  9. sort,uniq,wc,history命令简介
  10. C# HasRows 和 Read的区别