Cropped image shown in black and white?

3 views (last 30 days)
Rachel
Rachel on 8 May 2013
Doing a project for matlab, part of which asks me to crop an image using ginput. I managed to figure out how to use the points from ginput to edit the matrix of my image, but when i used imshow my picture is only in black and white. Not really sure why this is happening...Sometimes,depending on the image I use, I get this error: "Warning: Integer operands are required for colon operator when used as index". Below is the part of the code that is used for cropping:
clear
clc
picture=input('What picture would you like to edit? \' )
image=imread(picture);
task= menu('What would you like to do?','Crop','Scale','Rotate','Apply blur','Apply filter');
switch task
case 1
disp('Please select cropping area.')
imshow(picture)
[x1,y1]=ginput
[x2,y2]=ginput
cropped=image((y1:y2),(x1:x2));
imshow(cropped)
saveas(gcf,'cropped.jpg')
end

Answers (1)

Image Analyst
Image Analyst on 8 May 2013
Edited: Image Analyst on 8 May 2013
You should be using imcrop() or rbbox() instead of ginput, and you should be using uigetfile() instead of input(), but anyway.....
Your x1,x2,y1,y2 are floating point values, not integers, so cast them to integers. Also you need to index "picture", which is your image array, not "image" which is a built-in function to display images.
picture = imread(filename);
...
cropped = picture(int32(y1:y2), int32(x1:x2));
  2 Comments
Rachel
Rachel on 8 May 2013
My professor specifically told us to use the ginput, so I didn't have a choice for that. I tried your suggestion, and matlab returned the error: Index exceeds matrix dimensions.
Error in final_project_image_edit (line 12) cropped = picture(int32(y1:y2), int32(x1:x2));
Then I tried using image instead of picture, and it gave me a black and white image again. Could you explain what you mean by "cast to integers"? I'm not familiar with this. Thanks!
Image Analyst
Image Analyst on 8 May 2013
Number 1, do NOT do this:
image=imread(picture);
image is a built-in function and you must not destroy it by doing that. You can do imshow(picture) because picture is a string, though the more typical way would be to send the output of imread() into imshow().
#2, when you use ginput, you get floating point numbers, not integers. Just take the semicolon off the end of the line to have it print them to the command window to see for yourself. Let's say y1 was 134.567. Now the image will have a 134th line, and it will have a 135th line, but it does not have a 134.567th line. You have to cast it to an integer - either 134 or 135. You can do this with the function int32() which will round to the nearest integer and give you an actual integer value out. Or you can use round(), ceil(), or floor(), which will make it an integer in value, but leave it as a double (just with no fractional part). Type "whos y1" to see.
#3, if imshow(picture) worked, then imshow(cropped) shoudl also work because cropped will be of the same data type, for example uint8. Do this
whos cropped
and see if it says it's a uint8 variable. If it's a double for some weird reason, then use [] in imshow:
imshow(cropped, []);

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!