图像处理之滤波---滤波在游戏中的应用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维数组就够了 四叉树对于区域查询,效率比较高. 原理图
随机推荐
- transform与position:fixed的那些恩怨--摘抄
1. 前言 在写这篇文章之前,我理解的fixed元素是这样的:(摘自CSS布局基础) 固定定位与absolute定位类型类似,但它的相对移动的坐标是视图(屏幕内的网页窗口)本身.由于视图本身是固定的, ...
- 标准C程序设计七---104
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+. 一.错误发生情景: 在安装gcc时,执行.c ...
- python pyd 加密相关
Dockerfile RUN 同时执行多条命令 Dokcerfile中的命令每执行一条即产生一个新的镜像,当前命令总是在最新的镜像上执行.如下Dockerfile: RUN cd /usr/share ...
- AC日记——合唱队形 洛谷 P1901
题目描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2…,K,他们的身高分别为T1,T2, ...
- Xamarin XAML语言教程基础语法篇大学霸
Xamarin XAML语言教程基础语法篇大学霸 前 言 Xamarin是一个跨平台开发框架.它可以用来开发iOS.Android.Windows Phone和Mac的应用程序.使用Xamarin框 ...
- 着陆攻击LAND Attack
着陆攻击LAND Attack 着陆攻击LAND Attack也是一种拒绝服务攻击DOS.LAND是Local Area Network Denial的缩写,意思是局域网拒绝服务攻击,翻译为着陆攻 ...
- mac下安装pyQt4
1.首先安装QT,同时要有gcc 2.然后就是先安装sip,然后安装pyqt4 python configure.py -q /usr/bin/qmake-4.8 -d /Library/Python ...
- 济南day3
连续几天都有点炸 预计的分拿不到,调整好心态,考试的时候多想一下,think twice,code once 唉,什么情况啊 题解链接 0+0+0 T1读错题输出反了 n*m%2判断是否==1 T2 ...
- Java线程池的内部实现
一.线程池介绍 线程是稀缺资源,如果无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,合理的使用线程池可以对线程进行统一的分配.调优和监控,并有以下好处: (1)降低资源消耗. (2)提高响应速 ...