Matlab Image Cropping problem

Hello Everyone,
I have some issue when I'm trying to crop an image.
I have chosen a RGB Image(256x256) and converted it into grayscale image, then I check out the image pixel values. I have attached here only 8x8 dimension pixel values (shown in fig.1)
from the entire 256x256 pixel workspace values.
But when I crop that image using imcrop() command, then I get also that 8x8 pixel but with different values (shown in fig.2).
fig2.png
This creates the problem I get the different pixel values from same image when trying to crop that image with a specified dimension and also get differnt pixel values when stored it into a hex file for furthur processing.
Below is my Matlab code:
a =imread('cameraman.tif');
a1=rgb2gray(a);
rect=[1 7 7 7];
i_c = imcrop(a1,rect);
pixel = cell(8,8);
ctr = 1;
ctc = 1;
for R=1:8
for C=1:8
pixel{ctr,ctc}=dec2hex(i_c(R,C));
ctc = ctc+1;
end
ctr = ctr+1;
ctc=1;
end
fid = fopen('cam_data_tif_8.hex', 'wt');
fprintf(fid, '%x\n', i_c);
disp('Text file write done');disp(' ');
fclose(fid);
celldisp(pixel)
disp ([pixel{:}]);
Please reply to me.Thank you.

 Accepted Answer

Attach your cameraman.tif. Evidently you changed the one that ships with MATLAB because that one is not a color image and your code throws an error.
This is what I did to make it work with the official cameraman photo, and it works fine:
a = imread('cameraman.tif');
if ndims(a) >= 3
a1=rgb2gray(a);
else
a1 = a;
end
rect=[1 7 7 7];
i_c = imcrop(a1,rect)
pixel = cell(8,8);
ctr = 1;
ctc = 1;
for R=1:8
for C=1:8
pixel{ctr,ctc}=dec2hex(i_c(R,C));
ctc = ctc+1;
end
ctr = ctr+1;
ctc=1;
end
fid = fopen('cam_data_tif_8.hex', 'wt');
fprintf(fid, '%x\n', i_c);
disp('Text file write done');disp(' ');
fclose(fid);
celldisp(pixel)
disp ([pixel{:}]);
Keep in mind that you are cropping starting at row 7.

4 Comments

Thanks for answering me.I have attach cameraman.tif photo also in zip format..please extract it.
But Issue still not solve. I have attached more two images which i have taken after run your code.fig3. is code and gives an warning and fig 4. is that what problem I actually facing.
Sandip, your fig3 is not an error. It's not even a warning. It's just a suggestion, and not even an appropriate one at that because the interpreter didn't understand the code. You can safely ignore it.
Your fig 4 is pointing to the wrong place on the right hand side. The arrow should be pointing to line 7, column 1, NOT line 1, column 1. Look at your rect -- it's [1, 7, 7, 7] which means xLeft = 1, and yTop = 7, so you should be looking at row 7 of the original image, which goes to row 1 of the cropped image. You can see that they're both 156 and the code is correctly giving you what you ask for. If you want it to start at row 1 you need rect(2) to be 1, not 7.
The format for rectangles in MATLAB is [x, y, width, height], or using row and column its [column, row, width, height]. Sometimes I just go super explicit and combine them to say [xLeftColumn, yTopRow, width, height].
thank you very much

Sign in to comment.

More Answers (1)

Sandip Paul
Sandip Paul on 19 Jan 2020
ok.I understand it.Actually I cann't find the actual syntax of this rect=[1 7 7 7]. I put the values arbitarily.means I want to know that which position defines xleft/yTop etc. of rect.can you please define the actual syntax behind this rect [ ].
thank you for your previous suggestions.Reply me with the solution which I mentioned above.

1 Comment

Problem Solved.I change rect =[1 1 7 7].Now I get the value which I want. Thank you very much.
But you please send me the syntax of " rect= [ ] " command.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!