图像处理之滤波---滤波在游戏中的应用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维数组就够了 四叉树对于区域查询,效率比较高. 原理图
随机推荐
- 通过设置chrome浏览器解决跨域问题,在本地进行开发工作
后端跨域权限无法打开,于是去网上找了下我这边能不能解决 现在的浏览器出于安全策略的限制,都是不允许跨域的,但是开发的时候经常需要一些别的域的接口,特别是一些接口不是自己能控制的时候,往往会造成开发困难 ...
- docker基础——自定义镜像、创建私有仓库、查看 docker 运行状态
一.自定义镜像 1,案例1 要求:请自定义一个 docker 镜像,基于 hub.c.163.com/library/centos,要求创建出来的镜像在生成容器的时候,可以直接使用 ifconfig ...
- 谷歌Chrome 27测试版已经发布 更快的浏览速度
谷歌已经发布了Chrome 27测试版浏览器,为普通用户带来了大约提升为5%的网页浏览速度.用户已经可以在Chrome测试网页下载到最新的更新了.我们已经对这版更新做了一个全面的快速测试,让我们看看究 ...
- 【BZOJ2286】消耗战(虚树,DFS序,树形DP)
题意:一棵N个点的树上有若干个关键点,每条边有一个边权,现在要将这些关键点到1的路径全部切断,切断一条边的代价就是边权. 共有M组询问,每组询问有k[i]个关键点,对于每组询问求出完成任务的最小代价. ...
- python读取Excel实例
1.操作步骤: (1)安装python官方Excel库-->xlrd (2)获取Excel文件位置并读取 (3)读取sheet (4)读取指定rows和cols内容 2.示例代码 # -*- c ...
- [AI开发]深度学习如何选择GPU?
机器推理在深度学习的影响下,准确性越来越高.速度越来越快.深度学习对人工智能行业发展的贡献巨大,这得益于现阶段硬件计算能力的提升.互联网海量训练数据的出现.本篇文章主要介绍深度学习过程中如何选择合适的 ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- 1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- echarts判断点击参数类型,series为有效,markPoint 无效
https://www.w3cschool.cn/echarts_tutorial/echarts_tutorial-7o3u28yh.html 可以设置如果点击的是markPoint,直接返回
- mysql存储过程命令行批量插入N条数据命令
原文:http://blog.csdn.net/tomcat_2014/article/details/53377924 delimiter $$ create procedure myproc () ...