图像处理之滤波---滤波在游戏中的应用boxfilter
http://www.yxkfw.com/?p=7810 很有意思的全方位滤波应用
https://developer.nvidia.com/sites/default/files/akamai/gameworks/CN/CGDC14/OpenGL_4.x_for_Mobile_Games_CN.pdf 游戏开发
http://tech.it168.com/a2010/0722/1081/000001081111_all.shtml 医学应用
http://www.ladeng6666.com/blog/2012/11/02/filterdata-to-separate-the-box2d-collision/ 2d碰撞
http://www.pudn.com/downloads317/sourcecode/graph/detail1404298.html 光线跟踪
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "cvaux.h"
#include "math.h"
#ifdef _DEBUG
#pragma comment(lib,"cv200d.lib")
#pragma comment(lib,"cvaux200d.lib")
#pragma comment(lib,"cxcore200d.lib")
#pragma comment(lib,"highgui200d.lib")
#else
#pragma comment(lib,"cv200.lib")
#pragma comment(lib,"cvaux200.lib")
#pragma comment(lib,"cxcore200.lib")
#pragma comment(lib,"highgui200.lib")
#endif
CvMat * cumsum(CvMat *src,int rc)
{
CvMat *Imdst = cvCloneMat(src);
if (rc==1)
{
for(int row=1;row<src->rows;row++)
{
for(int col=0;col<src->cols;col++)
{
cvSetReal2D(Imdst,row,col,cvGetReal2D(Imdst,row-1,col)+cvGetReal2D(Imdst,row,col));
}
}
}
if (rc==2)
{
for(int row=0;row<src->rows;row++)
{
for(int col=1;col<src->cols;col++)
{
cvSetReal2D(Imdst,row,col,cvGetReal2D(Imdst,row,col-1)+cvGetReal2D(Imdst,row,col));
}
}
}
return Imdst;
}
CvMat * boxFilter(CvMat *src,int r)
{
CvMat *Imdst = cvCloneMat(src);
//imCum = cumsum(imSrc, 1);
CvMat *imCum = cumsum(Imdst,1);
//imDst(1:r+1, :) = imCum(1+r:2*r+1, :);
CvMat *subMat = cvCreateMat(r+1,Imdst->cols,CV_32FC1);
cvGetRows(imCum,subMat,r,2*r+1);//前闭后开的区间
for (int row = 0;row<r+1;row++)
{
for(int col = 0;col<Imdst->cols;col++)
{
cvSetReal2D(Imdst,row,col,cvGetReal2D(subMat,row,col));
}
}
cvReleaseMat(&subMat);
//imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :);
subMat = cvCreateMat(Imdst->rows-2*r,Imdst->cols,CV_32FC1);
cvGetRows(imCum,subMat,2*r+1,Imdst->rows);//这里是不对的第rows行没有被提取
CvMat *subMat2 = cvCreateMat(Imdst->rows-2*r,Imdst->cols,CV_32FC1);
cvGetRows(imCum,subMat2,0,Imdst->rows-2*r-1);
cvSub(subMat,subMat2,subMat2);
for (int row = r+1;row<Imdst->rows-r;row++)
{
for(int col = 0;col<Imdst->cols;col++)
{
cvSetReal2D(Imdst,row,col,cvGetReal2D(subMat2,row-r-1,col));
}
}
cvReleaseMat(&subMat);
cvReleaseMat(&subMat2);
//imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :);
subMat = cvCreateMat(r,Imdst->cols,CV_32FC1);
cvGetRows(imCum,subMat,r,2*r);
CvMat *subMatOne = cvCreateMat(1,Imdst->cols,CV_32FC1);
cvRepeat(cvGetRow(imCum,subMatOne,Imdst->rows-1),subMat);
subMat2 = cvCreateMat(r+1,Imdst->cols,CV_32FC1);
cvGetRows(imCum,subMat2,Imdst->rows-2*r-1,Imdst->rows-r-1);
cvSub(subMat,subMat2,subMat2);
for (int row = Imdst->rows-r;row<Imdst->rows;row++)
{
for(int col = 0;col<Imdst->cols;col++)
{
cvSetReal2D(Imdst,row,col,cvGetReal2D(subMat2,row+r-Imdst->rows,col));
}
}
cvReleaseMat(&subMat);
cvReleaseMat(&subMat2);
CvMat *Imdst2= cvCloneMat(Imdst);
//imCum = cumsum(imDst, 2);
imCum = cumsum(Imdst2,2);
//imDst(:, 1:r+1) = imCum(:, 1+r:2*r+1);
subMat = cvCreateMat(Imdst2->rows,r+1,CV_32FC1);
cvGetCols(imCum,subMat,r,2*r+1);
for(int row=0;row<Imdst2->rows;row++)
{
for(int col=0;col<r+1;col++)
{
cvSetReal2D(Imdst2,row,col,cvGetReal2D(subMat,row,col));
}
}
cvReleaseMat(&subMat);
//imDst(:, r+2:wid-r) = imCum(:, 2*r+2:wid) - imCum(:, 1:wid-2*r-1);
subMat = cvCreateMat(Imdst2->rows,Imdst2->cols-2*r-1,CV_32FC1);
subMat2 = cvCreateMat(Imdst2->rows,Imdst2->cols-2*r-1,CV_32FC1);
cvGetCols(imCum,subMat,2*r+1,imCum->cols);
cvGetCols(imCum,subMat2,0,imCum->cols-2*r-1);
cvSub(subMat,subMat2,subMat2);
for(int row=0;row<Imdst2->rows;row++)
{
for(int col=r+1;col<Imdst->cols-r;col++)
{
cvSetReal2D(Imdst2,row,col,cvGetReal2D(subMat2,row,col-r-1));
}
}
cvReleaseMat(&subMat);
cvReleaseMat(&subMat2);
//imDst(:, wid-r+1:wid) = repmat(imCum(:, wid), [1, r]) - imCum(:, wid-2*r:wid-r-1);
subMat = cvCreateMat(Imdst2->rows,r,CV_32FC1);
cvGetCols(imCum,subMat,r,2*r);
subMatOne = cvCreateMat(Imdst2->rows,1,CV_32FC1);
cvRepeat(cvGetCol(imCum,subMatOne,Imdst->cols-1),subMat);
subMat2 = cvCreateMat(Imdst2->rows,r,CV_32FC1);
cvGetCols(imCum,subMat2,imCum->cols-2*r-1,imCum->cols-r-1);
cvSub(subMat,subMat2,subMat2);
for(int row=0;row<Imdst2->rows;row++)
{
for(int col=Imdst2->cols-r;col<Imdst->cols;col++)
{
cvSetReal2D(Imdst2,row,col,cvGetReal2D(subMat2,row,col+r-Imdst2->cols));
}
}
cvReleaseMat(&subMat);
cvReleaseMat(&subMat2);
cvReleaseMat(&subMatOne);
return Imdst2;
}
CV_IMPL void
cvSplitssss( const CvMat * srcarr, CvMat* dstarr0, CvMat* dstarr1, CvMat* dstarr2, CvMat* dstarr3 )
{
for(int y=0;y<srcarr->rows;y++)
{
for(int x=0;x<srcarr->cols;x++)
{
cvSetReal2D(dstarr0,y,x,cvGet2D(srcarr,y,x).val[0]/255.00);
if(dstarr1!=NULL&&dstarr2!=NULL)
{
cvSetReal2D(dstarr1,y,x,cvGet2D(srcarr,y,x).val[1]/255.00);
cvSetReal2D(dstarr2,y,x,cvGet2D(srcarr,y,x).val[2]/255.00);
}
}
}
}
CvMat * GuidedFilter_Color(CvMat * I,CvMat *pp,int r, float eps)
{
int height = pp->rows;
int weight = pp->cols;
CvMat *p = cvCreateMat(height,weight,CV_32FC1);
cvSplitssss(pp,p,NULL,NULL,NULL);
CvMat *ones = cvCreateMat(height,weight,CV_32FC1);
cvSet(ones,cvRealScalar(1));
CvMat * N = boxFilter(ones,r);
CvMat * I_b = cvCreateMat(height,weight,CV_32FC1);
CvMat * I_g = cvCreateMat(height,weight,CV_32FC1);
CvMat * I_r = cvCreateMat(height,weight,CV_32FC1);
cvZero(I_r);
cvSplitssss(I,I_r,I_g,I_b,NULL);
CvMat * mean_I_r = cvCreateMat(height,weight,CV_32FC1);
cvDiv(boxFilter(I_r,r),N,mean_I_r);
CvMat * mean_I_g = cvCreateMat(height,weight,CV_32FC1);
cvDiv(boxFilter(I_g,r),N,mean_I_g);
CvMat * mean_I_b = cvCreateMat(height,weight,CV_32FC1);
cvDiv(boxFilter(I_b,r),N,mean_I_b);
CvMat * mean_p = cvCreateMat(height,weight,CV_32FC1);
cvDiv(boxFilter(p,r),N,mean_p);
CvMat * pr = cvCreateMat(height,weight,CV_32FC1);
cvMul(I_r,p,pr);
CvMat * mean_Ip_r = cvCreateMat(height,weight,CV_32FC1);
cvDiv(boxFilter(pr,r),N,mean_Ip_r);
CvMat * pg = cvCreateMat(height,weight,CV_32FC1);
cvMul(I_g,p,pg);
CvMat * mean_Ip_g = cvCreateMat(height,weight,CV_32FC1);
cvDiv(boxFilter(pg,r),N,mean_Ip_g);
CvMat * pb = cvCreateMat(height,weight,CV_32FC1);
cvMul(I_b,p,pb);
CvMat * mean_Ip_b = cvCreateMat(height,weight,CV_32FC1);
cvDiv(boxFilter(pb,r),N,mean_Ip_b);
cvMul(mean_I_r,mean_p,pr);
cvMul(mean_I_g,mean_p,pg);
cvMul(mean_I_b,mean_p,pb);
CvMat * cov_Ip_r = cvCreateMat(height,weight,CV_32FC1);
cvSub(mean_Ip_r,pr,cov_Ip_r);
CvMat * cov_Ip_g = cvCreateMat(height,weight,CV_32FC1);
cvSub(mean_Ip_g,pg,cov_Ip_g);
CvMat * cov_Ip_b = cvCreateMat(height,weight,CV_32FC1);
cvSub(mean_Ip_b,pb,cov_Ip_b);
CvMat * var_I_rr = cvCreateMat(height,weight,CV_32FC1);
cvMul(I_r,I_r,pr);
cvDiv(boxFilter(pr,r),N,var_I_rr);
cvMul(mean_I_r,mean_I_r,pr);
cvSub(var_I_rr,pr,var_I_rr);
CvMat * var_I_rg = cvCreateMat(height,weight,CV_32FC1);
cvMul(I_r,I_g,pr);
cvDiv(boxFilter(pr,r),N,var_I_rg);
cvMul(mean_I_r,mean_I_g,pr);
cvSub(var_I_rg,pr,var_I_rg);
CvMat * var_I_rb = cvCreateMat(height,weight,CV_32FC1);
cvMul(I_r,I_b,pr);
cvDiv(boxFilter(pr,r),N,var_I_rb);
cvMul(mean_I_r,mean_I_b,pr);
cvSub(var_I_rb,pr,var_I_rb);
CvMat * var_I_gg = cvCreateMat(height,weight,CV_32FC1);
cvMul(I_g,I_g,pr);
cvDiv(boxFilter(pr,r),N,var_I_gg);
cvMul(mean_I_g,mean_I_g,pr);
cvSub(var_I_gg,pr,var_I_gg);
CvMat * var_I_gb = cvCreateMat(height,weight,CV_32FC1);
cvMul(I_g,I_b,pr);
cvDiv(boxFilter(pr,r),N,var_I_gb);
cvMul(mean_I_g,mean_I_b,pr);
cvSub(var_I_gb,pr,var_I_gb);
CvMat * var_I_bb = cvCreateMat(height,weight,CV_32FC1);
cvMul(I_b,I_b,pr);
cvDiv(boxFilter(pr,r),N,var_I_bb);
cvMul(mean_I_b,mean_I_b,pr);
cvSub(var_I_bb,pr,var_I_bb);
CvMat * Sigma = cvCreateMat(3,3,CV_32FC1);
CvMat * cov_Ip = cvCreateMat(1,3,CV_32FC1);
CvMat * cov_Ipo = cvCreateMat(1,3,CV_32FC1);
CvMat * SigmaInv = cvCreateMat(3,3,CV_32FC1);
CvMat * a_b = cvCreateMat(height,weight,CV_32FC1);
CvMat * a_g = cvCreateMat(height,weight,CV_32FC1);
CvMat * a_r = cvCreateMat(height,weight,CV_32FC1);
cvZero(SigmaInv);
for(int i=0;i<p->rows;i++)
{
for (int j=0;j<p->cols;j++)
{
cvSetReal2D(Sigma,0,0,cvGetReal2D(var_I_rr,i,j)+2*eps);
cvSetReal2D(Sigma,0,1,cvGetReal2D(var_I_rg,i,j));
cvSetReal2D(Sigma,0,2,cvGetReal2D(var_I_rb,i,j));
cvSetReal2D(Sigma,1,0,cvGetReal2D(var_I_rg,i,j));
cvSetReal2D(Sigma,1,1,cvGetReal2D(var_I_gg,i,j)+2*eps);
cvSetReal2D(Sigma,1,2,cvGetReal2D(var_I_gb,i,j));
cvSetReal2D(Sigma,2,0,cvGetReal2D(var_I_rb,i,j));
cvSetReal2D(Sigma,2,1,cvGetReal2D(var_I_gb,i,j));
cvSetReal2D(Sigma,2,2,cvGetReal2D(var_I_bb,i,j)+2*eps);
cvSetReal2D(cov_Ip,0,0,cvGetReal2D(cov_Ip_r,i,j));
cvSetReal2D(cov_Ip,0,1,cvGetReal2D(cov_Ip_g,i,j));
cvSetReal2D(cov_Ip,0,2,cvGetReal2D(cov_Ip_b,i,j));
cvInvert(Sigma,SigmaInv);
cvMatMulAdd(cov_Ip,SigmaInv,0,cov_Ip);
cvSetReal2D(a_r,i,j,cvGetReal2D(cov_Ip,0,0));
cvSetReal2D(a_g,i,j,cvGetReal2D(cov_Ip,0,1));
cvSetReal2D(a_b,i,j,cvGetReal2D(cov_Ip,0,2));
}
}
cvMul(a_r,mean_I_r,pr);
cvMul(a_g,mean_I_g,pg);
cvMul(a_b,mean_I_b,pb);
cvSub(mean_p,pr,mean_p);
cvSub(mean_p,pg,mean_p);
cvSub(mean_p,pb,mean_p);
cvMul(boxFilter(a_r,r),I_r,I_r);
cvMul(boxFilter(a_g,r),I_g,I_g);
cvMul(boxFilter(a_b,r),I_b,I_b);
cvAdd(I_r,I_g,I_r);
cvAdd(I_r,I_b,I_r);
cvAdd(I_r,boxFilter(mean_p,r),I_r);
cvDiv(I_r,N,I_r);
cvReleaseMat(&a_b);
cvReleaseMat(&a_g);
cvReleaseMat(&a_r);
cvReleaseMat(&SigmaInv);
cvReleaseMat(&cov_Ip);
cvReleaseMat(&Sigma);
cvReleaseMat(&var_I_bb);
cvReleaseMat(&var_I_gb);
cvReleaseMat(&var_I_gg);
cvReleaseMat(&var_I_rb);
cvReleaseMat(&var_I_rg);
cvReleaseMat(&var_I_rr);
cvReleaseMat(&cov_Ip_r);
cvReleaseMat(&cov_Ip_g);
cvReleaseMat(&cov_Ip_b);
cvReleaseMat(&pr);
cvReleaseMat(&pg);
cvReleaseMat(&pb);
cvReleaseMat(&mean_Ip_r);
cvReleaseMat(&mean_Ip_g);
cvReleaseMat(&mean_Ip_b);
cvReleaseMat(&I_g);
cvReleaseMat(&I_b);
cvReleaseMat(&ones);
return I_r;
}
附加比较完整的opecv guidefiltercolor:
http://blog.sina.com.cn/s/blog_98ddf7cb01017m3e.html
图像处理之滤波---滤波在游戏中的应用boxfilter的更多相关文章
- 图像处理之基础---滤波器之高斯低通滤波器3c代码实现yuv,rgb
()高斯理论简介 () ()代码实现 四 使用高斯滤波器进行图像的平滑 ()高斯简介 http://research.microsoft.com/en-us/um/people/kahe/eccv10 ...
- opencv3.2.0图像处理之中值滤波medianBlur API函数
/*中值滤波:medianBlur函数是非线性滤波 函数原型:void medianBlur(inputArray src,OutputArray dst,int ksize) 参数详解: input ...
- OpenCV-跟我学一起学数字图像处理之中值滤波
中值滤波(median filter)在数字图像处理中属于空域平滑滤波的内容(spatial filtering).对消除椒盐噪声具有很好的效果. 数学原理 为了讲述的便捷,我们以灰度图为例.RGB三 ...
- 图像处理之中值滤波介绍及C实现
1 中值滤波概述 中值滤波是基于排序统计理论的一种能有效抑制噪声的非线性信号平滑处理技术,它将每一像素点的灰度值设置为该点某邻域窗口内的所有像素点灰度值的中值. 中值滤波的基本原理是把数字图像或数字序 ...
- 图像处理之均值滤波介绍及C算法实现
1 均值滤波介绍 滤波是滤波是将信号中特定波段频率滤除的操作,是从含有干扰的接收信号中提取有用信号的一种技术. 均值滤波是典型的线性滤波算法,它是指在图像上对目标像素给一个模板,该模板包括了其周围的临 ...
- 原创教程“ActionScript3.0游戏中的图像编程”開始连载啦!
经过近两年的不懈努力,笔者的原创教程"ActionScript3游戏中的图像编程"最终在今日划上了完美的句号!这其中记录着笔者多年来在游戏制作,尤其是其中图像处理方 ...
- 【Unity3d游戏开发】游戏中的贝塞尔曲线以及其在Unity中的实现
RT,马三最近在参与一款足球游戏的开发,其中涉及到足球的各种运动轨迹和路径,比如射门的轨迹,高吊球,香蕉球的轨迹.最早的版本中马三是使用物理引擎加力的方式实现的足球各种运动,后来的版本中使用了根据物理 ...
- Unity游戏中使用贝塞尔曲线
孙广东 2015.8.15 比方在3D rpg游戏中.我们想设置弹道,不同的轨迹类型! 目的:这篇文章的主要目的是要给你关于在游戏怎样使用贝塞尔曲线的基本想法. 贝塞尔曲线是最主要的曲线,一般用在 ...
- 地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了
地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了 四叉树对于区域查询,效率比较高. 原理图
随机推荐
- 【2018.12.17】NOI模拟赛4
题目 WZJ题解 T1 T2 T3 后缀自动机+($parents$ 树)树链剖分 发现有大量子串需要考虑,考虑摁死子串的一端. 首先,这题显然是一道离线题,因为所有的询问都是 $1$ 到 某个数,也 ...
- [bzoj1095][ZJOI2007]Hide 捉迷藏 点分树,动态点分治
[bzoj1095][ZJOI2007]Hide 捉迷藏 2015年4月20日7,8876 Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiaji ...
- 【MFC】MFC中使对话框变成圆角矩形的代码(转)
原文转自 http://blog.csdn.net/cracent/article/details/48274469 BOOL CLoginDlg::OnInitDialog() { CDialog: ...
- R语言实战读书笔记(七)基本统计分析
summary() sapply(x,fun,options):对数据框或矩阵中的每一个向量进行统计 mean sd:标准差 var:方差 min: max: median: length: rang ...
- 第2章 Spring Boot 文档
Spring Boot 文档 本节简要介绍了Spring Boot文档,是整个文档的参考指南. 您可以完整阅读本参考指南,或者如果您不感兴趣的话可以跳过该部分. 1. 关于文档 Spring Boot ...
- CEF General Usage(CEF3预览)
CEF General Usage(CEF3预览) 介绍 CEF全称Chromium Embedded Framework,是一个基于Google Chromium 的开源项目.Google Chro ...
- 第四章——SQLServer2008-2012资源及性能监控(1)专家
http://blog.csdn.net/dba_huangzj/article/details/8614817
- :jQuery实例【DEMO】
前言: 今天2月最后一天,写一篇jQuery的几个实例,算是之前前端知识的应用.写完这篇博客会做一个登陆界面+后台管理(i try...) 一.菜单实例 最开始的界面: 点击菜单三后的界面: 二. ...
- SilverLight:基础控件使用(4)-日期显示和选择类控件
ylbtech-SilverLight-Basic-Control:基础控件使用(4)-日期显示和选择类控件 Calendar,DatePicker 1.A,返回顶部 Calendar控件(日期控件) ...
- 最新最全的 Android 开源项目合集
原文链接:https://github.com/opendigg/awesome-github-android-ui 在 Github 上做了一个很新的 Android 开发相关开源项目汇总,涉及到 ...