当前位置:   article > 正文

图像处理之地图轮廓美化_地图美化算法

地图美化算法

在利用传感器获取房间地图信息后由于多次测量存在误差,导致房间地图轮廓歪歪曲曲影响观感,我这里采用水平方向和垂直方向分别执行邻近线段合并的方法对这一问题做了一些处理。以下图为例,黑色表示地图轮廓,比如在垂直方向执行邻近线段合并时先获取图像3列数据作为一个计算单元,如下图的计算单元a、b、c,再将这些计算单元里邻近的线段合并成一条直线,至于计算单元c,由于其近似于曲线不进行合并,避免矫枉过正。

计算单元处理前后

完整代码如下所示:

  1. #include <opencv2/highgui/highgui.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <vector>
  4. using namespace std;
  5. using namespace cv;
  6. //函数功能:排序(从小到大)
  7. template<typename T>
  8. void quick_sort(vector<T>& arr, int left, int right, vector<int>& arr_index)
  9. {
  10. int i, j;
  11. int t1,temp1;
  12. T temp,t;
  13. if (left>right)
  14. return;
  15. temp = arr[left];
  16. temp1 = arr_index[left];
  17. i = left;
  18. j = right;
  19. while (i != j)
  20. {
  21. while (arr[j] >= temp && i<j)
  22. j--;
  23. while (arr[i] <= temp && i<j)
  24. i++;
  25. if (i<j)
  26. {
  27. t = arr[i];
  28. arr[i] = arr[j];
  29. arr[j] = t;
  30. t1 = arr_index[i];
  31. arr_index[i] = arr_index[j];
  32. arr_index[j] = t1;
  33. }
  34. }
  35. arr[left] = arr[i];
  36. arr[i] = temp;
  37. arr_index[left] = arr_index[i];
  38. arr_index[i] = temp1;
  39. quick_sort(arr,left, i - 1,arr_index);
  40. quick_sort(arr,i + 1, right,arr_index);
  41. }
  42. //函数功能:根据给定索引进行排序
  43. template<typename T>
  44. void resort_arr(vector<T>& arr, vector<int> indexs)
  45. {
  46. int len = arr.size();
  47. T* backup = new T[len];
  48. for(int i=0; i<len; i++){
  49. backup[i] = arr[i];
  50. }
  51. for(int i=0; i<len; i++){
  52. arr[i] = backup[indexs[i]];
  53. }
  54. }
  55. /*
  56. 函数功能:在二维数组中绘制指定位置的线段
  57. arr:二维数组,可理解为二值化后的灰度图像
  58. cols:图像的宽
  59. ltop:线段左上位置的端点
  60. rbottom:线段的右下位置的端点
  61. val:绘制直线线段的值
  62. */
  63. void set_array(uchar* arr, int cols, Point ltop, Point rbottom, int val)
  64. {
  65. // 直线绘制
  66. if(ltop.x == rbottom.x || ltop.y == rbottom.y)
  67. {
  68. for(int i=ltop.y; i<=rbottom.y; i++)
  69. {
  70. for(int j=ltop.x; j<=rbottom.x; j++)
  71. {
  72. *(arr+i*cols+j) = val;
  73. }
  74. }
  75. }
  76. // 斜线绘制,绘制斜线时须在X和Y两个方向进行遍历,否则对于近乎水平或竖直的斜线绘制会有问题
  77. else
  78. {
  79. float x_slope = float(rbottom.y - ltop.y) / (rbottom.x - ltop.x);
  80. for(int i=ltop.x; i<=rbottom.x; i++)
  81. {
  82. int yoffset = (i-ltop.x)*x_slope + ltop.y;
  83. *(arr+yoffset*cols+i) = val;
  84. }
  85. float y_slope = float(rbottom.x - ltop.x) / (rbottom.y - ltop.y);
  86. for(int j=ltop.y; j<=rbottom.y; j++)
  87. {
  88. int xoffset = (j-ltop.y)*y_slope + ltop.x;
  89. *(arr+j*cols+xoffset) = val;
  90. }
  91. }
  92. }
  93. /*
  94. 函数功能:在竖直方向和水平方向合并邻近的直线线段(对斜线不作处理)
  95. image_data:二维数组,可以认为是二值化后的灰度图像
  96. img_w:图像宽
  97. img_h:图像高
  98. merge_step:每merge_step行或列作为一个线段合并的计算单元
  99. merge_max_gap:前后线段之间的首尾距离小于merge_max_gap才满足合并条件
  100. merge_len_thresh:合并的两条线段的长度和大于merge_len_thresh才满足合并条件(避免斜线被合并)
  101. */
  102. void merge_lines(uchar* image_data,
  103. int img_w,
  104. int img_h,
  105. int merge_step = 3,
  106. int merge_max_gap = 3,
  107. int merge_len_thresh = 30)
  108. {
  109. // 竖直方向合并线段
  110. for(int n=0; n<img_w-merge_step; n++)
  111. {
  112. int line_num = 0; //检测到的线段数量
  113. vector<vector<int>> vec_lines; //存储计算单元内每列的线段首尾偏移数据
  114. for(int x=n; x<n+merge_step; x++)
  115. {
  116. vector<int> vec_line;
  117. int last_val = 0;
  118. int head_off = 0;
  119. int tail_off = 0;
  120. for(int y=0; y<img_h; y++)
  121. {
  122. int gray_scale = *(image_data+y*img_w+x);
  123. if(gray_scale==255 && last_val==0){
  124. head_off = y;
  125. }
  126. if((gray_scale==0 && last_val==255)){
  127. tail_off = y;
  128. // 长度为1的线段忽略,避免处理后直角处出现断裂
  129. if(tail_off - head_off == 1)
  130. {
  131. last_val = 0;
  132. continue;
  133. }
  134. vec_line.push_back(head_off);
  135. vec_line.push_back(tail_off);
  136. line_num++;
  137. }
  138. last_val = gray_scale;
  139. }
  140. vec_lines.push_back(vec_line);
  141. }
  142. // 不少于2条线段执行合并操作
  143. if(line_num >= 2)
  144. {
  145. vector<int> heads; //保存线段的头部偏移
  146. vector<int> tails; //保存线段的尾部偏移
  147. vector<int> indexs;
  148. for(int i=0; i<vec_lines.size(); i++)
  149. {
  150. for(int j=0; j<vec_lines[i].size(); j+=2)
  151. {
  152. heads.push_back(vec_lines[i][j]);
  153. tails.push_back(vec_lines[i][j+1]);
  154. indexs.push_back(indexs.size());
  155. }
  156. }
  157. // 对计算单元内所有的线段头部偏移进行从小到大的排序
  158. quick_sort(heads, 0, line_num-1, indexs);
  159. // 所有的尾部偏移根据头部偏移的排序索引重新排序
  160. resort_arr(tails, indexs);
  161. vector<int> merge_heads; //线段合并后的头部偏移数组,同一列需要合并的线段可能不止一条
  162. vector<int> merge_tails; //线段合并后的尾部偏移数组
  163. int current_merge_head = heads[0]; //当前合并线段的头部偏移
  164. int current_merge_tail = tails[0]; //当前合并线段的尾部偏移
  165. for(int i=1; i<line_num; i++)
  166. {
  167. int merge_len = current_merge_tail - current_merge_head;
  168. int line_len = tails[i] - heads[i];
  169. // 合并条件:当前合并线段的尾部偏移和下一条线段的头部偏移最大的距离不超过阈值,
  170. // 且二者长度和需大于阈值,防止邻近的两条斜线被误合并
  171. if(current_merge_tail-heads[i] > -merge_max_gap
  172. && merge_len+line_len > merge_len_thresh)
  173. {
  174. current_merge_tail = max(current_merge_tail, tails[i]);
  175. }
  176. else
  177. {
  178. if(merge_len > merge_len_thresh)
  179. {
  180. merge_heads.push_back(current_merge_head);
  181. merge_tails.push_back(current_merge_tail);
  182. }
  183. current_merge_head = heads[i];
  184. current_merge_tail = tails[i];
  185. }
  186. }
  187. // 若最后一条线段也需要合并,则需要手动把合并线段加进来(此处可能会添加重复,不过不影响)
  188. if(current_merge_tail - current_merge_head > merge_len_thresh)
  189. {
  190. merge_heads.push_back(current_merge_head);
  191. merge_tails.push_back(current_merge_tail);
  192. }
  193. // 遍历计算单元里所有线段,将组成合并线段的各个子线段清除
  194. for(int k=0; k<merge_heads.size(); k++)
  195. {
  196. Point merge_head_pt(-1);
  197. Point merge_tail_pt(-1);
  198. for(int i=0; i<vec_lines.size(); i++)
  199. {
  200. for(int j=0; j<vec_lines[i].size(); j+=2)
  201. {
  202. if(vec_lines[i][j] >= merge_heads[k] && vec_lines[i][j+1] <= merge_tails[k])
  203. {
  204. // 记录合并线段的头坐标点
  205. if(vec_lines[i][j] == merge_heads[k] && merge_head_pt.x == -1)
  206. {
  207. merge_head_pt.x = n+i;
  208. merge_head_pt.y = merge_heads[k];
  209. }
  210. // 记录合并线段的尾坐标点
  211. if(vec_lines[i][j+1] == merge_tails[k] && merge_tail_pt.x == -1)
  212. {
  213. merge_tail_pt.x = n+i;
  214. merge_tail_pt.y = merge_tails[k]-1;
  215. }
  216. // 将子线段的像素值置0
  217. set_array(image_data, img_w, Point(n+i,vec_lines[i][j]), Point(n+i, vec_lines[i][j+1]-1), 0);
  218. }
  219. }
  220. }
  221. // 将合并线段的像素值置255
  222. set_array(image_data, img_w, merge_head_pt, merge_tail_pt, 255);
  223. }
  224. }
  225. }
  226. // 水平方向合并线段(同竖直方向合并线段同理)
  227. for(int n=0; n<img_h-merge_step; n++)
  228. {
  229. int line_num = 0;
  230. vector<vector<int>> vec_lines;
  231. for(int y=n; y<n+merge_step; y++)
  232. {
  233. vector<int> vec_line;
  234. int last_val = 0;
  235. int head_off = 0;
  236. int tail_off = 0;
  237. for(int x=0; x<img_w; x++)
  238. {
  239. int gray_scale = *(image_data+y*img_w+x);
  240. if(gray_scale==255 && last_val==0){
  241. head_off = x;
  242. }
  243. if((gray_scale==0 && last_val==255)){
  244. tail_off = x;
  245. if(tail_off - head_off == 1)
  246. {
  247. last_val = 0;
  248. continue;
  249. }
  250. vec_line.push_back(head_off);
  251. vec_line.push_back(tail_off);
  252. line_num++;
  253. }
  254. last_val = gray_scale;
  255. }
  256. vec_lines.push_back(vec_line);
  257. }
  258. if(line_num >= 2)
  259. {
  260. vector<int> heads;
  261. vector<int> tails;
  262. vector<int> indexs;
  263. for(int i=0; i<vec_lines.size(); i++)
  264. {
  265. for(int j=0; j<vec_lines[i].size(); j+=2)
  266. {
  267. heads.push_back(vec_lines[i][j]);
  268. tails.push_back(vec_lines[i][j+1]);
  269. indexs.push_back(indexs.size());
  270. }
  271. }
  272. quick_sort(heads, 0, line_num-1, indexs);
  273. resort_arr(tails, indexs);
  274. vector<int> merge_heads;
  275. vector<int> merge_tails;
  276. int current_merge_head = heads[0];
  277. int current_merge_tail = tails[0];
  278. for(int i=1; i<line_num; i++)
  279. {
  280. int merge_len = current_merge_tail - current_merge_head;
  281. int line_len = tails[i] - heads[i];
  282. if(current_merge_tail-heads[i] > -merge_max_gap
  283. && merge_len+line_len > merge_len_thresh)
  284. {
  285. current_merge_tail = max(current_merge_tail, tails[i]);
  286. }
  287. else
  288. {
  289. if(merge_len > merge_len_thresh)
  290. {
  291. merge_heads.push_back(current_merge_head);
  292. merge_tails.push_back(current_merge_tail);
  293. }
  294. current_merge_head = heads[i];
  295. current_merge_tail = tails[i];
  296. }
  297. }
  298. if(current_merge_tail - current_merge_head > merge_len_thresh)
  299. {
  300. merge_heads.push_back(current_merge_head);
  301. merge_tails.push_back(current_merge_tail);
  302. }
  303. for(int k=0; k<merge_heads.size(); k++)
  304. {
  305. Point merge_head_pt(-1,-1);
  306. Point merge_tail_pt(-1,-1);
  307. for(int i=0; i<vec_lines.size(); i++)
  308. {
  309. for(int j=0; j<vec_lines[i].size(); j+=2)
  310. {
  311. if(vec_lines[i][j] >= merge_heads[k] && vec_lines[i][j+1] <= merge_tails[k])
  312. {
  313. if(vec_lines[i][j] == merge_heads[k] && merge_head_pt.x == -1)
  314. {
  315. merge_head_pt.x = merge_heads[k];
  316. merge_head_pt.y = n+i;
  317. }
  318. if(vec_lines[i][j+1] == merge_tails[k] && merge_tail_pt.x == -1)
  319. {
  320. merge_tail_pt.x = merge_tails[k]-1;
  321. merge_tail_pt.y = n+i;
  322. }
  323. set_array(image_data, img_w, Point(vec_lines[i][j], n+i), Point(vec_lines[i][j+1]-1, n+i), 0);
  324. }
  325. }
  326. }
  327. set_array(image_data, img_w, merge_head_pt, merge_tail_pt, 255);
  328. }
  329. }
  330. }
  331. }
  332. int main()
  333. {
  334. Mat src_img = imread("../mergeLines/test.png", IMREAD_GRAYSCALE);
  335. imshow("合并线段前", src_img);
  336. // 合并邻近的直线
  337. merge_lines(src_img.data, src_img.cols, src_img.rows, 3, 3, 30);
  338. imshow("合并线段后", src_img);
  339. waitKey();
  340. return 0;
  341. }

最后效果如下:

 

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

闽ICP备14008679号