图像处理 VC++ 自适应阈值分割-积分图

叁歲伎倆 2022-05-31 00:42 398阅读 0赞

双窗otsu,通两个不同尺寸的窗口实现阈值分割,然后把两个不同窗口的二值图像合并。

第一列为原图,otsu结果。第二列,3.bmp表示以3x3的窗口的内的Otsu自适应阈值分割效果图,其他图片以此类推。

随着窗口越大越接近原图的otsu结果。

不过局部自适应的阈值的缺点是算法时间花费比较大,而通过积分图可以减少阈值化的时间。

SouthEast

自适应阈值分割,主要解决全局阈值不适用光照不均图像的阈值分割,并可以在线性时间完成计算积分图像I(x,y),原图为f(x,y),过程为:
SouthEast 1

积分图计算完后,只需要要知道一个矩形窗的左上角和右下角的坐标,就可以借用积分图快速实现对矩形窗内的像素的累加,

,完成自适应阈值化:

SouthEast 2

  1. //积分图像实现自适应阈值分割
  2. void CWK_Image_ProcessingView::On_Integral_Image_Adaptive_Threshold()
  3. {
  4. CDC* pDC = GetDC();
  5. int i, j,t=15;
  6. int x1, x2, y1, y2, s = 31;
  7. double sum = 0, temp1 = 0, temp2 = 0;
  8. vector<vector<int> > integral(intHeight, vector<int>(intWidth, 0));//点的结构图矩阵,积分图
  9. vector<vector<int> > out(intHeight, vector<int>(intWidth, 0));//输出
  10. for (i = 0; i < intHeight; i++)
  11. {
  12. sum = 0;
  13. for (j = 0; j <intWidth; j++)
  14. {
  15. sum += Data[i][j];//Data为原图
  16. if (i == 0)
  17. integral[i][j] = sum;
  18. else
  19. integral[i][j] = integral[i-1][j] + sum;
  20. }
  21. }
  22. for (i = 0; i < intHeight; i++)
  23. {
  24. for (j = 0; j <intWidth; j++)
  25. {
  26. x1 = i - s / 2;
  27. x2 = i + s / 2;
  28. y1 = j - s / 2;
  29. y2 = j + s / 2;
  30. if (x1 < 0) x1 = 0;//边界判断
  31. if (y1 < 0) y1 = 0;//边界判断
  32. if (x2 >= intHeight) x2 = intHeight - 1;//边界判断
  33. if (y2 >= intWidth) y2 = intWidth - 1;//边界判断
  34. int count = (x2 - x1)*(y2-y1);
  35. if (y2 < 1) y2 = 1;//边界判断
  36. if (x1 < 1) x1 = 1;//边界判断
  37. if (y1 < 1) y1 = 1;//边界判断
  38. sum = integral[x2][y2] - integral[x2][y1-1] - integral[x1-1][y2] + integral[x1-1][y1-1];
  39. temp1 = Data[i][j] * count;
  40. temp2 = (sum*(100 - t) / 100);
  41. if (temp1<=temp2)
  42. {
  43. pDC->SetPixel(j + intWidth, i, RGB(0, 0, 0));
  44. out[i][j] = 0;
  45. }
  46. else
  47. {
  48. pDC->SetPixel(j + intWidth, i, RGB(255, 255, 255));
  49. out[i][j] = 255;
  50. }
  51. }
  52. }
  53. ReleaseDC(pDC);
  54. }

SouthEast 3

Bradley D,Roth G. Adaptive Thresholding using the Integral Image[J]. Journal of Graphics Gpu & Game Tools, 2007, 12(2):13-21.

发表评论

表情:
评论列表 (有 0 条评论,398人围观)

还没有评论,来说两句吧...

相关阅读