【问题标题】:Convert a binary image to 2D array or Matrix in Python?在 Python 中将二进制图像转换为二维数组或矩阵?
【发布时间】:2017-04-16 00:00:13
【问题描述】:

我是图像处理的新手,我真的很难理解一些东西......所以我的想法是如何在 python 中从二进制图像创建矩阵?

到这样的事情:

虽然要点在那里,但它不是相同的图像。 谢谢你的帮助,我很感激干杯

【问题讨论】:

    标签: python python-2.7 numpy image-processing


    【解决方案1】:

    使用 cv2 -阅读更多here

    import cv2
    img = cv2.imread('path/to/img.jpg')
    resized = cv2.resize(img, (128, 128), cv2.INTER_LINEAR)
    print pic
    

    使用 skimage - 阅读更多 here

    import skimage.io as skio
    faces = skio.imread_collection('path/to/images/*.pgm',conserve_memory=True) # can load multiple images at once
    

    使用 Scipy - 阅读更多 here

    from scipy import misc
    pic = misc.imread('path/to/img.jpg')
    print pic
    

    绘制图像

    import matplotlib.pyplot as plt
    plt.imshow(faces[0],cmap=plt.cm.gray_r,interpolation="nearest")
    plt.show()
    

    【讨论】:

      【解决方案2】:

      我目前正在使用的示例。

      """
      A set of utilities that are helpful for working with images. These are utilities
      needed to actually apply the seam carving algorithm to images
      """
      
      
      from PIL import Image
      
      
      class Color:
          """
          A simple class representing an RGB value.
          """
      
          def __init__(self, r, g, b):
              self.r = r
              self.g = g
              self.b = b
      
          def __repr__(self):
              return f'Color({self.r}, {self.g}, {self.b})'
      
          def __str__(self):
              return repr(self)
      
      
      def read_image_into_array(filename):
          """
          Read the given image into a 2D array of pixels. The result is an array,
          where each element represents a row. Each row is an array, where each
          element is a color.
      
          See: Color
          """
      
          img = Image.open(filename, 'r')
          w, h = img.size
      
          pixels = list(Color(*pixel) for pixel in img.getdata())
          return [pixels[n:(n + w)] for n in range(0, w * h, w)]
      
      
      def write_array_into_image(pixels, filename):
          """
          Write the given 2D array of pixels into an image with the given filename.
          The input pixels are represented as an array, where each element is a row.
          Each row is an array, where each element is a color.
      
          See: Color
          """
      
          h = len(pixels)
          w = len(pixels[0])
      
          img = Image.new('RGB', (w, h))
      
          output_pixels = img.load()
          for y, row in enumerate(pixels):
              for x, color in enumerate(row):
                  output_pixels[x, y] = (color.r, color.g, color.b)
      
          img.save(filename)
      

      【讨论】:

        猜你喜欢
        • 2019-05-22
        • 2013-12-17
        • 1970-01-01
        • 1970-01-01
        • 2015-06-01
        • 1970-01-01
        • 2020-03-03
        • 2018-06-30
        • 1970-01-01
        相关资源
        最近更新 更多