当前位置:   article > 正文

基于OpenCV实现二值图细化,骨骼化并求出端点和交叉点_二值图像 求端点

二值图像 求端点

所谓细化就是经过一层层的剥离,从原来的图中去掉一些点,但仍要保持原来的形状,直到得到图像的骨架。骨架,可以理解为物体的中轴,例如一个长方形的骨架是它的长方向上的中轴线;正方形的骨架是它的中心点;圆的骨架是它的圆心,直线的骨架是它自身,孤立点的骨架也是自身。得到了骨架,就相当于突出物体的主要结构和形状信息,去除了多余信息,根据这些信息可以实现图像上特征点的检测,如端点,交叉点和拐点。

下面先介绍经典的Zhang并行快速细化算法:

        设p1点的八邻域为:

p9 p2 p3

  p8 p1 p4

p7 p6 p5

(其中p1为白点也就是物体,如果以下四个条件同时满足,则删除p1,即令p1=0)

其中迭代分为两个子过程:

过程1 细化删除条件为:              
(1)、2 <=N(p1) <= 6,   N(x)为x的8邻域中黑点的数目
(2)、A(p1)=1,A(x)指的是将p2-p8之间按序前后分别成对值为0、1的个数(背景色:0)
(3)、p2*p4*p6=0 
(4)、p4*p6*p8=0
如果同时满足以上四个条件则该点可以删除(赋值为0)。

过程2 细化删除条件为:        
(1)、2 <=N(p1) <= 6,   N(x)为x的8邻域中黑点的数目
(2)、A(p1)=1,A(x)指的是将p2-p8之间按序前后分别为0、1的对数(背景色:0)
(3)、p2*p4*p8=0 
(4)、p2*p6*p8=0
如果同时满足以上四个条件则该点可以删除。这样反复迭代,直到获取细化图像为止。

过滤部分较为简单:

如果p2+p3+p8+p9>=1,则该点可以删除(赋值为0)。实现每两个白点之间不能紧靠在一起

检测部分比较复杂需要反复实验:

过程1 确定卷积邻域范围: 

p25 p10 p11 p12 p13

p24 p9 p2 p3 p14

  p23 p8 p1 p4 p15

p22 p7 p6 p5 p16

p21 p20 p19 p18 p17

(这里是使用5x5,实际上为了更好的检测需要至少6x6的卷积且为圆形)

过程2 统计卷积范围内白点个数:

如果白点个数较多,则说明p1为交叉点。

如果白点个数较少,则说明p1为端点。

过程3 对检测出的点进行合并:

如果两个点之间距离太近,取平均值。(下面代码没有实现该功能)

