image grayscale and distortion

4 ビュー (過去 30 日間)
ronit
ronit 2023 年 11 月 21 日
編集済み: Voss 2023 年 11 月 21 日
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

採用された回答

Voss
Voss 2023 年 11 月 21 日
編集済み: Voss 2023 年 11 月 21 日
In the distortImage function, notice that the code for color images:
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; % <-
has topLeft and bottomLeft swapped relative to the code for grayscale images:
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; % <-
Since the error references grayscale images, I expect that the color block is OK and the grayscale block needs to be changed accordingly. (This interpretation is consistent with the instructions for this project.)
However, also realize that you don't need the grayscale block at all, because the colon used to index along the third dimension of a color image array works just as well for a grayscale image matrix, so all you really need to do is to remove the grayscale block:
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
% For ALL 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
  3 件のコメント
ronit
ronit 2023 年 11 月 21 日
移動済み: Voss 2023 年 11 月 21 日
Thankyou so much its done
Voss
Voss 2023 年 11 月 21 日
編集済み: Voss 2023 年 11 月 21 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by