OpenCV中积分图函数与应用

描述

 

 

OpenCV中积分图函数与应用

一:图像积分图概念

积分图像是Crow在1984年首次提出,是为了在多尺度透视投影中提高渲染速度。随后这种技术被应用到基于NCC的快速匹配、对象检测和SURF变换中、基于统计学的快速滤波器等方面。积分图像是一种在图像中快速计算矩形区域和的方法,这种算法主要优点是一旦积分图像首先被计算出来我们可以计算图像中任意大小矩形区域的和而且是在常量时间内。这样在图像模糊、边缘提取、对象检测的时候极大降低计算量、提高计算速度。第一个应用积分图像技术的应用是在Viola-Jones的对象检测框架中出现。

函数

函数

上图左侧四个点的矩形区域像素求和,只要根据每个点左上方所有像素和表值,进行两次减法与一次加法即可=》46 – 22 – 20 + 10 = 14

二:OpenCV中积分图函数

OpenCV中通过integral()函数可以很容易的计算图像的积分图,该函数支持和表积分图、平方和表积分图、瓦块和表积分图计算。integral函数与参数解释如下:


	
  1. void cv::integral(

  2.    InputArray  src, // 输入图像

  3.    OutputArray  sum, // 和表

  4.    OutputArray  sqsum, // 平方和表

  5.    OutputArray  tilted, // 瓦块和表

  6.    int     sdepth = -1, // 和表数据深度常见CV_32S

  7.    int     sqdepth = -1 // 平方和表数据深度 常见 CV_32F

  8. )

三:使用积分图函数

