필터 지우기
필터 지우기

image grayscale and distortion

조회 수: 7 (최근 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개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by