所有程序源代码:

  1. #include <opencv2/opencv.hpp>
  2. #include <opencv2/core/core.hpp>
  3. #include <iostream>
  4. #include <vector>
  5. using namespace cv;
  6. using namespace std;
  7. /**
  8. * @brief 对输入图像进行细化,骨骼化
  9. * @param src为输入图像,用cvThreshold函数处理过的8位灰度图像格式,元素中只有0与1,1代表有元素,0代表为空白
  10. * @param maxIterations限制迭代次数,如果不进行限制,默认为-1,代表不限制迭代次数,直到获得最终结果
  11. * @return 为对src细化后的输出图像,格式与src格式相同,元素中只有0与1,1代表有元素,0代表为空白
  12. */
  13. cv::Mat thinImage(const cv::Mat & src, const int maxIterations = -1)
  14. {
  15. assert(src.type() == CV_8UC1);
  16. cv::Mat dst;
  17. int width = src.cols;
  18. int height = src.rows;
  19. src.copyTo(dst);
  20. int count = 0; //记录迭代次数
  21. while (true)
  22. {
  23. count++;
  24. if (maxIterations != -1 && count > maxIterations) //限制次数并且迭代次数到达
  25. break;
  26. std::vector<uchar *> mFlag; //用于标记需要删除的点
  27. //对点标记
  28. for (int i = 0; i < height; ++i)
  29. {
  30. uchar * p = dst.ptr<uchar>(i);
  31. for (int j = 0; j < width; ++j)
  32. {
  33. //如果满足四个条件,进行标记
  34. // p9 p2 p3
  35. // p8 p1 p4
  36. // p7 p6 p5
  37. uchar p1 = p[j];
  38. if (p1 != 1) continue;
  39. uchar p4 = (j == width - 1) ? 0 : *(p + j + 1);
  40. uchar p8 = (j == 0) ? 0 : *(p + j - 1);
  41. uchar p2 = (i == 0) ? 0 : *(p - dst.step + j);
  42. uchar p3 = (i == 0 || j == width - 1) ? 0 : *(p - dst.step + j + 1);
  43. uchar p9 = (i == 0 || j == 0) ? 0 : *(p - dst.step + j - 1);
  44. uchar p6 = (i == height - 1) ? 0 : *(p + dst.step + j);
  45. uchar p5 = (i == height - 1 || j == width - 1) ? 0 : *(p + dst.step + j + 1);
  46. uchar p7 = (i == height - 1 || j == 0) ? 0 : *(p + dst.step + j - 1);
  47. if ((p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) >= 2 && (p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) <= 6)
  48. {
  49. int ap = 0;
  50. if (p2 == 0 && p3 == 1) ++ap;
  51. if (p3 == 0 && p4 == 1) ++ap;
  52. if (p4 == 0 && p5 == 1) ++ap;
  53. if (p5 == 0 && p6 == 1) ++ap;
  54. if (p6 == 0 && p7 == 1) ++ap;
  55. if (p7 == 0 && p8 == 1) ++ap;
  56. if (p8 == 0 && p9 == 1) ++ap;
  57. if (p9 == 0 && p2 == 1) ++ap;
  58. if (ap == 1 && p2 * p4 * p6 == 0 && p4 * p6 * p8 == 0)
  59. {
  60. //标记
  61. mFlag.push_back(p + j);
  62. }
  63. }
  64. }
  65. }
  66. //将标记的点删除
  67. for (std::vector<uchar *>::iterator i = mFlag.begin(); i != mFlag.end(); ++i)
  68. {
  69. **i = 0;
  70. }
  71. //直到没有点满足,算法结束
  72. if (mFlag.empty())
  73. {
  74. break;
  75. }
  76. else
  77. {
  78. mFlag.clear();//将mFlag清空
  79. }
  80. //对点标记
  81. for (int i = 0; i < height; ++i)
  82. {
  83. uchar * p = dst.ptr<uchar>(i);
  84. for (int j = 0; j < width; ++j)
  85. {
  86. //如果满足四个条件,进行标记
  87. // p9 p2 p3
  88. // p8 p1 p4
  89. // p7 p6 p5
  90. uchar p1 = p[j];
  91. if (p1 != 1) continue;
  92. uchar p4 = (j == width - 1) ? 0 : *(p + j + 1);
  93. uchar p8 = (j == 0) ? 0 : *(p + j - 1);
  94. uchar p2 = (i == 0) ? 0 : *(p - dst.step + j);
  95. uchar p3 = (i == 0 || j == width - 1) ? 0 : *(p - dst.step + j + 1);
  96. uchar p9 = (i == 0 || j == 0) ? 0 : *(p - dst.step + j - 1);
  97. uchar p6 = (i == height - 1) ? 0 : *(p + dst.step + j);
  98. uchar p5 = (i == height - 1 || j == width - 1) ? 0 : *(p + dst.step + j + 1);
  99. uchar p7 = (i == height - 1 || j == 0) ? 0 : *(p + dst.step + j - 1);
  100. if ((p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) >= 2 && (p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) <= 6)
  101. {
  102. int ap = 0;
  103. if (p2 == 0 && p3 == 1) ++ap;
  104. if (p3 == 0 && p4 == 1) ++ap;
  105. if (p4 == 0 && p5 == 1) ++ap;
  106. if (p5 == 0 && p6 == 1) ++ap;
  107. if (p6 == 0 && p7 == 1) ++ap;
  108. if (p7 == 0 && p8 == 1) ++ap;
  109. if (p8 == 0 && p9 == 1) ++ap;
  110. if (p9 == 0 && p2 == 1) ++ap;
  111. if (ap == 1 && p2 * p4 * p8 == 0 && p2 * p6 * p8 == 0)
  112. {
  113. //标记
  114. mFlag.push_back(p + j);
  115. }
  116. }
  117. }
  118. }
  119. //将标记的点删除
  120. for (std::vector<uchar *>::iterator i = mFlag.begin(); i != mFlag.end(); ++i)
  121. {
  122. **i = 0;
  123. }
  124. //直到没有点满足,算法结束
  125. if (mFlag.empty())
  126. {
  127. break;
  128. }
  129. else
  130. {
  131. mFlag.clear();//将mFlag清空
  132. }
  133. }
  134. return dst;
  135. }
  136. /**
  137. * @brief 对骨骼化图数据进行过滤,实现两个点之间至少隔一个空白像素
  138. * @param thinSrc为输入的骨骼化图像,8位灰度图像格式,元素中只有0与1,1代表有元素,0代表为空白
  139. */
  140. void filterOver(cv::Mat thinSrc)
  141. {
  142. assert(thinSrc.type() == CV_8UC1);
  143. int width = thinSrc.cols;
  144. int height = thinSrc.rows;
  145. for (int i = 0; i < height; ++i)
  146. {
  147. uchar * p = thinSrc.ptr<uchar>(i);
  148. for (int j = 0; j < width; ++j)
  149. {
  150. // 实现两个点之间至少隔一个像素
  151. // p9 p2 p3
  152. // p8 p1 p4
  153. // p7 p6 p5
  154. uchar p1 = p[j];
  155. if (p1 != 1) continue;
  156. uchar p4 = (j == width - 1) ? 0 : *(p + j + 1);
  157. uchar p8 = (j == 0) ? 0 : *(p + j - 1);
  158. uchar p2 = (i == 0) ? 0 : *(p - thinSrc.step + j);
  159. uchar p3 = (i == 0 || j == width - 1) ? 0 : *(p - thinSrc.step + j + 1);
  160. uchar p9 = (i == 0 || j == 0) ? 0 : *(p - thinSrc.step + j - 1);
  161. uchar p6 = (i == height - 1) ? 0 : *(p + thinSrc.step + j);
  162. uchar p5 = (i == height - 1 || j == width - 1) ? 0 : *(p + thinSrc.step + j + 1);
  163. uchar p7 = (i == height - 1 || j == 0) ? 0 : *(p + thinSrc.step + j - 1);
  164. if (p2 + p3 + p8 + p9 >= 1)
  165. {
  166. p[j] = 0;
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * @brief 从过滤后的骨骼化图像中寻找端点和交叉点
  173. * @param thinSrc为输入的过滤后骨骼化图像,8位灰度图像格式,元素中只有0与1,1代表有元素,0代表为空白
  174. * @param raudis卷积半径,以当前像素点位圆心,在圆范围内判断点是否为端点或交叉点
  175. * @param thresholdMax交叉点阈值,大于这个值为交叉点
  176. * @param thresholdMin端点阈值,小于这个值为端点
  177. * @return 为对src细化后的输出图像,格式与src格式相同,元素中只有0与1,1代表有元素,0代表为空白
  178. */
  179. std::vector<cv::Point> getPoints(const cv::Mat &thinSrc, unsigned int raudis = 4, unsigned int thresholdMax = 6, unsigned int thresholdMin = 4)
  180. {
  181. assert(thinSrc.type() == CV_8UC1);
  182. int width = thinSrc.cols;
  183. int height = thinSrc.rows;
  184. cv::Mat tmp;
  185. thinSrc.copyTo(tmp);
  186. std::vector<cv::Point> points;
  187. for (int i = 0; i < height; ++i)
  188. {
  189. for (int j = 0; j < width; ++j)
  190. {
  191. if (*(tmp.data + tmp.step * i + j) == 0)
  192. {
  193. continue;
  194. }
  195. int count=0;
  196. for (int k = i - raudis; k < i + raudis+1; k++)
  197. {
  198. for (int l = j - raudis; l < j + raudis+1; l++)
  199. {
  200. if (k < 0 || l < 0||k>height-1||l>width-1)
  201. {
  202. continue;
  203. }
  204. else if (*(tmp.data + tmp.step * k + l) == 1)
  205. {
  206. count++;
  207. }
  208. }
  209. }
  210. if (count > thresholdMax||count<thresholdMin)
  211. {
  212. Point point(j, i);
  213. points.push_back(point);
  214. }
  215. }
  216. }
  217. return points;
  218. }
  219. int main(int argc, char*argv[])
  220. {
  221. cv::Mat src;
  222. //获取图像
  223. if (argc != 2)
  224. {
  225. src = cv::imread("src.jpg", cv::IMREAD_GRAYSCALE);
  226. }
  227. else
  228. {
  229. src = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
  230. }
  231. if (src.empty())
  232. {
  233. std::cout << "读取文件失败!" << std::endl;
  234. return -1;
  235. }
  236. //将原图像转换为二值图像
  237. cv::threshold(src, src, 128, 1, cv::THRESH_BINARY);
  238. //图像细化,骨骼化
  239. cv::Mat dst = thinImage(src);
  240. //过滤细化后的图像
  241. filterOver(dst);
  242. //查找端点和交叉点
  243. std::vector<cv::Point> points = getPoints(dst,6,9,6);
  244. //二值图转化成灰度图,并绘制找到的点
  245. dst = dst * 255;
  246. src = src * 255;
  247. vector<cv::Point>::iterator it = points.begin();
  248. for (;it != points.end(); it++)
  249. {
  250. circle(dst, *it,4,255, 1);
  251. }
  252. imwrite("dst.jpg", dst);
  253. //显示图像
  254. cv::namedWindow("src1", CV_WINDOW_AUTOSIZE);
  255. cv::namedWindow("dst1", CV_WINDOW_AUTOSIZE);
  256. cv::imshow("src1", src);
  257. cv::imshow("dst1", dst);
  258. cv::waitKey(0);
  259. }
测试结果1图片:

原图

细化及检测结果

测试结果2图片:

原图

细化及检测结果

整个程序运行时间大约需要0.02秒,不会占用什么资源,代码还可以进一步优化,检测出的点也没有过滤合并。对于拐点的检测可以使用局部求导,多点拟合

或者傅里叶变换。有实现的朋友大家可以共享代码。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/605127
推荐阅读
相关标签
  

闽ICP备14008679号