How to crop rows and columns of pixels out in MATLAB

16,450

Solution 1

What you need is the built-in MATLAB function imcrop. To use it, specify something like

B = imcrop(A,[xmin ymin width height]);

if A is your original image. First find the dimensions of your image. Say its 800 by 600. Then you are looking to crop a 770 by 580 image so these numbers respectively will be your width and height in the above function. Your x and y would be something like 5 and 20, respectively.

Solution 2

U can use imcrop for this if you have image processing toolbox or you can make new image as follows:

I2 = I(21:end-5, 6:end-5)

For 3 dimensions, you can use:

I2 = I(21:end-5,6:end-5,:)

For example as per your comment:

I = rand(153,1510,3);
size(I); %  153        1510           3
I2 = I(21:end-5,6:end-5,:);
size(I2); % 128        1500           3
Share:
16,450
singmotor
Author by

singmotor

Updated on June 07, 2022

Comments

  • singmotor
    singmotor almost 2 years

    I have an image with a white border around it, and I need to get rid of the border. There are 20 rows of white pixels above the image, 5 columns of white to the left, 5 of white columns to the right, and 5 rows of white below the image. I wan't to crop the image exactly out of that border, how do I do this in matlab? Thanks for any help you can give!

    (The image is a tiff, which is why I can't use an online service for this, they won't let me upload .tiff)