图像显著区域检测代码及其效果图 saliency region detection
from:http://blog.csdn.net/onezeros/article/details/6299745
先看几张效果图吧
效果图:
可以直接测试的代码:
头文件:
- // Saliency.h: interface for the Saliency class.
- //
- //
- //===========================================================================
- // Copyright (c) 2009 Radhakrishna Achanta [EPFL]
- //===========================================================================
- #if !defined(_SALIENCY_H_INCLUDED_)
- #define _SALIENCY_H_INCLUDED_
- #include <vector>
- #include <cfloat>
- using namespace std;
- class Saliency
- {
- public:
- Saliency();
- virtual ~Saliency();
- public:
- void GetSaliencyMap(
- const vector<unsigned int>& inputimg,//INPUT: ARGB buffer in row-major order
- const int& width,
- const int& height,
- vector<double>& salmap,//OUTPUT: Floating point buffer in row-major order
- const bool& normalizeflag = true);//false if normalization is not needed
- private:
- void RGB2LAB(
- const vector<unsigned int>& ubuff,
- vector<double>& lvec,
- vector<double>& avec,
- vector<double>& bvec);
- void GaussianSmooth(
- const vector<double>& inputImg,
- const int& width,
- const int& height,
- const vector<double>& kernel,
- vector<double>& smoothImg);
- //==============================================================================
- /// Normalize
- //==============================================================================
- void Normalize(
- const vector<double>& input,
- const int& width,
- const int& height,
- vector<double>& output,
- const int& normrange = 255)
- {
- double maxval(0);
- double minval(DBL_MAX);
- {int i(0);
- for( int y = 0; y < height; y++ )
- {
- for( int x = 0; x < width; x++ )
- {
- if( maxval < input[i] ) maxval = input[i];
- if( minval > input[i] ) minval = input[i];
- i++;
- }
- }}
- double range = maxval-minval;
- if( 0 == range ) range = 1;
- int i(0);
- output.clear();
- output.resize(width*height);
- for( int y = 0; y < height; y++ )
- {
- for( int x = 0; x < width; x++ )
- {
- output[i] = ((normrange*(input[i]-minval))/range);
- i++;
- }
- }
- }
- };
- #endif // !defined(_SALIENCY_H_INCLUDED_)
cpp:
- // Saliency.cpp: implementation of the Saliency class.
- //
- //
- //===========================================================================
- // Copyright (c) 2009 Radhakrishna Achanta [EPFL]
- //===========================================================================
- #include "Saliency.h"
- #include <cmath>
- //
- // Construction/Destruction
- //
- Saliency::Saliency()
- {
- }
- Saliency::~Saliency()
- {
- }
- //===========================================================================
- /// RGB2LAB
- //===========================================================================
- void Saliency::RGB2LAB(
- const vector<unsigned int>& ubuff,
- vector<double>& lvec,
- vector<double>& avec,
- vector<double>& bvec)
- {
- int sz = int(ubuff.size());
- lvec.resize(sz);
- avec.resize(sz);
- bvec.resize(sz);
- for( int j = 0; j < sz; j++ )
- {
- int r = (ubuff[j] >> 16) & 0xFF;
- int g = (ubuff[j] >> 8) & 0xFF;
- int b = (ubuff[j] ) & 0xFF;
- double xval = 0.412453 * r + 0.357580 * g + 0.180423 * b;
- double yval = 0.212671 * r + 0.715160 * g + 0.072169 * b;
- double zVal = 0.019334 * r + 0.119193 * g + 0.950227 * b;
- xval /= (255.0 * 0.950456);
- yval /= 255.0;
- zVal /= (255.0 * 1.088754);
- double fX, fY, fZ;
- double lval, aval, bval;
- if (yval > 0.008856)
- {
- fY = pow(yval, 1.0 / 3.0);
- lval = 116.0 * fY - 16.0;
- }
- else
- {
- fY = 7.787 * yval + 16.0 / 116.0;
- lval = 903.3 * yval;
- }
- if (xval > 0.008856)
- fX = pow(xval, 1.0 / 3.0);
- else
- fX = 7.787 * xval + 16.0 / 116.0;
- if (zVal > 0.008856)
- fZ = pow(zVal, 1.0 / 3.0);
- else
- fZ = 7.787 * zVal + 16.0 / 116.0;
- aval = 500.0 * (fX - fY)+128.0;
- bval = 200.0 * (fY - fZ)+128.0;
- lvec[j] = lval;
- avec[j] = aval;
- bvec[j] = bval;
- }
- }
- //==============================================================================
- /// GaussianSmooth
- ///
- /// Blur an image with a separable binomial kernel passed in.
- //==============================================================================
- void Saliency::GaussianSmooth(
- const vector<double>& inputImg,
- const int& width,
- const int& height,
- const vector<double>& kernel,
- vector<double>& smoothImg)
- {
- int center = int(kernel.size())/2;
- int sz = width*height;
- smoothImg.clear();
- smoothImg.resize(sz);
- vector<double> tempim(sz);
- int rows = height;
- int cols = width;
- //--------------------------------------------------------------------------
- // Blur in the x direction.
- //---------------------------------------------------------------------------
- {int index(0);
- for( int r = 0; r < rows; r++ )
- {
- for( int c = 0; c < cols; c++ )
- {
- double kernelsum(0);
- double sum(0);
- for( int cc = (-center); cc <= center; cc++ )
- {
- if(((c+cc) >= 0) && ((c+cc) < cols))
- {
- sum += inputImg[r*cols+(c+cc)] * kernel[center+cc];
- kernelsum += kernel[center+cc];
- }
- }
- tempim[index] = sum/kernelsum;
- index++;
- }
- }}
- //--------------------------------------------------------------------------
- // Blur in the y direction.
- //---------------------------------------------------------------------------
- {int index = 0;
- for( int r = 0; r < rows; r++ )
- {
- for( int c = 0; c < cols; c++ )
- {
- double kernelsum(0);
- double sum(0);
- for( int rr = (-center); rr <= center; rr++ )
- {
- if(((r+rr) >= 0) && ((r+rr) < rows))
- {
- sum += tempim[(r+rr)*cols+c] * kernel[center+rr];
- kernelsum += kernel[center+rr];
- }
- }
- smoothImg[index] = sum/kernelsum;
- index++;
- }
- }}
- }
- //===========================================================================
- /// GetSaliencyMap
- ///
- /// Outputs a saliency map with a value assigned per pixel. The values are
- /// normalized in the interval [0,255] if normflag is set true (default value).
- //===========================================================================
- void Saliency::GetSaliencyMap(
- const vector<unsigned int>& inputimg,
- const int& width,
- const int& height,
- vector<double>& salmap,
- const bool& normflag)
- {
- int sz = width*height;
- salmap.clear();
- salmap.resize(sz);
- vector<double> lvec(0), avec(0), bvec(0);
- RGB2LAB(inputimg, lvec, avec, bvec);
- //--------------------------
- // Obtain Lab average values
- //--------------------------
- double avgl(0), avga(0), avgb(0);
- {for( int i = 0; i < sz; i++ )
- {
- avgl += lvec[i];
- avga += avec[i];
- avgb += bvec[i];
- }}
- avgl /= sz;
- avga /= sz;
- avgb /= sz;
- vector<double> slvec(0), savec(0), sbvec(0);
- //----------------------------------------------------
- // The kernel can be [1 2 1] or [1 4 6 4 1] as needed.
- // The code below show usage of [1 2 1] kernel.
- //----------------------------------------------------
- vector<double> kernel(0);
- kernel.push_back(1.0);
- kernel.push_back(2.0);
- kernel.push_back(1.0);
- GaussianSmooth(lvec, width, height, kernel, slvec);
- GaussianSmooth(avec, width, height, kernel, savec);
- GaussianSmooth(bvec, width, height, kernel, sbvec);
- {for( int i = 0; i < sz; i++ )
- {
- salmap[i] = (slvec[i]-avgl)*(slvec[i]-avgl) +
- (savec[i]-avga)*(savec[i]-avga) +
- (sbvec[i]-avgb)*(sbvec[i]-avgb);
- }}
- if( true == normflag )
- {
- vector<double> normalized(0);
- Normalize(salmap, width, height, normalized);
- swap(salmap, normalized);
- }
- }
关于代码的使用说明:
- 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.
- Sample usage:
- #include "Saliency.h"
- void main()
- {
- // Assume we already have an unsigned integer buffer inputImg of
- // inputWidth and inputHeight (in row-major order).
- // Each unsigned integer has 32 bits and contains pixel data in ARGB
- // format. I.e. From left to right, the first 8 bits contain alpha
- // channel value and are not used in our case. The next 8 bits
- // contain R channel value; the next 8 bits contain G channel value;
- // the last 8 bits contain the B channel value.
- //
- // Now create a Saliency object and call the GetSaliencyMap function on it.
- Saliency sal;
- vector<double> salmap(0);
- sal.GetSaliencyMap(inputImg, inputWidth, inputHeight, salmap);
- // salmap is a floating point output (in row major order)
- }
我自己写的测试主程序:
可以指定一个文件夹,程序保存该文件夹下所有jpg文件的处理结果
- #include "Saliency.h"
- #include <cv.h>
- #include <cxcore.h>
- #include <highgui.h>
- #include "windows.h"
- #include <iostream>
- #include <cassert>
- using namespace std;
- int main(int argc,char** argv)
- {
- WIN32_FIND_DATAA FileData;
- HANDLE hFind;
- hFind = FindFirstFileA((LPCSTR)"Imgs/*.jpg",&FileData);
- if (hFind == INVALID_HANDLE_VALUE) {
- printf ("Invalid File Handle. GetLastError reports %d/n",
- GetLastError ());
- return (0);
- }
- Saliency sal;
- vector<double> salmap(0);
- while (FindNextFileA(hFind, &FileData)) {
- cout<<FileData.cFileName<<endl;
- string name("Imgs/");
- name.append(FileData.cFileName);
- IplImage* img=cvLoadImage(name.c_str());
- if (!img) {
- cout<<"failed to load image"<<endl;
- break;
- }
- assert(img->nChannels==3);
- vector<unsigned int >imgInput;
- vector<double> imgSal;
- //IplImage to vector
- for (int h=0;h<img->height;h++) {
- unsigned char*p=(unsigned char*)img->imageData+h*img->widthStep;
- for (int w=0;w<img->width;w++) {
- unsigned int t=0;
- t+=*p++;
- t<<=8;
- t+=*p++;
- t<<=8;
- t+=*p++;
- imgInput.push_back(t);
- }
- }
- sal.GetSaliencyMap(imgInput, img->width, img->height, imgSal);
- //vector to IplImage
- int index=0;
- IplImage* imgout=cvCreateImage(cvGetSize(img),IPL_DEPTH_64F ,1);
- for (int h=0;h<imgout->height;h++) {
- double*p=(double*)(imgout->imageData+h*imgout->widthStep);
- for (int w=0;w<imgout->width;w++) {
- *p++=imgSal[index++];
- }
- }
- name.append(".saliency.jpg");
- cvSaveImage(name.c_str(),imgout);
- cvReleaseImage(&img);
- cvReleaseImage(&imgout);
- }
- FindClose(&hFind);
- return 0;
- }
该代码的主页:http://ivrg.epfl.ch/supplementary_material/RK_ICIP2010/index.html
清华的最新研究:http://cg.cs.tsinghua.edu.cn/people/~cmm/saliency/
图像显著区域检测代码及其效果图 saliency region detection相关推荐
- 基于马尔科夫吸收概率的显着区域检测_MAP(Saliency Region Detection Based on Markov Absorption Probabilities)
详见卢湖川主页 在本文中,我们通过利用显着性检测与马尔科夫吸收概率之间的关系,提出一种新颖的自下而上的突出物体检测方法. 首先,我们通过把加权图上部分图像边界作为背景(仅使用左侧和顶侧作为背景),通过 ...
- python图像清晰度_图像的模糊检测方法
关键字:blur detection Function:图像的清晰度检测英文表达为 image blue detection; 以此为关键字可以找到很多有关清晰度检测的demo和算法. 图像的清晰度检 ...
- 显著性图matlab,cvpr14_saliency_code 2014上的关于图像显著性区域的检测matlab代码。 271万源代码下载- www.pudn.com...
文件名称: cvpr14_saliency_code下载 收藏√ [ 5 4 3 2 1 ] 开发工具: matlab 文件大小: 4413 KB 上传时间: 2014-09-07 下载次 ...
- 图像中的天空区域检测!
一.引言 天空区域作为图像中的背景信息,为机器人导航.自动驾驶等领域的图像理解提供了重要依据,因此如何检测图像中的天空区域非常重要,本文提供了一个基于传统视觉算法(非机器学习方法)的提取天空区域的方法 ...
- 车牌识别算法实现及其代码实现之一:车牌区域检测
本文地址:http://blog.csdn.net/shanglianlm/article/details/78005815 本文主要处理汽车车牌的识别过程,包括三个步骤,一:车牌区域检测,本文利用车 ...
- OpenCV与图像处理学习八——图像边缘提取(Canny检测代码)
OpenCV与图像处理学习八--图像边缘提取(Canny检测代码) 一.图像梯度 1.1 梯度 1.2 图像梯度 二.梯度图与梯度算子 2.1模板卷积 2.2 梯度图 2.3 梯度算子 2.3.1 R ...
- android代码查找图像,Android平台上利用opencv进行图像的边沿检测
原标题:Android平台上利用opencv进行图像的边沿检测 近开始接触opencv for Android,从网上down了图像的边沿检测的代码. 测试图片: 在Android2.3.1模拟器上跑 ...
- 视觉显著性 matlab,转载图像/视觉显著性检测技术发展情况梳理(Saliency Detection、Visual Attention)...
图像/视觉显著性检测技术发展情况梳理(Saliency Detection.Visual Attention) Sason@CSDN 转载:http://blog.csdn.net/anshan198 ...
- 深度学习实战 | 智慧工地安全帽和危险区域检测系统(代码已开源!)
文章目录 原创声明 前言 Smart_Construction 指标 yolov5s 为基础训练,`epoch = 50` yolov5m 为基础训练,`epoch = 100` yolov5l 为基 ...
最新文章
- 分享jQuery对象和Javascript对象之间的转换代码
- 超强实时人像抠图算法开源,随心所欲背景替换!
- android python 纠正图片,Python脚本替换Android资源(包名,图片,文件内容)
- SAP Spartacus 自定义 Component 的使用 - SimpleResponsiveBannerComponent
- CoreCLR源码探索(五) GC内存收集器的内部实现 调试篇
- [深度学习-实践]Transformer模型训练IMDB-tensorflow2 keras
- 7-12 藏头诗 (15 分)
- [bug解决] TensorFlow安装错误:ERROR Cannot uninstall ‘wrapt‘
- css3 弹性盒模型 变化
- 清华姚班、斯坦福博士、普林斯顿NLP组创始人 陈丹琦 获小诺奖之称的斯隆奖!...
- python 数据挖掘 简书_Python数据挖掘与分析----Pandas常用知识
- 2021-09-10二叉树的层序遍历
- 员工效率低下,责任在管理层的数学解释和分析
- 编程为什么有趣?浅谈编程的快乐。
- 高效能人士的7个习惯
- Android中的Drawable(一)
- Ubuntu 16.04 解决WPS for Linux提示“系统缺失字体symbol、wingdings、wingdings 2、wingdings 3、webding”的问题
- Windows定时关机小程序
- 抖音音乐怎么下载 mp3格式转换器如何使用
- Pipeline(流水线)模式
热门文章
- 多项式乘法c语言,急!!!!c语言:求n次多项式的加法和乘法
- C中printf函数的实现原理
- OVS bridge和port(三十三)
- linux恢复出厂设置_怎么恢复tp-link路由器出厂设置 恢复tp-link出厂设置方法【详解】...
- python词频统计_python统计词频的三种方法
- c语言做小学生测验程序,[转载]程序设计方法学课程设计--小学生算术四则运算测试程序(C)...
- java 定义类变量初始化吗_Java的变量有哪些类型?变量如何定义?如何初始化?请说明理由并举例_学小易找答案...
- GDB attach 用法
- sort,uniq,wc,history命令简介
- C# HasRows 和 Read的区别