Problem 44958. Crop an Image
Solution Stats
Problem Comments

22 Comments
you should mention which is which in this matrix Icrop = crop_image(I, 2, 2, 1, 3)
Thank you Payam. I assume you meant specifying the order of the inputs explicitly. I have updated the description to make it clearer.
Good problem!
Can someone help me to solve this question? I did not learn image processing. So I tried to solve this question assuming that the pixels either have a value of 1 or 0. I am a beginner. This is my second day into coding. I would really love some help. This is my code:
[Ix,Iy]=size(I);
for n = 1:Ix %for Rmin
if I(n,:)==0
n = n + 1;
else n = n;
break
end
end
Rmin = n
for m = 1:Iy %for Cmin
if I(:,m)==0
m = m + 1;
else m = m;
break
end
end
Cmin = m
for o = Rmin:Ix %for Rmax
if I(o,Cmin) == 1
o = o+1;
elseif I(o,Cmin) == 0
o = o  1;
break;
end
end
Rmax = o
for p = Cmin:Iy %for Cmax
if I(Rmin,p) == 1
p = p+1;
elseif I(Rmin,p) == 0
p = p  1;
break;
end
end
Cmax = p
Rpix = Rmax  Rmin + 1
Cpix = Cmax  Cmin + 1
Hint please?
Hint 1: You need not know or use anything about the value of pixels in this image.
Hint 2: All you have to do is get a subset of a matrix in MATLAB (the matrix, in this case, represents an image).
The problem generates all the input values, so what is the reason I get a "Out of memory. The likely cause is an infinite recursion within the program." error when I simply input:
Icrop = crop_image(I, Rmin, Cmin, Rpix, Cpix)
I suggest to improve problem's statement.
I found I had to check my indices and subtract one.
I saw that image cropping function is frequently used in digital image processing
yea improve the statement
this is good concept for image processing
Difficult problem!
It took me time to do this
Very good conceptual problem about image.
(Rmin,Cmin) is the (x,y) coordinate of the image's lefttop point, and Rpix*Cpix is the size of the rectangle image.
interesting problem
Good problem
Why doesn't this work?
Icrop = I(Rmin:Rmin+Rpix, Cmin:Cmin+Cpix)
I am a beginner, but this seems correct if I'm understanding the problem statement. Maybe I'm not..
@Jeremy, your approach is correct. but your formula needs a slight change. Try your formula on the given example and you'll see
I really don't get neither the example given nor the question asked. Could someone explain it?
Rmin  start keeping image from row
Cmin  start keeping image from column
Rpix  the number of rows to keep starting from Rmin
Cpix  the number of columns to keep starting from Cmin
Solution Comments

2 Comments
one liner
Agree

1 Comment
Took a second to understand the question. Good for image processing

1 Comment
Hello, how did you solve this.Im stuck.Please help

2 Comments
Could you please help me out with the solution ? I am stuck
In the given e.g., Rmin = 2, Cmin = 2; means we have to keep 2nd row element of 2nd column. Now Rpix = 1, Cpix = 3; indicates that we have to keep 1 pixel of row & 3 pixels of column from & including I(Rmin,Cmin) element.
Note: each element in the matrix indicates a pixel.

3 Comments
how did you do it?
@Ryan Charlinski Think of it more like subsetting a matrix given these values and less like cropping an image. The indices for them start at Rmin and Cmin and go up to Rpix and Cpix. Although, careful for MATLAB indexing starting at 1!
Is there anyone that could eventually help me ? Thanks in advance
Problem Recent Solvers5392
Suggested Problems

Numbers with prime factors 2, 3 and 5.
415 Solvers

We love vectorized solutions. Problem 1 : remove the row average.
761 Solvers

10601 Solvers

convert matrix to single column
377 Solvers

Mersenne Primes vs. All Primes
368 Solvers
More from this Author13
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!