void integral_blur()
{
Mat src = imread("src960_720.bmp", 1);
int height = src.rows;
int width = src.cols;
int Rmax = 5;
int sh2 = Rmax / 2;
int sw2 = Rmax / 2;
int Rsize = Rmax;
int RsizeArea = 1.0 / (Rsize * Rsize) * 2048;
cout << RsizeArea << endl;
Mat srcborder;
MakeBorder(src, srcborder, sw2, sh2);
int ybegin = sh2;
//int yend = height - sh2;
int xbegin = sw2;
//int xend = width - sw2;
int nchannel = src.channels();
Mat blurimg(height, width, CV_8UC3);
for (int i = 0; i < height; ++i)
{
uchar *blurimgRow = blurimg.ptr<uchar>(i);
for (int j = 0; j < width; ++j)
{
uchar *blurimgCol = blurimgRow + j * nchannel;
int nCenter = Rsize >> 1;
int sumR = 0;
int sumG = 0;
int sumB = 0;
int jxbegin = j + xbegin - nCenter;
int iybegin = i + ybegin - nCenter;
for (int y = 0; y < Rsize; ++y)
{
uchar *srcRow = srcborder.ptr<uchar>(iybegin + y);
for (int x = 0; x < Rsize; ++x)
{
uchar *srcCol = srcRow + (jxbegin + x) *nchannel;
sumR += srcCol[2];
sumG += srcCol[1];
sumB += srcCol[0];
}
}
blurimgCol[0] = (sumB * RsizeArea) >> 11;
blurimgCol[1] = (sumG * RsizeArea) >> 11;
blurimgCol[2] = (sumR * RsizeArea) >> 11;
}
}
Mat srcborder_integral;
integral(srcborder, srcborder_integral, CV_32S);
Mat blurimg_integral(height, width, CV_8UC3);
for (int i = 0; i < height; ++i)
{
uchar *blurimgRow = blurimg_integral.ptr<uchar>(i);
for (int j = 0; j < width; ++j)
{
uchar *blurimgCol = blurimgRow + j * nchannel;
int nCenter = Rsize >> 1;
int jxbegin = j + xbegin - nCenter;
int iybegin = i + ybegin - nCenter;
int jxend = jxbegin + Rsize;
int iyend = iybegin + Rsize;
int *p1 = srcborder_integral.ptr<int>(iybegin) +(jxbegin)*nchannel;
int *p2 = srcborder_integral.ptr<int>(iybegin) +(jxend)*nchannel;
int *p3 = srcborder_integral.ptr<int>(iyend) +(jxbegin)*nchannel;
int *p4 = srcborder_integral.ptr<int>(iyend) +(jxend)*nchannel;
int sumR = p1[2] + p4[2] - p2[2] - p3[2];
int sumG = p1[1] + p4[1] - p2[1] - p3[1];
int sumB = p1[0] + p4[0] - p2[0] - p3[0];
blurimgCol[0] = (sumB * RsizeArea) >> 11;
blurimgCol[1] = (sumG * RsizeArea) >> 11;
blurimgCol[2] = (sumR * RsizeArea) >> 11;
}
}
Mat blurimg_opencv;
blur(src, blurimg_opencv, Size(Rmax, Rmax));
namedWindow("src", 0);
imshow("src", src);
namedWindow("dst0", 0);
imshow("dst0", blurimg);
namedWindow("dst1", 0);
imshow("dst1", blurimg_opencv);
namedWindow("dst2", 0);
imshow("dst2", blurimg_integral);
waitKey(0);
}