修剪灰度图像的空白

时间:2018-05-29 20:36:21

标签: python image numpy deep-learning

image like this

我得到一个图像集,图像是这样的。

如何使用python删除图像下面的白色部分,而python不包含任何有用的内容?

我在python中将图像读取为numpy数组。

我的代码是这样的:

<div class="container-fluid">
             <div class="row">
          <nav class="col-md-2 d-none d-md-block bg-light sidebar">
              <div class="sidebar-sticky">

                  <h1>text</h1>
                    <p>text </p>

   </div>
                  <!--sticky footer-->
          <footer class="footer">
              <div >
                          <p align="center"><a href="index.html">Home</a> | <a href="es/test.html">spanish</a><br>
                          <!--<p>This interactive was made possible through the support of</p>-->
                          <a href="index.html"><img src="images/logo.png" alt="logo" style="width:100%" /></a></p>

                      </div>

                  </footer>

           </nav>
  </div>
        <main role="main" class="col-md-9 ml-sm-auto col-lg-12 px-0">

            <!-- Featured Content  -->

              <div class="embed-responsive embed-responsive-16by9" onload="onload="redirect();">


                      <iframe style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none;
                   margin:0; padding:0; overflow:hidden; z-index:1;" class="embed-responsive-item" src="models/page.html" allowfullscreen></iframe>


              </div>

        </main>
    </div>

2 个答案:

答案 0 :(得分:1)

这会在上方和下方逐行修剪白色空间(实际上它修剪任何完整的白色行):

trimmed = image[np.where(~np.all(image == 255, axis=1))]

如果您需要修剪上边距和下边距,您可以这样做:

empty_row_mask = np.all(image == 255, axis=1)
top = np.searchsorted(~empty_row_mask, True)
bottom = np.searchsorted(empty_row_mask, True)
trimmed = image[top:bottom]

答案 1 :(得分:0)

我找到了一种使用openCV3和python3.6

的方法
image_neg = cv2.bitwise_not(image) # invert the root to white
coords = cv2.findNonZero(image_neg) # find all the non-zero points (root)
x, y, w, h = cv2.boundingRect(coords) # find minimum spanning bounding box
rect = image[y:y+h, x:x+w]