How to find the value of the (maximum, minimum, average) intensity in the image on MATLAB?

12 visualizaciones (últimos 30 días)
How to find the value of the (maximum, minimum, average) intensity in the image on MATLAB? solve all please
% Load the image
im = imread('peppers.png');
% convert to grayscale
gray_image = rgb2gray(im);
% display the rgb and grayscale image
figure; subplot(1,2,1); imshow(im);
subplot(1,2,2); imshow(gray_image);
[row, col] = size(gray_image);
% You should see the value for row is 384
% You should see the value for col is 512
disp(row);
disp(col);
%% Your work starts
% Q1
% Locate the position and find the value
% of the pixel with maximum intensity
% You need to change __ to your code
max_value = max(im(:));
max_row_index = max ____ ;
max_col_index = ____;
% Use nested for loop
____
% Q2
% Locate the position and find the value
% of the pixel with minimum intensity
min_value = ____ ;
min_row_index = ____ ;
min_col_index = ____ ;
% Use nested for loop
____
% Q3
% Find the average intensity
all_values = ____ ;
average_intensity = ____ ;
% Use nested for loop
____
% Q4
% Swap the maximum intensity and the minimum intensity
____
% display the images
  3 comentarios
Destiny
Destiny el 14 de Oct. de 2024
Editada: Destiny el 14 de Oct. de 2024
I also have this assignment. It's for a gen ed course for non-computer science majors and we're all confused. I believe we're supposed to use loops but unfortionately we weren't even taught the syntax. It's like asking us to say something in another spoken lanuage without giving us the vocabulary. Is there a way to do this in a loop even if it's lenghty? We were given an example code but it doens't something different than finding the max, min, adn average.
Example code here:
% % This is example code we went through during the discussion section
% % How for loop works
% https://www.mathworks.com/help/matlab/ref/for.html
% Create it image
it_image = zeros(8, 8);
row_idx = [2, 2, 3, 3, 4, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 7, 7];
col_idx = [2, 5, 2, 5, 2, 4, 5, 6, 7, 2, 5, 2, 5, 2, 5, 6, 7];
for i=1:length(row_idx)
i_idx = row_idx(i);
j_idx = col_idx(i);
it_image(i_idx, j_idx) = 255;
end
% display it image
imshow(it_image);
% swap the background and foreground
it_image = imcomplement(it_image);
imshow(it_image);
% create mirror image 1
it_image_mirror_1 = zeros(8, 8);
% nested for loop
for i=1:8
for j=1:8
it_image_value = it_image(i, j);
it_image_mirror_1(i, 9-j) = it_image_value;
end
end
% display mirror image 1
imshow(it_image_mirror_1);
% create mirror image 2
it_image_mirror_2 = zeros(8, 8);
% nested for loop
for i=1:8
for j=1:8
it_image_value = it_image(i, j);
it_image_mirror_2(9-i, j) = it_image_value;
end
end
% display mirror image 2
imshow(it_image_mirror_2);
% create mirror image 3
it_image_mirror_3 = zeros(8, 8);
% nested for loop
for i=1:8
for j=1:8
it_image_value = it_image(i, j);
it_image_mirror_3(9-i, 9-j) = it_image_value;
end
end
% display mirror image 3
imshow(it_image_mirror_3);
% display all four images together
figure; hold on;
subplot(2, 2, 1); imshow(it_image);
subplot(2, 2, 2); imshow(it_image_mirror_1);
subplot(2, 2, 3); imshow(it_image_mirror_2);
subplot(2, 2, 4); imshow(it_image_mirror_3);
% % How if works
% https://www.mathworks.com/help/matlab/ref/if.html
% If end
if 6 > 5
disp("Hello");
end
% If else end
if 6 < 5
disp("Hello");
else
disp("World");
end
DGM
DGM el 15 de Oct. de 2024
I still think the assignment template is baloney.
This is a start comparing finding min and mean with or without loops. If we need to know the location of the (first) minimum/maximum, there are different ways to either obtain that location expressed as either a linear index or as row/column subscripts. Choose what you feel best suits your interpretation of the assignment, and expand it from there.
% this is a single-channel uint8 image
inpict = imread('cameraman.tif');
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate image stats without loops
% i'm only doing min and mean
[mn0 idxmn0] = min(inpict(:)); % still uint8
av0 = mean(inpict(:)); % uint8-scale float
% what if you wanted the row/col subscripts of the min value
% instead of the linear index?
[rowmn0 colmn0] = ind2sub(size(inpict),idxmn0);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% alternatively, you could use a single loop
% both of these outputs are uint8-scale float
mn1 = Inf;
idxmn1 = 1;
av1 = 0;
for k = 1:numel(inpict)
% look for first minimum
if inpict(k) < mn1
mn1 = inpict(k);
idxmn1 = k;
end
% accumulate for mean
av1 = av1 + double(inpict(k));
end
av1 = av1/numel(inpict); % mean from sum
% same as before
[rowmn1 colmn1] = ind2sub(size(inpict),idxmn1);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% alternatively, you could use two loops
% to address the array using subscripts
mn2 = Inf;
rowmn2 = 1;
colmn2 = 1;
av2 = 0;
for c = 1:size(inpict,2)
for r = 1:size(inpict,1)
% same concept as before
if inpict(r,c) < mn2
mn2 = inpict(r,c);
rowmn2 = r;
colmn2 = c;
end
% same as before
av2 = av2 + double(inpict(r,c));
end
end
av2 = av2/numel(inpict); % same as before
Given your example, I see two interpretations to the final question. "Swap the maximum intensity and the minimum intensity". I gave one example for the literal interpretation of swapping the array extrema, but maybe that's not intended.
Alternatively, if they wanted you to do something akin to imcomplement(), that's a different story. For the simple case of a uint8 image, it might simplify to merely
% the naive assumption is a great way to cause problems
outpict = 255 - inpict; % flip 0 and 255
... but not all images are uint8. The scale of values in an image is expected to correspond to its numeric class, so invert the image based on its class. You could conditionally check the class and do a similar linear transformation of values arithmetically, but this is the simpler way.
% invert an image by swapping the nominal values
% associated with black and white in the given array class.
% e.g. [0 1] for float classes
% [0 255] for uint8
% [0 65535] for uint16
% [-32768 32767] for int16
% and so on.
if isnumeric(inpict)
inclass = class(inpict);
switch inclass(1)
case {'u','i'}
outpict = bitcmp(inpict); % this works for all integer classes
otherwise
outpict = 1 - inpict; % this works for unit-scale float
end
elseif islogical(inpict)
outpict = ~inpict; % this works for logical class
else
error('expected INPICT to be numeric or logical')
end
It's important then, that images are properly-scaled for their class. Just as it's problematic to universally presume the wrong pivot values, if we're assuming pivot values based on the numeric class, then the scale of the image values needs to match the class.
In fact, the example you were given includes an utterly common error which creates an improperly-scaled image for which imcomplement() then silently fails.
it_image = zeros(8, 8); % this is a float-class (should be unit-scale)
row_idx = [2, 2, 3, 3, 4, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 7, 7];
col_idx = [2, 5, 2, 5, 2, 4, 5, 6, 7, 2, 5, 2, 5, 2, 5, 6, 7];
for i=1:length(row_idx)
i_idx = row_idx(i);
j_idx = col_idx(i);
it_image(i_idx, j_idx) = 255; % but we're shoving uint8-scale values into it
end
% this should either be uint-scale (0 to 1) float
% or uint8-scale (0 to 255) uint8
% instead it's uint8-scale float
it_image
it_image = 8×8
0 0 0 0 0 0 0 0 0 255 0 0 255 0 0 0 0 255 0 0 255 0 0 0 0 255 0 255 255 255 255 0 0 255 0 0 255 0 0 0 0 255 0 0 255 0 0 0 0 255 0 0 255 255 255 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% so when we invert it, the values are messed up even more
% imshow simply truncates the negative values, so you don't notice it
it_image = imcomplement(it_image)
it_image = 8×8
1 1 1 1 1 1 1 1 1 -254 1 1 -254 1 1 1 1 -254 1 1 -254 1 1 1 1 -254 1 -254 -254 -254 -254 1 1 -254 1 1 -254 1 1 1 1 -254 1 1 -254 1 1 1 1 -254 1 1 -254 -254 -254 1 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
... but maybe that's getting off-topic.

Iniciar sesión para comentar.

Respuestas (1)

Zinea
Zinea el 7 de Oct. de 2024
Hi Ahmed,
This is a homework question and hence I won't be providing the exact answers. Below are some hints to help you reach the correct solution:
Q1: A function that finds the maximum value in an array is to be used. Then, a function that returns indices of elements that meet a condition is to be used. Finally, convert linear indices to row and column indices.
Q2: The same steps are to be followed as in Q1, but for the minimum value.
Q3: A function that calculates the mean of an array is to be used.
Q4: The indices obtained from Q1 and Q2 are to be used to exchange the values at those positions in the image array.
Happy learning!

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by