显著性区域检测代码课件_第1页
显著性区域检测代码课件_第2页
显著性区域检测代码课件_第3页
显著性区域检测代码课件_第4页
显著性区域检测代码课件_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/ Saliency.h#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

2、 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<d

3、ouble>& 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 Norma

4、lize( 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

5、i ) maxval = inputi; if( minval > inputi ) minval = inputi; 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+ ) outputi = (normrange*(inputi-minval)/range); i+; ;

6、 #endif / !defined(_SALIENCY_H_INCLUDED_) / Saliency.cpp: implementation of the Saliency class. / / /= / Copyright (c) 2009 Radhakrishna Achanta EPFL /= #include "StdAfx.h" #include "Saliency.h" #include <cmath> / / Construction/Destruction / Saliency:Saliency() Saliency:Sa

7、liency() /= / 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+ ) i

8、nt r = (ubuffj >> 16) & 0xFF; int g = (ubuffj >> 8) & 0xFF; int b = (ubuffj ) & 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 *

9、 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.7

10、87 * 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; lvecj = lval; avecj = aval; bvecj = bval; /= / GaussianSmooth / / Blur an image with a separable binomial kernel passed

11、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); v

12、ector<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+

13、cc) < cols) sum += inputImgr*cols+(c+cc) * kernelcenter+cc; kernelsum += kernelcenter+cc; tempimindex = 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 = (-ce

14、nter); rr <= center; rr+ ) if(r+rr) >= 0) && (r+rr) < rows) sum += tempim(r+rr)*cols+c * kernelcenter+rr; kernelsum += kernelcenter+rr; smoothImgindex = sum/kernelsum; index+; /= / GetSaliencyMap / / Outputs a saliency map with a value assigned per pixel. The values are / normalized

15、 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.resi

16、ze(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 += lveci; avga += aveci; avgb += bveci; avgl /= sz; avga /= sz; avgb /= sz; vector<double> slvec

17、(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,

18、width, height, kernel, savec); GaussianSmooth(bvec, width, height, kernel, sbvec); for( int i = 0; i < sz; i+ ) salmapi = (slveci-avgl)*(slveci-avgl) + (saveci-avga)*(saveci-avga) + (sbveci-avgb)*(sbveci-avgb); if( true = normflag ) vector<double> normalized(0); Normalize(salmap, width, hei

19、ght, normalized); swap(salmap, normalized); / SaliencyMapDlg.cpp : 实º¦Ì现?文?件t/#include "stdafx.h"#include "SaliencyMap.h"#include "SaliencyMapDlg.h"#include "afxdialogex.h"#include "opencv2opencv.hpp" #include "Saliency.h"#if

20、def _DEBUG#define new DEBUG_NEW#endif/ 用®?于®¨²应®|用®?程¨¬序¨°“¡ã关?于®¨²”¡À菜?单Ì£¤项?的Ì? CAboutDlg 对?话¡ã框¨°class CAboutDlg : public CDialogExpublic:CAboutDlg();/ 对?话¡ã框

21、8;°数ºy据Yenum IDD = IDD_ABOUTBOX ;protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV 支¡ì持?/ 实º¦Ì现?protected:DECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialogEx(CAboutDlg:IDD)void CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialogEx:DoDataExcha

22、nge(pDX);BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP()/ CSaliencyMapDlg 对?话¡ã框¨°CSaliencyMapDlg:CSaliencyMapDlg(CWnd* pParent /*=NULL*/): CDialogEx(CSaliencyMapDlg:IDD, pParent)m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);void CSaliencyMapDlg:DoDataExchange(CDataE

23、xchange* pDX)CDialogEx:DoDataExchange(pDX);BEGIN_MESSAGE_MAP(CSaliencyMapDlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUT_OPEN, &CSaliencyMapDlg:OnBnClickedButOpen)END_MESSAGE_MAP()/ CSaliencyMapDlg 消?息¡é处ä|理¤¨ª程¨¬序&

