Crop image in skimage?

I use skimage to crop the rectangle in this image, now I have (x1, y1, x2, y2) as the coordinates of the rectangle, then I uploaded the image

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

However, this is the wrong way to crop the image, as I would do it correctly in skimage

+6
source share
4 answers

This seems like a simple syntax error.

Well, in Matlab you can use _'parentheses'_to extract a pixel or area of ​​an image. But in Python and numpy.ndarrayyou should use brackets to cut off the area of ​​your image, in addition, in this code you use the wrong way to cut a rectangle.

- :.

,

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

Image PIL

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

I need to complete this action a lot. I can confirm that the iver56 comment on Darleison's request is correct, at least when using skimage.

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

You can also use skimage.util.crop()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 the original and cropped image):

enter image description here

0
source

Source: https://habr.com/ru/post/1612808/


All Articles