#include "myGuidedFilter_Mat.h"
CvMat * cumsum(CvMat *src,intrc)
{
CvMat *Imdst = cvCreateMat(src->rows,src->cols,CV_64FC1);
Imdst=cvCloneMat(src);
if(rc==1)
{
for(inty=1;yheight;y++)
{
double*ptr0=(double*)(Imdst->data.ptr+(y-1)*Imdst->step);
double*ptr=(double*)(Imdst->data.ptr+y*Imdst->step);
for(intx=0;xwidth;x++)
{
ptr[x]=ptr0[x]+ptr[x];
//cvSetReal2D(Imdst,y,x,cvGetReal2D(Imdst,y-1,x)+cvGetReal2D(Imdst,y,x));
}
}
}
elseif(rc==2)
{
for(inty=0;yheight;y++)
{
double*ptr=(double*)(Imdst->data.ptr+y*Imdst->step);
for(intx=1;xwidth;x++)
{
ptr[x]=ptr[x-1]+ptr[x];
//cvSetReal2D(Imdst,y,x,cvGetReal2D(Imdst,y,x-1)+cvGetReal2D(Imdst,y,x));
}
}
}
returnImdst;
}
CvMat * boxFilter(CvMat *src,intr)
{
CvMat *Imdst = cvCreateMat(src->rows,src->cols,CV_64FC1);
Imdst=cvCloneMat(src);
CvMat *subImage;
//imCum = cumsum(imSrc, 1);
CvMat *imCum = cumsum(Imdst,1);
//imDst(1:r+1, :) = imCum(1+r:2*r+1, :);
for(inty = 0;y
{
//double *ptrDst=(double *)Imdst->data.ptr+y*Imdst->step;
//double *ptrCum=(double *)imCum->data.ptr+(y+r)*imCum->step;
for(intx = 0;xwidth;x++)
{
//ptrDst[x]=ptrCum[x];
cvSetReal2D(Imdst,y,x,cvGetReal2D(imCum,y+r,x));
}
}
//imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :);
for(inty = r+1;yheight-r-1;y++)
{
for(intx = 0;xwidth;x++)
{
cvSetReal2D(Imdst,y,x,(cvGetReal2D(imCum,y+r,x)-cvGetReal2D(imCum,y-r-1,x)));
}
}
//imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :);
subImage = cvCreateMat(r,Imdst->width,CV_64FC1);
CvMat *tem=cvCreateMat(1,Imdst->width,CV_64FC1);
cvGetRow(imCum,tem,imCum->height-1);
cvRepeat(tem,subImage);
/*for(int y=0;y
{
for(int x=0;xwidth;x++)
{
cvSetReal2D(subImage,y,x,cvGetReal2D(imCum,Imdst->height-1,x));
}
}*/
for(inty = Imdst->height-r;yheight;y++)
{
for(intx = 0;xwidth;x++)
{
cvSetReal2D(Imdst,y,x,cvGetReal2D(subImage,y-Imdst->height+r,x)-cvGetReal2D(imCum,y-r-1,x));
}
}
cvReleaseMat(&subImage);
cvReleaseMat(&tem);
imCum = cumsum(Imdst, 2);
//imDst(:, 1:r+1) = imCum(:, 1+r:2*r+1);
for(inty = 0;yheight;y++)
{
for(intx = 0;x
{
cvSetReal2D(Imdst,y,x,cvGetReal2D(imCum,y,x+r));
}
}
//imDst(:, r+2:wid-r) = imCum(:, 2*r+2:wid) - imCum(:, 1:wid-2*r-1);
for(inty = 0;yheight;y++)
{
for(intx = r+1;xwidth-r-1;x++)
{
cvSetReal2D(Imdst,y,x,(cvGetReal2D(imCum,y,x+r)-cvGetReal2D(imCum,y,x-r-1)));
}
}
//imDst(:, wid-r+1:wid) = repmat(imCum(:, wid), [1, r]) - imCum(:, wid-2*r:wid-r-1);
subImage = cvCreateMat(Imdst->height,r,CV_64FC1);
tem=cvCreateMat(Imdst->height,1,CV_64FC1);
cvGetCol(imCum,tem,imCum->width-1);
cvRepeat(tem,subImage);
/*for(int y=0;yheight;y++)
{
for(int x=0;x
{
cvSetReal2D(subImage,y,x,cvGetReal2D(imCum,y,Imdst->width-1));
}
}*/
for(inty = 0;yheight;y++)
{
for(intx = Imdst->width-r;xwidth;x++)
{
cvSetReal2D(Imdst,y,x,cvGetReal2D(subImage,y,x-Imdst->width+r)-cvGetReal2D(imCum,y,x-r-1));
}
}
cvReleaseMat(&subImage);
returnImdst;
}
CvMat * myGuidedFilter_Mat(CvMat * I,CvMat *img_pp,intr,doubleeps)
{
intheight = img_pp->height;
intwidth = img_pp->width;
inttype = CV_64FC1;
CvMat *ones = cvCreateMat(height,width,type);
cvSet(ones,cvRealScalar(1));
CvMat * N = boxFilter(ones,r);
//求I的均值
CvMat * mean_I = cvCreateMat(height,width,type);
cvDiv(boxFilter(I,r),N,mean_I);
//求P的均值
CvMat * mean_p = cvCreateMat(height,width,type);
cvDiv(boxFilter(img_pp,r),N,mean_p);
//求I*P的均值
CvMat * pr = cvCreateMat(height,width,type);
cvMul(I,img_pp,pr);
CvMat * mean_Ip = cvCreateMat(height,width,type);
cvDiv(boxFilter(pr,r),N,mean_Ip);
//求I与p协方差
cvMul(mean_I,mean_p,pr);
CvMat * cov_Ip = cvCreateMat(height,width,type);
cvSub(mean_Ip,pr,cov_Ip);
//求I的方差
CvMat * var_I = cvCreateMat(height,width,type);
cvMul(I,I,pr);
cvDiv(boxFilter(pr,r),N,var_I);
cvMul(mean_I,mean_I,pr);
cvSub(var_I,pr,var_I);
//求a
CvMat * a = cvCreateMat(height,width,type);
cvAddS(var_I,cvScalar(eps),var_I);
cvDiv(cov_Ip,var_I,a);
//求b
CvMat * b = cvCreateMat(height,width,type);
cvMul(a,mean_I,pr);
cvSub(mean_p,pr,b);
//求a的均值
CvMat * mean_a = cvCreateMat(height,width,type);
cvDiv(boxFilter(a,r),N,mean_a);
//求b的均值
CvMat * mean_b = cvCreateMat(height,width,type);
cvDiv(boxFilter(b,r),N,mean_b);
//求Q
CvMat * q = cvCreateMat(height,width,type);
cvMul(mean_a,I,a);
cvAdd(a,mean_b,q);
cvReleaseMat(&ones);
cvReleaseMat(&mean_I);
cvReleaseMat(&mean_p);
cvReleaseMat(&pr);
cvReleaseMat(&mean_Ip);
cvReleaseMat(&cov_Ip);
cvReleaseMat(&var_I);
cvReleaseMat(&a);
cvReleaseMat(&b);
cvReleaseMat(&mean_a);
cvReleaseMat(&mean_b);
returnq;
}