24、#168;°BOOL CSaliencyMapDlg:OnInitDialog()CDialogEx:OnInitDialog();/ 将?“¡ã关?于®¨².”¡À菜?单Ì£¤项?添¬¨ª加¨®到Ì?系¦Ì统ª3菜?单Ì£¤中D。¡ê/ IDM_ABOUTBOX 必À?须?在¨²系¦Ì统ª

25、;3命¨¹令¢?范¤?围¡ì内¨²。¡êASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX)

26、;ASSERT(bNameValid);if (!strAboutMenu.IsEmpty()pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ 设¦¨¨置?此ä?对?话¡ã框¨°的Ì?图ª?标À¨º。¡ê当Ì¡À应®|用®?程¨

27、72;序¨°主¡Â窗ä¡ã口¨²不?是º?对?话¡ã框¨°时º¡À,ê?框¨°架¨¹将?自Á?动¡¥/ 执¡ä行D此ä?操¨´作Á¡ÂSetIcon(m_hIcon, TRUE);/ 设¦¨¨置?大䨮图&#

28、170;?标À¨ºSetIcon(m_hIcon, FALSE);/ 设¦¨¨置?小?图ª?标À¨º/ TODO: 在¨²此ä?添¬¨ª加¨®额?外ªa的Ì?初?始º?化¡¥代䨲码?return TRUE; / 除y非¤?将?焦1点Ì?设¦¨¨置?到Ì?控?件t,ê

29、?否¤?则¨°返¤¦Ì回? TRUEvoid CSaliencyMapDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialogEx:OnSysCommand(nID, lParam);/ 如¨?果?向¨°对?话¡ã框¨°添¬¨ª加¨

30、®最Á?小?化¡¥按ã¡ä钮£¤,ê?则¨°需¨¨要°a下?面?的Ì?代䨲码?/ 来¤¡ä绘?制?该?图ª?标À¨º。¡ê对?于®¨²使º1用®?文?档̦Ì/视º¨®图ª?模¡

31、4;型¨ª的Ì? MFC 应®|用®?程¨¬序¨°,ê?/ 这a将?由®¨¦框¨°架¨¹自Á?动¡¥完ª¨º成¨¦。¡êvoid CSaliencyMapDlg:OnPaint()if (IsIconic()CPaintDC dc(this); / 用®?于®¨²绘?制?的Ì?设

32、¦¨¨备À?上¦?下?文?SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc(), 0);/ 使º1图ª?标À¨º在¨²工¡è作Á¡Â区?矩?形?中D居¨®中Dint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_C

33、YICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;/ 绘?制?图ª?标À¨ºdc.DrawIcon(x, y, m_hIcon);elseCDialogEx:OnPaint();/当Ì¡À用®?户¡ì拖ª?动¡¥最Á?小?化¡¥窗ä

34、¡ã口¨²时º¡À系¦Ì统ª3调Ì¡Â用®?此ä?函¡¥数ºy取¨?得Ì?光a标À¨º/显?示º?。¡êHCURSOR CSaliencyMapDlg:OnQueryDragIcon()return static_cast<HCURSOR>(m_hIcon);/*/src IPL_DEPTH_8U/dst IPL_DEPTH_

35、64F*/傅¦Ì里¤?叶°?正y变À?换?void fft2(IplImage *src, IplImage *dst) /实º¦Ì部?、¡é虚¨¦部? IplImage *image_Re = 0, *image_Im = 0, *Fourier = 0; / int i, j; image_Re = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1); /实º¦Ì部? /Imaginary part

36、 image_Im = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1); /虚¨¦部? /2 channels (image_Re, image_Im) Fourier = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 2); / Real part conversion from u8 to 64f (double) cvConvertScale(src, image_Re, 1, 0); / Imaginary part (zeros) cvZero(image_Im); / J

37、oin real and imaginary parts and stock them in Fourier image cvMerge(image_Re, image_Im, 0, 0, Fourier); / Application of the forward Fourier transform cvDFT(Fourier, dst, CV_DXT_FORWARD); cvReleaseImage(&image_Re); cvReleaseImage(&image_Im); cvReleaseImage(&Fourier);void CSaliencyMapDlg