通过代码演示计算积分图实现任意窗口大小的盒子模糊与垂直边缘提取,完整的代码实现如下:


	
  1. #include

  2. #include

  3.  

  4. using namespace cv;

  5. using namespace std;

  6.  

  7. void blur_demo(Mat &image, Mat &sum);

  8. void edge_demo(Mat &image, Mat &sum);

  9. int getblockSum(Mat &sum, int x1, int y1, int x2, int y2, int i);

  10. int main(int argc, char** argv) {

  11.    Mat src = imread("D:/vcprojects/images/yuan_test.png");

  12.    if (src.empty()) {

  13.        printf("could not load image... ");

  14.        return -1;

  15.    }

  16.    namedWindow("input", CV_WINDOW_AUTOSIZE);

  17.    imshow("input", src);

  18.  

  19.    namedWindow("output", CV_WINDOW_AUTOSIZE);

  20.  

  21.    // 计算积分图

  22.    Mat sum, sqrsum;

  23.    integral(src, sum, sqrsum, CV_32S, CV_32F);

  24.  

  25.    // 积分图应用

  26.    int type = 0;

  27.    while (true) {

  28.        char c = waitKey(100);

  29.        if (c > 0) {

  30.            type = (int)c;

  31.            printf("c : %d ", type);

  32.        }

  33.  

  34.        if (c == 27) {

  35.            break; // ESC

  36.        }

  37.        if (type == 49) { // 数字键 1

  38.            blur_demo(src, sum);

  39.        }

  40.        else if (type == 50) { // 数字键 2

  41.            edge_demo(src, sum);

  42.        }

  43.        else {

  44.            blur_demo(src, sum);

  45.        }

  46.    }

  47.  

  48.    waitKey(0);

  49.    return 0;

  50. }

  51.  

  52. void blur_demo(Mat &image, Mat &sum) {

  53.    int w = image.cols;

  54.    int h = image.rows;

  55.    Mat result = Mat::zeros(image.size(), image.type());

  56.    int x2 = 0, y2 = 0;

  57.    int x1 = 0, y1 = 0;

  58.    int ksize = 5;

  59.    int radius = ksize / 2;

  60.    int ch = image.channels();

  61.    int cx = 0, cy = 0;

  62.    for (int row = 0; row < h + radius; row++) {

  63.        y2 = (row + 1)>h ? h : (row + 1);

  64.        y1 = (row - ksize) < 0 ? 0 : (row - ksize);

  65.        for (int col = 0; col < w + radius; col++) {

  66.            x2 = (col + 1)>w ? w : (col + 1);

  67.            x1 = (col - ksize) < 0 ? 0 : (col - ksize);

  68.            cx = (col - radius) < 0 ? 0 : col - radius;

  69.            cy = (row - radius) < 0 ? 0 : row - radius;

  70.            int num = (x2 - x1)*(y2 - y1);

  71.            for (int i = 0; i < ch; i++) {

  72.                // 积分图查找和表,计算卷积

  73.                int s = getblockSum(sum, x1, y1, x2, y2, i);

  74.                result.at<Vec3b>(cy, cx)[i] = saturate_cast(s / num);

  75.            }

  76.        }

  77.    }

  78.    imshow("output", result);

  79.    imwrite("D:/result.png", result);

  80. }

  81.  

  82. /**

  83. * 3x3 sobel 垂直边缘检测演示

  84. */

  85. void edge_demo(Mat &image, Mat &sum) {

  86.    int w = image.cols;

  87.    int h = image.rows;

  88.    Mat result = Mat::zeros(image.size(), CV_32SC3);

  89.    int x2 = 0, y2 = 0;

  90.    int x1 = 0, y1 = 0;

  91.    int ksize = 3; // 算子大小,可以修改,越大边缘效应越明显

  92.    int radius = ksize / 2;

  93.    int ch = image.channels();

  94.    int cx = 0, cy = 0;

  95.    for (int row = 0; row < h + radius; row++) {

  96.        y2 = (row + 1)>h ? h : (row + 1);

  97.        y1 = (row - ksize) < 0 ? 0 : (row - ksize);

  98.        for (int col = 0; col < w + radius; col++) {

  99.            x2 = (col + 1)>w ? w : (col + 1);

  100.            x1 = (col - ksize) < 0 ? 0 : (col - ksize);

  101.            cx = (col - radius) < 0 ? 0 : col - radius;

  102.            cy = (row - radius) < 0 ? 0 : row - radius;

  103.            int num = (x2 - x1)*(y2 - y1);

  104.            for (int i = 0; i < ch; i++) {

  105.                // 积分图查找和表,计算卷积

  106.                int s1 = getblockSum(sum, x1, y1, cx, y2, i);

  107.                int s2 = getblockSum(sum, cx, y1, x2, y2, i);

  108.                result.at<Vec3i>(cy, cx)[i] = saturate_cast(s2 - s1);

  109.            }

  110.        }

  111.    }

  112.    Mat dst, gray;

  113.    convertScaleAbs(result, dst);

  114.    normalize(dst, dst, 0, 255, NORM_MINMAX);

  115.    cvtColor(dst, gray, COLOR_BGR2GRAY);

  116.    imshow("output", gray);

  117.    imwrite("D:/edge_result.png", gray);

  118. }

  119.  

  120. int getblockSum(Mat &sum, int x1, int y1, int x2, int y2, int i) {

  121.    int tl = sum.at<Vec3i>(y1, x1)[i];

  122.    int tr = sum.at<Vec3i>(y2, x1)[i];

  123.    int bl = sum.at<Vec3i>(y1, x2)[i];

  124.    int br = sum.at<Vec3i>(y2, x2)[i];

  125.    int s = (br - bl - tr + tl);

  126.    return s;

  127. }

这里最重要的是要注意到上面的图示,积分图对象的Mat(1,1)对应实际图像Mat(0,0),如果不加处理的话会导致结果有明显的中心迁移。edge_demo实现了积分图查找提取图像边缘、blur_demo函数实现积分图查找图像均值模糊,getblockSum函数实现和表查找功能,运行显示:

原图:

 

模糊效果

函数

边缘效果

函数

 

审核编辑 :李倩


打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分