OpenCV 实现图片的水平投影与垂直投影,并进行行分割

前言:对于印刷体图片来说,进行水平投影和垂直投影可以很快的进行分割,本文在OpenCV中如何进行水平投影和垂直投影通过代码进行说明。

水平投影:二维图像在y轴上的投影

垂直投影:二维图像在x轴上的投影

由于投影的图像需要进行二值化,本文采用积分二值化的方式,对图片进行处理。


具体代码如下:

  1 //积分二值化
  2 void thresholdIntegral (Mat inputMat, Mat& outputMat)
  3 {
  4 
  5     int nRows = inputMat.rows;
  6     int nCols = inputMat.cols;
  7 
  8     // create the integral image
  9     Mat sumMat;
 10     integral (inputMat, sumMat);
 11 
 12     int S = MAX (nRows, nCols) / 8;
 13     double T = 0.15;
 14 
 15     // perform thresholding
 16     int s2 = S / 2;
 17     int x1, y1, x2, y2, count, sum;
 18 
 19     int* p_y1, *p_y2;
 20     uchar* p_inputMat, *p_outputMat;
 21 
 22     for (int i = 0; i < nRows; ++i)
 23     {
 24         y1 = i - s2;
 25         y2 = i + s2;
 26 
 27         if (y1 < 0)
 28         {
 29             y1 = 0;
 30         }
 31         if (y2 >= nRows)
 32         {
 33             y2 = nRows - 1;
 34         }
 35 
 36         p_y1 = sumMat.ptr<int> (y1);
 37         p_y2 = sumMat.ptr<int> (y2);
 38         p_inputMat = inputMat.ptr<uchar> (i);
 39         p_outputMat = outputMat.ptr<uchar> (i);
 40 
 41         for (int j = 0; j < nCols; ++j)
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值