crop image in skimage?

前端 未结 4 1756
小蘑菇
小蘑菇 2021-02-09 04:30

I\'m using skimage to crop a rectangle in a given image, now I have (x1,y1,x2,y2) as the rectangle coordinates, then I had loaded the image

 image = skimage.io.         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-02-09 04:41

    One could use skimage.util.crop() function too, as shown in the following code:

    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()
    

    with the following output (with original and cropped image):

提交回复
热议问题