Ask user for another image or abort

1 Ansicht (letzte 30 Tage)
Ally Kassam
Ally Kassam am 24 Okt. 2019
Kommentiert: Ally Kassam am 25 Okt. 2019
% What I would like the program to do is, to ask the user if they want to select another image to process. How do I do that?
clear all
[file,path] = uigetfile('*.jpg')
rgbImage = imread (strcat(path, file));
[rows, columns, numberOfColorChannels] = size(rgbImage); % Get the dimensions of the image.
subplot(2, 2, 1);% Display the original color image on the first box
imshow(rgbImage);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1); % arrayName(rowIndex, columnIndex, colorChannelIndex). Colon : means "all".
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Find white, where each color channel is more than , say, 200
thresholdValue = 200;
binaryImage = redChannel > thresholdValue & greenChannel > thresholdValue & blueChannel > thresholdValue;
% Display the binary image.
subplot(2, 2, 2); % display the binary image on the 2nd plot
imshow(binaryImage);
% Determine the areas on this first pass.
numRegions = 0;
[labeledImage, numRegions] = bwlabel(binaryImage); % returns the number of distinct regions that make up the shape
props = regionprops(labeledImage, 'Area'); % returns measurements for the set of properties specified by 'Area'
allAreas = sort([props.Area]); % returns a collection of index vectors
% Get rid of small blobs - blobs smaller than 500 pixels to see the tumor
% more clearly
binaryImage = bwareaopen(binaryImage, 500);
% Display the binary image.
subplot(2, 2, 3); % plots the image with lesser blobs on 3rd plot
imshow(binaryImage);
% Now crop out the blob - the one in the lower left.
[labeledImage, numRegions] = bwlabel(binaryImage);
% Extract blob
binaryImage = ismember(labeledImage, 1);
[labeledImage, numRegions] = bwlabel(binaryImage);
props = regionprops(labeledImage, 'BoundingBox');
subImage = imcrop(rgbImage, props.BoundingBox);
% Display the cropped image.
subplot(2, 2, 4); % plot the final cropped image in the original way showing the tumor
imshow(subImage);

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Okt. 2019
Delete the "clear all" -- you should never include "clear all" as part of a program that does computation. (But you could have a script that is intended to just reset your MATLAB session that it would be valid to have "clear all" as part of.)
After that, put
while true
at the top. Then at the bottom add
user_choice = menu('Do you want to process another image?', 'Yes', 'No');
if user_choice ~= 1; break; end
end

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by