フィルターのクリア

Image reduction issues/problems?

1 回表示 (過去 30 日間)
andrew_cup
andrew_cup 2012 年 11 月 20 日
Hi. My program is coded so if the height or width of an image is larger than 400 pixels it reduces the width and height with a reduction factor 0.5. I have a menu that also asks the user to choose 'yes' or 'no' wether he want to make the image into a shape of a square (all sides have equal lengths). The problem is I can't figure out how to display and use the normal image after when the user pressed 'no'. The image is converted to black and white and then the user is asked to input a radius for a circular frame to be on top of the image.
im = imread('michelle_pf.jpeg');
reductionFactor = 0.5;
[h,w,kanaler] = size(im);
if h>400 || w > 400
im_r = imresize(im, reductionFactor);
end
m = menu('Do you want the image be in a shape of a square?','yes','no');
if m == 1
[h,w,kanaler] = size(im);
im_k = im(1:min(h,w),1:min(h,w),:);
im_g = rgb2gray(im_k);
im_d = im2double(im_g);
rgbImage = im_d;
elseif m==2
rbgImage = im_r;
end
[rows columns numberOfColorChannels] = size(rgbImage);
[columnsInImage rowsInImage] = meshgrid(1:columns, 1:rows);
centerX = columns/2;
centerY = rows/2;
radius = input('What should the radius of the circular frame be?');
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
maskedRgbImage = bsxfun(@times, rgbImage, cast(circlePixels, class(rgbImage)));
if radius <= min([rows columns]) / 2
figure(1),clf
imshow(im)
figure(2),clf
imshow(im_k)
figure(3),clf
imshow(im_d);
figure(4),clf
imshow(maskedRgbImage);
else
disp('NB! Radius have to be smaller or equal to the pictures dimensions');
k = menu('NB! Radius have to be smaller or equal to the pictures dimensions. Try again?','Yes,'No');
if k == 1
filename.m
elseif k== 2
return;
end
end

採用された回答

Image Analyst
Image Analyst 2012 年 11 月 20 日
Try it this way:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
rgbImage = imread('peppers.png');
reductionFactor = 0.5;
[rows columns numberOfColorChannels] = size(rgbImage);
if rows > 400 || columns > 400
rgbImage = imresize(rgbImage, reductionFactor);
end
subplot(2,2,1);
imshow(rgbImage);
title('Original image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
m = menu('Do you want the image be in a shape of a square?','yes','no');
[rows columns numberOfColorChannels] = size(rgbImage);
squareWidth = min([rows columns]);
if m == 1
rgbImage = imresize(rgbImage, [squareWidth, squareWidth]);
subplot(2,2,2);
imshow(rgbImage);
title('Square image', 'FontSize', fontSize);
end
[rows columns numberOfColorChannels] = size(rgbImage);
[columnsInImage rowsInImage] = meshgrid(1:columns, 1:rows);
centerX = columns/2;
centerY = rows/2;
% Ask user for a number.
defaultValue = 45;
titleBar = 'Enter a value';
userPrompt = sprintf('What should the radius of the circular frame be (< %d)? ', squareWidth);
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
radius = str2double(cell2mat(caUserInput));
% Check for a valid integer.
if isnan(radius)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
radius = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', radius);
uiwait(warndlg(message));
end
if radius > min([rows columns]) / 2
uiwait(msgbox('NB! Radius have to be smaller or equal to the pictures dimensions'));
k = menu('NB! Radius have to be smaller or equal to the pictures dimensions. Try again?','Yes','No');
radius = min([rows columns]) / 2;
end
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
maskedRgbImage = bsxfun(@times, rgbImage, cast(circlePixels, class(rgbImage)));
subplot(2, 2, 3);
imshow(maskedRgbImage);
title('Circularly masked image', 'FontSize', fontSize);

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by