How can I get the matrix of the grayscale image?

3 views (last 30 days)
The code that I've written is as follows:
i = imread('Image_Name.bmp');
imshow(i);
input = i; % To store values of 'i' in 'input'
inmin = min(i); % To store values of 'min(i)' in 'inmin'.
inmax = max(i); % To store values of 'max(i)' in 'inmax'.
All of the above is in Command Window.
And the following is in Workspace Window.
i = 500x500x3 unit8
inmax = 1x500x3 unit8
inmin = 1x500x3 unit8
input = 500x500x3 unit8
j = 1
k = 500
zeros 1x500double
In the Editor Window.
k = 500;
zeros = (1:500);
for b = 1:k
for j = 1:k
disp (input(k,k)-inmin(b,j)) % I've taken 'input(k,k)' as its dimension is 500x500x3
end
end
It gives me some values and then showcases the error "Index exceeds matrix dimensions. Error in disp (input(k,k)-inmin(b,j))" Please help me. I'm new to MATLAB so I'm having difficulty with it.
  2 Comments
Florian Morsch
Florian Morsch on 4 Jul 2018
Could you try k=499 instead of 500 and see if you get a error again?

Sign in to comment.

Answers (2)

KSSV
KSSV on 4 Jul 2018
You should use the following to get the min and max:
inmin = min(i,[],3); % To store values of 'min(i)' in 'inmin'.
inmax = max(i,[],3); % To store values of 'max(i)' in 'inmax'.
  4 Comments
Rhea S Shrivastava
Rhea S Shrivastava on 4 Jul 2018
When I do it that way, all i get is the array of zeros. I need to apply this formula on each pixel. Hence, I need to use the for loop, but I'm stuck with its initialization.
Image Analyst
Image Analyst on 4 Jul 2018
Edited: Image Analyst on 4 Jul 2018
You need to cast to single or double. If you subtract a value from a uint8 or uint16 image and the result is less than 0, all you will see is zero. And don't use input as the name of your variable since it's the name of a built-in function.

Sign in to comment.


Image Analyst
Image Analyst on 4 Jul 2018
Lots wrong with that code. Just try this:
rgbImage = imread('peppers.png');
[rows, columns, numberOfColorChannels] = size(rgbImage)
imshow(rgbImage);
drawnow;
% Extract the individual red, green, and blue color channels.
redChannel = double(rgbImage(:, :, 1));
greenChannel = double(rgbImage(:, :, 2));
blueChannel = double(rgbImage(:, :, 3));
minR = min(redChannel(:))
minG = min(greenChannel(:))
minB = min(blueChannel(:))
maxR = max(redChannel(:))
maxG = max(greenChannel(:))
maxB = max(blueChannel(:))
for col = 1 : columns
for row = 1 : rows
message = sprintf('For row=%d, col=%d, diff = %.1f in the red, %.1f in the green, and %.1f in the blue',...
row, col,...
redChannel(row, col) - minR, ...
greenChannel(row, col) - minG, ...
blueChannel(row, col) - minB);
fprintf('%s\n', message);
end
end

Community Treasure Hunt

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

Start Hunting!