0
I = imread('data1.jpg')
[p3, p4] = size(I);
q1 = 50; % size of the crop box
i3_start = floor((p3-q1)/2); % or round instead of floor; using neither gives warning
i3_stop = i3_start + q1;

i4_start = floor((p4-q1)/2);
i4_stop = i4_start + q1;

I = I(i3_start:i3_stop, i4_start:i4_stop, :);
figure ,imshow(I);

I have run this code and get this error " Index exceeds matrix dimensions.

Error in ==> croptry at 10 I = I(i3_start:i3_stop, i4_start:i4_stop, :);"

Can anybody help me to fix this error? I want to crop image at the center

Andrea
  • 11,801
  • 17
  • 65
  • 72

1 Answers1

1

The error is probably due to the way you call the functin size.

If the matrix I in which you load the image is tri-dimensional (N x M x K), you have to call size this way:

[p3, p4, p5] = size(I)

that is, by adding an additional parameter (in that case "p5").

If you call size as:

[p3, p4] = size(I)

p4 will be set to the product of the second and third dimension of your matrix I

Updated code

I = imread('pdb_img_1.jpg');
% Modified call to "size"
% [p3, p4] = size(I)
[p3, p4, p5] = size(I)
% Increased the size of the "crop box"
q1 = 150; % size of the crop box
i3_start = floor((p3-q1)/2) % or round instead of floor; using neither gives warning
i3_stop = i3_start + q1

i4_start = floor((p4-q1)/2)
i4_stop = i4_start + q1

I = I(i3_start:i3_stop, i4_start:i4_stop, :);
figure
imshow(I)

Original Image

enter image description here

Cropped Image enter image description here Hope this helps.

il_raffa
  • 5,090
  • 129
  • 31
  • 36