Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crop image in skimage?

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.imread(filename)
 cropped = image(x1,y1,x2,y2)

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

like image 939
user824624 Avatar asked Oct 22 '15 18:10

user824624


People also ask

Is scikit-image same as Skimage?

scikit-image (a.k.a. skimage ) is a collection of algorithms for image processing and computer vision.

How do you read pictures in Skimage?

Specifically, you can use the Skimage imread function to read images in from files on your computer, to a form usable in a Python program. The skimage imread function is pretty simple to use, but to use it properly, you need to understand the syntax. That being the case, let's take a look at the syntax of the function.


2 Answers

This seems a simple syntax error.

Well, in Matlab you can use _'parentheses'_ to extract a pixel or an image region. But in Python, and numpy.ndarray you should use the brackets to slice a region of your image, besides in this code you is using the wrong way to cut a rectangle.

The right way to cut is using the : operator.

Thus,

from skimage import io
image = io.imread(filename)
cropped = image[x1:x2,y1:y2]
like image 162
Darleison Rodrigues Avatar answered Nov 06 '22 11:11

Darleison Rodrigues


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

enter image description here

like image 33
Sandipan Dey Avatar answered Nov 06 '22 12:11

Sandipan Dey