How to find the area of an image and display the output in the GUI?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi,
I have created a GUI using the APP DESIGNER in Matlab. When I click the AREA button in the GUI, a value is displayed in the field beside the AREA button. However, problem arises when I click the OPEN button in the GUI. A new image is displayed in the Image Panel but the old value of the Area remains in the GUI. I have to manually change the filename in the code in order for the actual value to be displayed on the GUI. The other buttons work perfectly i.e. when I open a new file, all the previous values are replaced automatically with the new values. Only the AREA button is not working as desired. Please note that the AREA value would be that of the area of the Image on the right side of the Image Panel(app.Image_2.ImageSource or the image file that is generated after the ProcessButtonPushed function is performed.) Any suggestions would be very much appreciated.
classdef Patient5 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
MiddleEarPanel matlab.ui.container.Panel
ImagePanel matlab.ui.container.Panel
Image_2 matlab.ui.control.Image
Image matlab.ui.control.Image
Image2 matlab.ui.control.Image
InfoPanel matlab.ui.container.Panel
FilenameEditFieldLabel matlab.ui.control.Label
FilenameEditField matlab.ui.control.EditField
OptionPanel matlab.ui.container.Panel
OpenButton matlab.ui.control.Button
ProcessButton matlab.ui.control.Button
IntensityProfileButton matlab.ui.control.Button
AreaButton matlab.ui.control.Button
AverageDensityButton matlab.ui.control.Button
StandardDeviationButton matlab.ui.control.Button
Label_2 matlab.ui.control.Label
EditField_5 matlab.ui.control.EditField
RedEditFieldLabel matlab.ui.control.Label
RedEditField matlab.ui.control.EditField
GreenEditFieldLabel matlab.ui.control.Label
GreenEditField matlab.ui.control.EditField
BlueEditFieldLabel matlab.ui.control.Label
BlueEditField matlab.ui.control.EditField
RedStdEditFieldLabel matlab.ui.control.Label
RedStdEditField matlab.ui.control.EditField
GreenStdEditFieldLabel matlab.ui.control.Label
GreenStdEditField matlab.ui.control.EditField
BlueStdEditFieldLabel matlab.ui.control.Label
BlueStdEditField matlab.ui.control.EditField
end
properties (Access = public)
UIAxes matlab.ui.control.UIAxes
UIaxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Callback function: FilenameEditField, OpenButton
function OpenButtonPushed(app, event)
[filename,filepath] = uigetfile({'*.*;*.jpg;*.png;*.bmp;*.oct'}, 'Select File to Open');
fullname = [filepath, filename];
app.FilenameEditField.Value = "filename";
app.Image.ImageSource = fullname;
app.Image_2.ImageSource = "";
app.EditField_5.Value = "";
app.RedStdEditField.Value = "";
app.GreenStdEditField.Value = "";
app.BlueStdEditField.Value = "";
app.RedEditField.Value = "";
app.GreenEditField.Value = "";
app.BlueEditField.Value = "";
app.FilenameEditField.Value = "";
end
% Button pushed function: IntensityProfileButton
function IntensityProfileButtonPushed(app, event)
I = imread(app.Image.ImageSource);
x=[size(I,2)/2 size(I,2)/2];
y=[0 size(I,1)];
c = improfile(I,x,y);
figure
subplot(2,1,1)
imshow(I)
hold on
plot(x,y,'r')
subplot(2,1,2)
plot(c(:,1,1),'r')
hold on
plot(c(:,1,2),'g')
plot(c(:,1,3),'b')
end
% Image clicked function: Image_2
function Image_2Clicked(app, event)
end
% Button pushed function: ProcessButton
function ProcessButtonPushed(app, event)
img = imread(app.Image.ImageSource);
% get red, green, blue channels
redC = img(:,:,1);
greenC = img(:,:,2);
blueC = img(:,:,3);
% mask where red channel is greater than blue channel and green channel greater than blue channel
mask = redC > blueC & greenC > blueC;
% overlay mask into original image
finalimg = bsxfun(@times, img, cast(mask,class(img)));
app.Image_2.ImageSource = finalimg;
end
% Button pushed function: StandardDeviationButton
function StandardDeviationButtonPushed(app, event)
img = imread(app.Image.ImageSource);
% get red, green, blue channels
redC = img(:,:,1);
greenC = img(:,:,2);
blueC = img(:,:,3);
% % mask where red channel is greater than blue channel and green channel greater than blue channel
mask = redC > blueC & greenC > blueC;
D = std2(redC(mask));
E = std2(greenC(mask));
F = std2(blueC(mask));
s = num2str(D);
t = num2str(E);
u = num2str(F);
app.RedStdEditField.Value = s;
app.GreenStdEditField.Value = t;
app.BlueStdEditField.Value = u;
% % overlay mask into original image
%finalimg = bsxfun(@times, img, cast(mask,class(img)));
% %plotting
% figure
% subplot(1,2,1)
% imshow(img)
% subplot(1,2,2)
% imshow(finalimg)
end
% Callback function: AreaButton, FilenameEditField, Image
function AreaButtonPushed(app, event)
%
%===============================================================================
% Read in a standard MATLAB gray scale demo image.
% folder = 'C:\Users\morteza\Downloads\Matlab\Matlab';
% baseFileName = 'finalimg.jpg';
% % Get the full filename, with path prepended.
% fullFileName = fullfile(folder, baseFileName);
% % Check if file exists.
% if ~exist(fullFileName, 'file
% % File doesn't exist -- didn't find it there. Check the search path for it.
% fullFileNameOnSearchPath = baseFileName; % No path this time.
% if ~exist(fullFileNameOnSearchPath, 'file')
% % Still didn't find it. Alert user.
% errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
% uiwait(warndlg(errorMessage));
% return;
% end
% end
M = imread(app.Image_2.ImageSource);
grayImage = imread(M);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
% subplot(2, 2, 1);
% imshow(grayImage, []);
% axis on;
% title('Original Grayscale Image', 'FontSize', fontSize);
% % Enlarge figure to full screen.
% set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% % Give a name to the title bar.
% set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% % Let's compute and display he histogram.
% [pixelCount, grayLevels] = imhist(grayImage);
% subplot(2, 2, 2);
% bar(grayLevels, pixelCount);
% grid on;
% title('Histogram of original image', 'FontSize', fontSize);
% xlim([0 grayLevels(end)]); % Scale x axis manually.
binaryImage = grayImage~= 40;
% % Display the binary image.
% subplot(2, 2, 3);
% imshow(binaryImage, []);
% axis on;
% title('Binary Image', 'FontSize', fontSize);
%
% % Fill the binary image.
% binaryImage = imfill(binaryImage, 'holes');
% % Display the binary image.
% subplot(2, 2, 4);
% imshow(binaryImage, []);
% axis on;
% title('Filled Binary Image', 'FontSize', fontSize);
%
% Calculate the area using bwarea().
area1 = bwarea(binaryImage);
% Calculate the area in pixels using sum()
area2 = sum(binaryImage(:));
% message = sprintf('The area = %.1f, or %d\ndepending on how you want to calculate it', area1, area2);
% uiwait(helpdlg(message));
answer = num2str(area2);
% Value = area2;
app.EditField_5.Value = answer;
% app.EditField_4ValueChanged.Value = num2str(Value);
end
% Callback function
function EditField_4ValueChanged(app, event)
% clc; % Clear the command window.
% close all; % Close all figures (except those of imtool.)
% imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
% clear; % Erase all existing variables. Or clearvars if you want.
% workspace; % Make sure the workspace panel is showing.
% format long g;
% format compact;
% fontSize = 22;
%
% % Check that user has the Image Processing Toolbox installed.
% hasIPT = license('test', 'image_toolbox');
% if ~hasIPT
% % User does not have the toolbox installed.
% message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
% reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
% if strcmpi(reply, 'No')
% % User said No, so exit.
% return;
% end
% end
%===============================================================================
% Read in a standard MATLAB gray scale demo image.
% folder = 'C:\Users\morteza\Downloads\Matlab\Matlab';
% baseFileName = 'M1.jpg';
% % Get the full filename, with path prepended.
% fullFileName = fullfile(folder, baseFileName);
% % Check if file exists.
% if ~exist(fullFileName, 'file')
% % File doesn't exist -- didn't find it there. Check the search path for it.
% fullFileNameOnSearchPath = baseFileName; % No path this time.
% if ~exist(fullFileNameOnSearchPath, 'file')
% % Still didn't find it. Alert user.
% errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
% uiwait(warndlg(errorMessage));
% return;
% end
% end
% grayImage = imread(fullFileName);
% % Get the dimensions of the image.
% % numberOfColorBands should be = 1.
% [rows, columns, numberOfColorBands] = size(grayImage);
% if numberOfColorBands > 1
% % It's not really gray scale like we expected - it's color.
% % Convert it to gray scale by taking only the green channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
% end
% % Display the original gray scale image.
% subplot(2, 2, 1);
% imshow(grayImage, []);
% axis on;
% title('Original Grayscale Image', 'FontSize', fontSize);
% % Enlarge figure to full screen.
% set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% % Give a name to the title bar.
% set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% % Let's compute and display the histogram.
% [pixelCount, grayLevels] = imhist(grayImage);
% subplot(2, 2, 2);
% bar(grayLevels, pixelCount);
% grid on;
% title('Histogram of original image', 'FontSize', fontSize);
% xlim([0 grayLevels(end)]); % Scale x axis manually.
%
% binaryImage = grayImage~= 40;
% % Display the binary image.
% subplot(2, 2, 3);
% imshow(binaryImage, []);
% axis on;
% title('Binary Image', 'FontSize', fontSize);
%
% % Fill the binary image.
% binaryImage = imfill(binaryImage, 'holes');
% % Display the binary image.
% subplot(2, 2, 4);
% imshow(binaryImage, []);
% axis on;
% title('Filled Binary Image', 'FontSize', fontSize);
%
%
%
% % Calculate the area using bwarea().
% area1 = bwarea(binaryImage);
% % Calculate the area in pixels using sum()
% area2 = sum(binaryImage(:));
% % message = sprintf('The area = %.1f, or %d\ndepending on how you want to calculate it', area1, area2);
% % uiwait(helpdlg(message));
%
% Value = area2;
%
%
app.EditField_4.Value = Value;
end
% Callback function
function Image2Clicked(app, event)
I = imread(app.Image.ImageSource);
x=[size(I,2)/2 size(I,2)/2];
y=[0 size(I,1)];
c = improfile(I,x,y);
figure
subplot(2,1,1)
imshow(I)
hold on
plot(x,y,'r')
subplot(2,1,2)
plot(c(:,1,1),'r')
hold on
plot(c(:,1,2),'g')
plot(c(:,1,3),'b')
end
% Value changed function: EditField_5
function EditField_5ValueChanged(app, event)
value = app.EditField_5.Value;
end
% Button pushed function: AverageDensityButton
function AverageDensityButtonPushed(app, event)
img = imread(app.Image.ImageSource);
% get red, green, blue channels
redC = img(:,:,1);
greenC = img(:,:,2);
blueC = img(:,:,3);
% % mask where red channel is greater than blue channel and green channel greater than blue channel
mask = redC > blueC & greenC > blueC;
meanR = mean(redC(mask));
meanG = mean(greenC(mask));
meanB = mean(blueC(mask));
% % overlay mask into original image
finalimg = bsxfun(@times, img, cast(mask,class(img)));
%plotting
% figure
% subplot(1,2,1)
% imshow(img)
% subplot(1,2,2)
% imshow(finalimg)
l = num2str(meanR);
m = num2str(meanG);
n = num2str(meanB);
app.RedEditField.Value = l;
app.GreenEditField.Value = m;
app.BlueEditField.Value = n;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 1282 808];
app.UIFigure.Name = 'UI Figure';
% Create MiddleEarPanel
app.MiddleEarPanel = uipanel(app.UIFigure);
app.MiddleEarPanel.TitlePosition = 'centertop';
app.MiddleEarPanel.Title = 'Middle Ear';
app.MiddleEarPanel.FontWeight = 'bold';
app.MiddleEarPanel.Position = [11 18 1217 783];
% Create ImagePanel
app.ImagePanel = uipanel(app.MiddleEarPanel);
app.ImagePanel.Title = 'Image Panel';
app.ImagePanel.FontWeight = 'bold';
app.ImagePanel.Position = [288 37 913 675];
% Create Image_2
app.Image_2 = uiimage(app.ImagePanel);
app.Image_2.ImageClickedFcn = createCallbackFcn(app, @Image_2Clicked, true);
app.Image_2.Position = [491 303 413 305];
% Create Image
app.Image = uiimage(app.ImagePanel);
app.Image.ImageClickedFcn = createCallbackFcn(app, @AreaButtonPushed, true);
app.Image.Position = [11 282 470 316];
% Create Image2
app.Image2 = uiimage(app.ImagePanel);
app.Image2.Position = [70 14 623 239];
% Create InfoPanel
app.InfoPanel = uipanel(app.MiddleEarPanel);
app.InfoPanel.Title = 'Info';
app.InfoPanel.FontWeight = 'bold';
app.InfoPanel.Position = [8 685 260 67];
% Create FilenameEditFieldLabel
app.FilenameEditFieldLabel = uilabel(app.InfoPanel);
app.FilenameEditFieldLabel.HorizontalAlignment = 'right';
app.FilenameEditFieldLabel.Position = [39 22 55 22];
app.FilenameEditFieldLabel.Text = 'Filename';
% Create FilenameEditField
app.FilenameEditField = uieditfield(app.InfoPanel, 'text');
app.FilenameEditField.ValueChangedFcn = createCallbackFcn(app, @OpenButtonPushed, true);
app.FilenameEditField.ValueChangingFcn = createCallbackFcn(app, @AreaButtonPushed, true);
app.FilenameEditField.Position = [109 22 100 22];
% Create OptionPanel
app.OptionPanel = uipanel(app.MiddleEarPanel);
app.OptionPanel.Title = 'Option Panel';
app.OptionPanel.FontWeight = 'bold';
app.OptionPanel.Position = [9 582 260 88];
% Create OpenButton
app.OpenButton = uibutton(app.OptionPanel, 'push');
app.OpenButton.ButtonPushedFcn = createCallbackFcn(app, @OpenButtonPushed, true);
app.OpenButton.Position = [9 41 100 22];
app.OpenButton.Text = 'Open ';
% Create ProcessButton
app.ProcessButton = uibutton(app.OptionPanel, 'push');
app.ProcessButton.ButtonPushedFcn = createCallbackFcn(app, @ProcessButtonPushed, true);
app.ProcessButton.Position = [119 41 100 22];
app.ProcessButton.Text = 'Process';
% Create IntensityProfileButton
app.IntensityProfileButton = uibutton(app.MiddleEarPanel, 'push');
app.IntensityProfileButton.ButtonPushedFcn = createCallbackFcn(app, @IntensityProfileButtonPushed, true);
app.IntensityProfileButton.Position = [24 135 100 22];
app.IntensityProfileButton.Text = 'Intensity Profile';
% Create AreaButton
app.AreaButton = uibutton(app.MiddleEarPanel, 'push');
app.AreaButton.ButtonPushedFcn = createCallbackFcn(app, @AreaButtonPushed, true);
app.AreaButton.Position = [16 530 100 22];
app.AreaButton.Text = 'Area';
% Create AverageDensityButton
app.AverageDensityButton = uibutton(app.MiddleEarPanel, 'push');
app.AverageDensityButton.ButtonPushedFcn = createCallbackFcn(app, @AverageDensityButtonPushed, true);
app.AverageDensityButton.Position = [53 319 103 22];
app.AverageDensityButton.Text = 'Average Density';
% Create StandardDeviationButton
app.StandardDeviationButton = uibutton(app.MiddleEarPanel, 'push');
app.StandardDeviationButton.ButtonPushedFcn = createCallbackFcn(app, @StandardDeviationButtonPushed, true);
app.StandardDeviationButton.Position = [62 494 118 22];
app.StandardDeviationButton.Text = 'Standard Deviation';
% Create Label_2
app.Label_2 = uilabel(app.MiddleEarPanel);
app.Label_2.HorizontalAlignment = 'right';
app.Label_2.Position = [119 530 25 22];
app.Label_2.Text = '';
% Create EditField_5
app.EditField_5 = uieditfield(app.MiddleEarPanel, 'text');
app.EditField_5.ValueChangedFcn = createCallbackFcn(app, @EditField_5ValueChanged, true);
app.EditField_5.Position = [159 530 100 22];
% Create RedEditFieldLabel
app.RedEditFieldLabel = uilabel(app.MiddleEarPanel);
app.RedEditFieldLabel.HorizontalAlignment = 'right';
app.RedEditFieldLabel.Position = [35 268 28 22];
app.RedEditFieldLabel.Text = 'Red';
% Create RedEditField
app.RedEditField = uieditfield(app.MiddleEarPanel, 'text');
app.RedEditField.Position = [78 268 100 22];
% Create GreenEditFieldLabel
app.GreenEditFieldLabel = uilabel(app.MiddleEarPanel);
app.GreenEditFieldLabel.HorizontalAlignment = 'right';
app.GreenEditFieldLabel.Position = [24 225 39 22];
app.GreenEditFieldLabel.Text = 'Green';
% Create GreenEditField
app.GreenEditField = uieditfield(app.MiddleEarPanel, 'text');
app.GreenEditField.Position = [78 225 100 22];
% Create BlueEditFieldLabel
app.BlueEditFieldLabel = uilabel(app.MiddleEarPanel);
app.BlueEditFieldLabel.HorizontalAlignment = 'right';
app.BlueEditFieldLabel.Position = [24 187 30 22];
app.BlueEditFieldLabel.Text = 'Blue';
% Create BlueEditField
app.BlueEditField = uieditfield(app.MiddleEarPanel, 'text');
app.BlueEditField.Position = [69 187 100 22];
% Create RedStdEditFieldLabel
app.RedStdEditFieldLabel = uilabel(app.MiddleEarPanel);
app.RedStdEditFieldLabel.HorizontalAlignment = 'right';
app.RedStdEditFieldLabel.Position = [45 457 46 22];
app.RedStdEditFieldLabel.Text = 'RedStd';
% Create RedStdEditField
app.RedStdEditField = uieditfield(app.MiddleEarPanel, 'text');
app.RedStdEditField.Position = [106 457 100 22];
% Create GreenStdEditFieldLabel
app.GreenStdEditFieldLabel = uilabel(app.MiddleEarPanel);
app.GreenStdEditFieldLabel.HorizontalAlignment = 'right';
app.GreenStdEditFieldLabel.Position = [45 415 57 22];
app.GreenStdEditFieldLabel.Text = 'GreenStd';
% Create GreenStdEditField
app.GreenStdEditField = uieditfield(app.MiddleEarPanel, 'text');
app.GreenStdEditField.Position = [117 415 100 22];
% Create BlueStdEditFieldLabel
app.BlueStdEditFieldLabel = uilabel(app.MiddleEarPanel);
app.BlueStdEditFieldLabel.HorizontalAlignment = 'right';
app.BlueStdEditFieldLabel.Position = [52 376 48 22];
app.BlueStdEditFieldLabel.Text = 'BlueStd';
% Create BlueStdEditField
app.BlueStdEditField = uieditfield(app.MiddleEarPanel, 'text');
app.BlueStdEditField.Position = [115 376 100 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Patient5
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
댓글 수: 9
채택된 답변
Adam Danz
2019년 8월 10일
Answer to first question:
(Summarized from comments under the question) The error is due to improper input to imread(). The expected input is a filename, not image data.
Answer to second question:
The reason you're getting a new figure instead of plotting the image in you app axes is because you're directly creating the new figure in your code by calling figure(). Remove the line that creates a new figure and remove the lines that create new subplots. Instead, specify your UIAxes when you callmby specifying the parent property.
function IntensityProfileButtonPushed(app, event)
I = imread(app.Image.ImageSource);
x=[size(I,2)/2 size(I,2)/2];
y=[0 size(I,1)];
c = improfile(I,x,y);
% figure REMOVE
% subplot(2,1,1) REMOVE
imshow(I,'parent',app.UIAxes1) % Use the correct axes handle
hold(app.UIAxes1,'on') % also specify the axes handle here
plot(app.UIAxes1, x,y,'r') % also specify the axes handle here
%subplot(2,1,2) REMOVE
plot(app.UIAxes2, c(:,1,1),'r') % Use the correct axes handle
hold(app.UIAxes2,'on') % also specify the axes handle here
plot(app.UIAxes2,c(:,1,2),'g') % also specify the axes handle here
plot(app.UIAxes2,c(:,1,3),'b') % also specify the axes handle here
end
Lesson learned: always specify the axis handle when possible.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!