OpenCV 骨架细化

2019/08/22 14:55
阅读数 28
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 }

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部