【问题标题】:crop image in skimage?在skimage中裁剪图像?
【发布时间】:2016-01-22 02:53:54
【问题描述】:

我正在使用 skimage 裁剪给定图像中的矩形,现在我有 (x1,y1,x2,y2) 作为矩形坐标,然后我已经加载了图像

 image = skimage.io.imread(filename)
 cropped = image(x1,y1,x2,y2)

但是这是裁剪图像的错误方法,我将如何在 skimage 中以正确的方式进行操作

【问题讨论】:

    标签: python image-processing scikit-image


    【解决方案1】:

    这似乎是一个简单的语法错误。

    好吧,在 Matlab 中,您可以使用_'parentheses'_ 来提取像素或图像区域。但是在 Python 和numpy.ndarray 中,您应该使用方括号来分割图像的一个区域,此外在这段代码中,您使用了错误的方式来切割一个矩形。

    正确的剪切方法是使用: 运算符。

    因此,

    from skimage import io
    image = io.imread(filename)
    cropped = image[x1:x2,y1:y2]
    

    【讨论】:

    • image[y1:y2, x1:x2] 在这里不是更正确,因为 x 指的是水平轴吗?
    【解决方案2】:

    你可以继续使用 PIL 库的 Image 模块

    from PIL import Image
    im = Image.open("image.png")
    im = im.crop((0, 50, 777, 686))
    im.show()
    

    【讨论】:

    • Image not image in from PIL import image
    【解决方案3】:

    也可以使用skimage.util.crop()函数,如下代码所示:

    import numpy as np
    from skimage.io import imread
    from skimage.util import crop
    import matplotlib.pylab as plt
    
    A = imread('lena.jpg')
    
    # crop_width{sequence, int}: Number of values to remove from the edges of each axis. 
    # ((before_1, after_1), … (before_N, after_N)) specifies unique crop widths at the 
    # start and end of each axis. ((before, after),) specifies a fixed start and end 
    # crop for every axis. (n,) or n for integer n is a shortcut for before = after = n 
    # for all axes.
    B = crop(A, ((50, 100), (50, 50), (0,0)), copy=False)
    
    print(A.shape, B.shape)
    # (220, 220, 3) (70, 120, 3)
    
    plt.figure(figsize=(20,10))
    plt.subplot(121), plt.imshow(A), plt.axis('off') 
    plt.subplot(122), plt.imshow(B), plt.axis('off') 
    plt.show()
    

    具有以下输出(带有原始图像和裁剪图像):

    【讨论】:

      【解决方案4】:

      您可以使用 skimage 裁剪图像,只需像下面这样切片图像数组:

      image = image_name[y1:y2, x1:x2]
      

      示例代码:

      from skimage import io
      import matplotlib.pyplot as plt
      
      image = io.imread(image_path)
      cropped_image = image[y1:y2, x1:x2]
      plt.imshow(cropped_image)
      

      【讨论】:

        猜你喜欢
        • 2015-02-02
        • 2017-05-22
        • 1970-01-01
        • 2011-09-12
        • 2014-11-30
        • 2011-01-01
        • 2011-01-01
        • 2016-12-05
        相关资源
        最近更新 更多