基于OpenCV的批量处理tiff图片黑边(QT环境)

luoshijie

发布日期: 2020-12-03 18:49:03 浏览量: 174
评分:
star star star star star star star star star star_border
*转载请注明来自write-bug.com

这几天基础OpenCV,练习写了一个去黑边程序,新手代码,记录一下。

该方法为扫描线法,遇到非黑边内的(0,0,0)黑色时,用坐标排除(不在边界上跳过)。

PS:这里还提供一种区域增长思路,找图片黑色区域,面积最大的区域为需要去除的黑边。

基本思路

  • 遍历导入图片,遍历像素,找到黑边所在的矩形框坐标,剪切图片(一分为四)

  • 根据矩形坐标,计算新的地理位置信息

  • 删除带有黑边的图片和tfw文件信息

  • 打开和写入保存tif图片和tfw位置信息

博客链接

  1. //传入图片 返回图片分割的图片 得到切点坐标
  2. QVector<Mat> separationTif(Mat &iplImg, Point2d &cutPnt)
  3. {
  4. QVector<Mat> vecMat;
  5. int width = iplImg.size().width;
  6. int height = iplImg.size().height;
  7. int begin_wid = 0, end_wid = 0, begin_heig = 0, end_heig = 0; //标记黑边 坐标
  8. int i = 0, j = 0;
  9. int blackNumber = 0;
  10. for (j = 0; j < height; ++j)
  11. {
  12. for (i = 0; i < width; ++i)
  13. {
  14. int tmpb, tmpg, tmpr;
  15. tmpb = cvGet2D(&(IplImage)iplImg, j, i).val[0];
  16. tmpg = cvGet2D(&(IplImage)iplImg, j, i).val[1];
  17. tmpr = cvGet2D(&(IplImage)iplImg, j, i).val[2];
  18. if (tmpb == 0 && tmpg == 0 && tmpr == 0) //这里RGB数字可以灵活变动
  19. {
  20. ++blackNumber;
  21. }
  22. }
  23. }
  24. if (blackNumber == 0) //不存在黑边
  25. {
  26. vecMat << iplImg;
  27. return vecMat;
  28. }
  29. else
  30. {
  31. for (j = 0; j < height; ++j)
  32. {
  33. bool flag = false;
  34. for (i = 0; i < width; ++i)
  35. {
  36. int tmpb, tmpg, tmpr;
  37. tmpb = cvGet2D(&(IplImage)iplImg, j, i).val[0];
  38. tmpg = cvGet2D(&(IplImage)iplImg, j, i).val[1];
  39. tmpr = cvGet2D(&(IplImage)iplImg, j, i).val[2];
  40. if (tmpb == 0 && tmpg == 0 && tmpr == 0)
  41. {
  42. if (i > 0 && i < width - 1 && j > 0 && j < height - 1)
  43. ;
  44. else
  45. {
  46. flag = true;
  47. begin_heig = j;
  48. break;
  49. }
  50. }
  51. }
  52. if (flag) break;
  53. }
  54. for (j = height - 1; j >= begin_heig; --j)
  55. {
  56. bool flag = false; //判断
  57. for (i = 0; i < width; ++i)
  58. {
  59. int tmpb, tmpg, tmpr;
  60. tmpb = cvGet2D(&(IplImage)iplImg, j, i).val[0];
  61. tmpg = cvGet2D(&(IplImage)iplImg, j, i).val[1];
  62. tmpr = cvGet2D(&(IplImage)iplImg, j, i).val[2];
  63. if (tmpb == 0 && tmpg == 0 && tmpr == 0)
  64. {
  65. if (i > 0 && i < width - 1 && j > 0 && j < height - 1)
  66. ;
  67. else
  68. {
  69. flag = true;
  70. end_heig = j;
  71. break;
  72. }
  73. }
  74. }
  75. if (flag) break;
  76. }
  77. for (i = 0; i < width; ++i)
  78. {
  79. bool flag = false;
  80. for (j = 0; j < height; ++j)
  81. {
  82. int tmpb, tmpg, tmpr;
  83. tmpb = cvGet2D(&(IplImage)iplImg, j, i).val[0];
  84. tmpg = cvGet2D(&(IplImage)iplImg, j, i).val[1];
  85. tmpr = cvGet2D(&(IplImage)iplImg, j, i).val[2];
  86. if (tmpb == 0 && tmpg == 0 && tmpr == 0)
  87. {
  88. if (i > 0 && i < width - 1 && j > 0 && j < height - 1)
  89. ;
  90. else
  91. {
  92. flag = true;
  93. begin_wid = i;
  94. break;
  95. }
  96. }
  97. }
  98. if (flag) break;
  99. }
  100. for (i = width - 1; i >= begin_wid; --i)
  101. {
  102. bool flag = false;
  103. for (j = 0; j < height; ++j)
  104. {
  105. int tmpb, tmpg, tmpr;
  106. tmpb = cvGet2D(&(IplImage)iplImg, j, i).val[0];
  107. tmpg = cvGet2D(&(IplImage)iplImg, j, i).val[1];
  108. tmpr = cvGet2D(&(IplImage)iplImg, j, i).val[2];
  109. if (tmpb == 0 && tmpg == 0 && tmpr == 0)
  110. {
  111. if (i > 0 && i < width - 1 && j > 0 && j < height - 1)
  112. ;
  113. else
  114. {
  115. flag = true;
  116. end_wid = i;
  117. break;
  118. }
  119. }
  120. }
  121. if (flag) break;
  122. }
  123. if (begin_wid > 0 && begin_wid < (width - 1))
  124. cutPnt.x = begin_wid;
  125. else
  126. cutPnt.x = end_wid;
  127. if (begin_heig > 0 && begin_heig < (height - 1))
  128. cutPnt.y = begin_heig;
  129. else
  130. cutPnt.y = end_heig;
  131. if (cutPnt.x != 0 || cutPnt.y != 0)
  132. { //裁剪的中心点不在(0,0)时 剪切图像
  133. cout << "裁剪的中心点为:" << endl;
  134. qDebug() << cutPnt.x << cutPnt.y;
  135. Mat img0 = Mat(iplImg, Rect(0, 0, cutPnt.x + 1, cutPnt.y + 1)); //左上
  136. Mat img1 = Mat(iplImg, Rect(0, cutPnt.y, cutPnt.x + 1, height - cutPnt.y - 1)); //左下
  137. Mat img2 = Mat(iplImg, Rect(cutPnt.x, 0, width - cutPnt.x - 1, cutPnt.y + 1)); //右上
  138. Mat img3 = Mat(iplImg, Rect(cutPnt.x, cutPnt.y, width - cutPnt.x - 1, height - cutPnt.y - 1)); //右下
  139. vecMat << img0 << img1 << img2 << img3;
  140. }
  141. else
  142. {
  143. qDebug() << "Do not have black_border";
  144. vecMat << iplImg;
  145. }
  146. return vecMat;
  147. }
  148. }
上传的附件 cloud_download 源码.zip ( 22.27mb, 5次下载 )
error_outline 下载需要7点积分

发送私信

1
文章数
0
评论数
最近文章
eject