image grayscale and distortion
Afficher commentaires plus anciens
function converted = convertImage(im, alterationType)
% Main function to alter the image based on alterationType
% Parameters:
% - im: the input image array
% - alterationType: numerical value (1, 2, or 3) indicating the type of alteration
% Display original image
figure;
subplot(1, 2, 1);
imshow(im);
title('Original Image');
% Call the appropriate helper function based on alterationType
switch alterationType
case 1
% Blurring image alteration
converted = blurImage(im);
title('Blurred Image');
case 2
% Distorting image alteration
converted = distortImage(im);
title('Distorted Image');
case 3
% Grayscale image alteration
converted = grayscaleImage(im);
title('Grayscale Image');
otherwise
% No alteration for unknown alterationType
converted = im;
title('No Alteration');
end
% Display altered image
subplot(2, 2, 1);
imshow(converted);
end
function blurred = blurImage(im)
% Helper function to blur the image
[rows, cols, ~] = size(im);
blurred = im;
for i = 2:rows-1
for j = 2:cols-1
% Calculate local mean for each pixel
localMean = mean(mean(im(i-1:i+1, j-1:j+1, :)));
blurred(i, j, :) = localMean;
end
end
end
function distorted = distortImage(im)
% Helper function to distort the image
[rows, cols, channels] = size(im);
halfwayRows = rows / 2;
halfwayCols = cols / 2;
distorted = im;
if channels == 3
% For color images
topRight = im(1:halfwayRows, halfwayCols+1:end, :);
topLeft = im(1:halfwayRows, 1:halfwayCols, :);
bottomRight = im(halfwayRows+1:end, halfwayCols+1:end, :);
bottomLeft = im(halfwayRows+1:end, 1:halfwayCols, :);
distorted(1:halfwayRows, 1:halfwayCols, :) = bottomRight;
distorted(1:halfwayRows, halfwayCols+1:end, :) = topLeft;
distorted(halfwayRows+1:end, 1:halfwayCols, :) = topRight;
distorted(halfwayRows+1:end, halfwayCols+1:end, :) = bottomLeft;
else
% For grayscale images
topRight = im(1:halfwayRows, halfwayCols+1:end);
topLeft = im(1:halfwayRows, 1:halfwayCols);
bottomRight = im(halfwayRows+1:end, halfwayCols+1:end);
bottomLeft = im(halfwayRows+1:end, 1:halfwayCols);
% Swap quadrants for grayscale values
distorted(1:halfwayRows, 1:halfwayCols) = bottomRight;
distorted(1:halfwayRows, halfwayCols+1:end) = bottomLeft;
distorted(halfwayRows+1:end, 1:halfwayCols) = topRight;
distorted(halfwayRows+1:end, halfwayCols+1:end) = topLeft;
end
end
function grayscale = grayscaleImage(im)
% Helper function to convert the image to grayscale
[rows, cols, channels] = size(im);
grayscale = im;
for i = 1:rows
for j = 1:cols
% Calculate grayscale value using the formula
grayscale(i, j, :) = 1.25 * mean(im(i, j, :));
end
end
% Eliminate the third dimension for color images
if channels == 3
grayscale = squeeze(grayscale(:, :, 1));
end
end
Alter Grayscale Image: convertImage(im, 2) - Expected to distort the image.
Variable studentResult has an incorrect value.
When calling the required convertImage function with the option for distorting the grayscale image, the output from convertImage was not an image array containing a properly distorted grayscale image.
it fails this one test out of 9
please help
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Image Quality dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!