OpenCV 骨架细化

void ThinImage(Mat  srcImg,Mat&dstImg)//srcImg原图像,dstImg输出图像
  1 void ThinImage(Mat  srcImg,Mat&dstImg)
  2 {
  3     dstImg = srcImg.clone();
  4     vector<Point> deleteList;
  5     int neighbourhood[9];
  6     int r = dstImg.rows;
  7     int c = dstImg.cols;
  8     bool inOddIterations = true;
  9     while (true)
 10     {
 11         for (int j = 1; j < (r - 1); j++) 
 12         {
 13             uchar* data_last = dstImg.ptr<uchar>(j - 1);
 14             uchar* data = dstImg.ptr<uchar>(j);
 15             uchar* data_next = dstImg.ptr<uchar>(j + 1);
 16             for (int i = 1; i < (c - 1); i++) 
 17             {
 18                 if (data[i] == 255)
 19                 {
 20                     int whitePointCount = 0;
 21                     neighbourhood[0] = 1;
 22                     if (data_last[i] == 255)
 23                         neighbourhood[1] = 1;
 24                     else  
 25                         neighbourhood[1] = 0;
 26                     if (data_last[i + 1] == 255)
 27                         neighbourhood[2] = 1;
 28                     else  
 29                         neighbourhood[2] = 0;
 30                     if (data[i + 1] == 255)
 31                         neighbourhood[3] = 1;
 32                     else 
 33                         neighbourhood[3] = 0;
 34                     if (data_next[i + 1] == 255) 
 35                         neighbourhood[4] = 1;
 36                     else  
 37                         neighbourhood[4] = 0;
 38                     if (data_next[i] == 255) 
 39                         neighbourhood[5] = 1;
 40                     else  
 41                         neighbourhood[5] = 0;
 42                     if (data_next[i - 1] == 255) 
 43                         neighbourhood[6] = 1;
 44                     else  
 45                         neighbourhood[6] = 0;
 46                     if (data[i - 1] == 255) 
 47                         neighbourhood[7] = 1;
 48                     else 
 49                         neighbourhood[7] = 0;
 50                     if (data_last[i - 1] == 255) 
 51                         neighbourhood[8] = 1;
 52                     else 
 53                         neighbourhood[8] = 0;
 54                     for (int k = 1; k < 9; k++) 
 55                     {
 56                         whitePointCount = whitePointCount + neighbourhood[k];
 57                     }
 58                     if ((whitePointCount >= 2) && (whitePointCount <= 6)) 
 59                     {
 60                         int ap = 0;
 61                         if ((neighbourhood[1] == 0) && (neighbourhood[2] == 1)) 
 62                             ap++;
 63                         if ((neighbourhood[2] == 0) && (neighbourhood[3] == 1))
 64                             ap++;
 65                         if ((neighbourhood[3] == 0) && (neighbourhood[4] == 1)) 
 66                             ap++;
 67                         if ((neighbourhood[4] == 0) && (neighbourhood[5] == 1)) 
 68                             ap++;
 69                         if ((neighbourhood[5] == 0) && (neighbourhood[6] == 1)) 
 70                             ap++;
 71                         if ((neighbourhood[6] == 0) && (neighbourhood[7] == 1)) 
 72                             ap++;
 73                         if ((neighbourhood[7] == 0) && (neighbourhood[8] == 1)) 
 74                             ap++;
 75                         if ((neighbourhood[8] == 0) && (neighbourhood[1] == 1)) 
 76                             ap++;
 77                         if (ap == 1)
 78                         {
 79                             if (inOddIterations && (neighbourhood[3] * neighbourhood[5] * neighbourhood[7] == 0)
 80                                 && (neighbourhood[1] * neighbourhood[3] * neighbourhood[5] == 0)) 
 81                             {
 82                                 deleteList.push_back(Point(i, j));
 83                             }
 84                             else if (!inOddIterations && (neighbourhood[1] * neighbourhood[5] * neighbourhood[7] == 0)
 85                                    && (neighbourhood[1] * neighbourhood[3] * neighbourhood[7] == 0)) 
 86                             {
 87                                 deleteList.push_back(Point(i, j));
 88                             }
 89                         }
 90                     }
 91                 }
 92             }
 93         }
 94         if (deleteList.size() == 0)
 95             break;
 96         for (size_t i = 0; i < deleteList.size(); i++)
 97         {
 98             Point tem;
 99             tem = deleteList[i];
100             uchar* data = dstImg.ptr<uchar>(tem.y);
101             data[tem.x] = 0;
102         }
103         deleteList.clear();
104 
105         inOddIterations = !inOddIterations;
106     }
107 }

 

转载于:https://www.cnblogs.com/hsy1941/p/11394318.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值