38、:OnBnClickedButOpen() IplImage *src, *ImageRe, *ImageIm, *Fourier, *Inverse, *LogAmplitude, *Sine, *Cosine; IplImage *Saliency, *Residual; IplImage *tmp1, *tmp2, *tmp3; double minNum = 0, maxNum = 0, scale, shift; int i, j, nRow, nCol; CString Path;CFileDialog dlg(true,_T("*.bmp"),NULL,OFN

39、_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,_T("image file(*.bmp;*.jpg)|*.bmp;*.jpg|All Files(*.*)|*.*|"),NULL);dlg.m_ofn.lpstrTitle=_T("open Image");if(dlg.DoModal()!=IDOK)return ;Path = dlg.GetPathName();Path.Replace(_T("/"),_T("/");src = cvLoadImage(C

40、T2CA) Path,0); Fourier = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 2); Inverse = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 2); ImageRe = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1); ImageIm = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1); LogAmplitude = cvCreateImage(cvGetSize(src), IPL_

41、DEPTH_64F, 1); Sine = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1); Cosine = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1); Residual = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1); Saliency = cvCreateImage(cvGetSize(src), src->depth, src->nChannels); tmp1 = cvCreateImage(cvGetSize(src)

42、, IPL_DEPTH_64F, 1); tmp2 = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1); tmp3 = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1); nRow = src->height; nCol = src->width; /归¨¦一°?化¡¥一°?下? scale = 1.0/255.0; cvConvertScale(src, tmp1, scale, 0); /傅¦Ì里¤

43、;?叶°?变À?换?,ê?得Ì?到Ì?的Ì?Fourier有®D两¢?个?通ª¡§道̨¤,ê?一°?个?是º?实º¦Ì部?,ê?一°?个?是º?虚¨¦部? fft2(tmp1, Fourier); /将?傅¦Ì里¤?叶°?谱¡Á的Ì?实º¦Ì部?和

44、¨ª虚¨¦部?存ä?放¤?到Ì?对?应®|的Ì?图ª?像?中D去¨£¤。¡ê cvSplit(Fourier, ImageRe, ImageIm, 0, 0); /计?算?傅¦Ì里¤?叶°?振?幅¤¨´谱¡Á,ê?实º¦Ì部?和¨ª虚¨¦部?平?方¤?和¨&#

45、170;再¨´开a方¤?,ê?得Ì?到Ì?振?幅¤¨´谱¡Á存ä?到Ì?tmp3中D cvPow( ImageRe, tmp1, 2.0); cvPow( ImageIm, tmp2, 2.0); cvAdd( tmp1, tmp2, tmp3); cvPow( tmp3, tmp3, 0.5 ); /计?算?正y弦¨°谱¡Á和¨ª余®¨¤弦¨°谱¡

46、;Á和¨ª自Á?然¨?对?数ºy谱¡Á cvLog( tmp3, LogAmplitude ); cvDiv(ImageIm, tmp3, Sine); cvDiv(ImageRe, tmp3, Cosine); /对?LogAmplitude做Á?3*3均¨´值¦Ì滤?波¡§ cvSmooth(LogAmplitude, tmp3, CV_BLUR, 3, 3); /计?算?出?剩º¡ê余®¨&

47、#164;普? cvSub(LogAmplitude, tmp3, Residual); /*/ /* 对?exp(Residual+i*Phase)作Á¡Â傅¦Ì里¤?叶°?反¤¡ä变À?换? 由®¨¦欧¡¤拉¤-公?式º?:êo exp(r+i*¡§) = exp(r)*(cos(¡§) + i*sin(¡§) = exp(r)*cos(¡§) + i*exp(r)*sin(¡§) 其?中D¡§是º?相¨¤位?。¡ê 而?sin(¡§) = ImageIm/Amplitude; cos(¡§) = Imag

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论