Can't convert RGB image to grayscale with button in app designer

6 ビュー (過去 30 日間)
Reyhan Putra
Reyhan Putra 2020 年 5 月 21 日
コメント済み: Reyhan Putra 2020 年 6 月 3 日
Hi, So apparently i want to make an app with matlab app designer that show RGB histogram of an image, and convert it to grayscale and b&w, but i kept getting an error when trying to convert it to grayscale, I don't know what's the problem, So can u guys help me ? The grayscale button called app.Grayscale.
Thank u, Have a great day.
classdef revisi_App < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
figure1 matlab.ui.Figure
axes1 matlab.ui.control.UIAxes
axes2 matlab.ui.control.UIAxes
RedAxes matlab.ui.control.UIAxes
LoadGambar matlab.ui.control.Button
Reset matlab.ui.control.Button
Exit matlab.ui.control.Button
RGB matlab.ui.control.Button
Grayscale matlab.ui.control.Button
BnW matlab.ui.control.Button
pushbutton12 matlab.ui.control.Button
GreenAxes matlab.ui.control.UIAxes
BlueAxes matlab.ui.control.UIAxes
end
methods (Access = private)
function updateimage(app,imagefile)
% img
if strcmp(imagefile,'corn.tif')
im = imread('corn.tif', 2);
else
try
im = imread(imagefile);
catch ME
% Error message
uialert(app.UIFigure, ME.message, 'Image Error');
return;
end
end
% Histogram
switch size(im,3) > 1
case 1
% Display the truecolor image
imagesc(app.axes1,im);
% Plotting 2
histr = histogram(app.RedAxes, im(:,:,1), 'FaceColor', [1 0 0], 'EdgeColor', 'none');
histg = histogram(app.GreenAxes, im(:,:,2), 'FaceColor', [0 1 0], 'EdgeColor', 'none');
histb = histogram(app.BlueAxes, im(:,:,3), 'FaceColor', [0 0 1], 'EdgeColor', 'none');
otherwise
% Error when image is not grayscale or truecolor
uialert(app.UIFigure, 'Image must be grayscale or truecolor.', 'Image Error');
return;
end
% Get largest bin count
maxr = max(histr.BinCounts);
maxg = max(histg.BinCounts);
maxb = max(histb.BinCounts);
maxcount = max([maxr maxg maxb]);
% Set y axes limits based on largest bin count
app.RedAxes.YLim = [0 maxcount];
app.RedAxes.YTick = round([0 maxcount/2 maxcount], 2, 'significant');
app.GreenAxes.YLim = [0 maxcount];
app.GreenAxes.YTick = round([0 maxcount/2 maxcount], 2, 'significant');
app.BlueAxes.YLim = [0 maxcount];
app.BlueAxes.YTick = round([0 maxcount/2 maxcount], 2, 'significant');
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function revisi_OpeningFcn(app)
% % This function has no output args, see OutputFcn.
% % hObject handle to figure
% % eventdata reserved - to be defined in a future version of MATLAB
% % handles structure with handles and user data (see GUIDATA)
% % varargin command line arguments to revisi (see VARARGIN)
%
% % Choose default command line output for revisi
% app.output = hObject;
%
% % Update handles structure
% guidata(hObject, handles);
% Configure image axes
app.axes1.Visible = 'off';
app.axes1.Colormap = gray(256);
axis(app.axes1, 'image');
end
% Button pushed function: BnW
function BnW_Callback(app, event)
% hObject handle to BnW (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=getappdata(0,'a');
a_bw=im2bw(a,0.4)
imshow(a_bw, 'Parent', app.axes2);
end
% Button pushed function: Exit
function Exit_Callback(app, event)
% hObject handle to Exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
delete(app);
end
% Button pushed function: Grayscale
function Grayscale_Callback(app, event)
% hObject handle to Grayscale (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a = app.axes1;
[rows, columns, numberOfColorChannels] = size(a);
if numberOfColorChannels > 1
a_gray = rgb2gray(a);
imshow(a_gray, 'Parent', app.axes2)
else
uialert(app.figure1, 'Image must be RGB', 'Image Error');
end
end
% Button pushed function: LoadGambar
function LoadGambar_Callback(app, event)
% Display uigetfile dialog
filterspec = {'*.jpg;*.tif;*.png;*.gif','All Image Files'};
[f, p] = uigetfile(filterspec);
% Make sure user didn't cancel uigetfile dialog
if (ischar(p))
fname = [p f];
updateimage(app, fname);
end
end
% Button pushed function: RGB
function RGB_Callback(app, event)
% hObject handle to RGB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=getappdata(0,'a');
imshow(a, 'Parent', app.axes1);
end
% Button pushed function: Reset
function Reset_Callback(app, event)
% hObject handle to Reset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clear, clc, close all
end
% Button pushed function: pushbutton12
function pushbutton12_Callback(app, event)
% hObject handle to pushbutton12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=getappdata(0,'a');
red=a;
red(:,:,2:3)=0;
imshow(red, 'Parent', app.axes2);
end
% Callback function
function pushbutton13_Callback(app, event)
% hObject handle to pushbutton13 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=getappdata(0,'a');
axes(app.axes2);
imhist(a);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create figure1 and hide until all components are created
app.figure1 = uifigure('Visible', 'off');
app.figure1.Position = [680 481 1210 617];
app.figure1.Name = 'revisi';
app.figure1.Scrollable = 'on';
% Create axes1
app.axes1 = uiaxes(app.figure1);
app.axes1.FontSize = 13;
app.axes1.TickDir = 'in';
app.axes1.GridAlpha = 0.15;
app.axes1.MinorGridAlpha = 0.25;
app.axes1.XTick = zeros(1,0);
app.axes1.YTick = zeros(1,0);
app.axes1.ZTick = zeros(1,0);
app.axes1.NextPlot = 'replace';
app.axes1.Position = [27 216 351 301];
% Create axes2
app.axes2 = uiaxes(app.figure1);
app.axes2.FontSize = 13;
app.axes2.XTick = [];
app.axes2.YTick = [];
app.axes2.NextPlot = 'replace';
app.axes2.Position = [427 209 351 309];
% Create RedAxes
app.RedAxes = uiaxes(app.figure1);
title(app.RedAxes, 'Red')
xlabel(app.RedAxes, 'Intensitas')
ylabel(app.RedAxes, 'Pixel')
app.RedAxes.FontSize = 13;
app.RedAxes.XTick = [0 128 255];
app.RedAxes.XTickLabel = {'0'; '128'; '255'};
app.RedAxes.NextPlot = 'replace';
app.RedAxes.Position = [777 365 308 160];
% Create LoadGambar
app.LoadGambar = uibutton(app.figure1, 'push');
app.LoadGambar.ButtonPushedFcn = createCallbackFcn(app, @LoadGambar_Callback, true);
app.LoadGambar.FontName = 'Open Sans';
app.LoadGambar.FontSize = 19;
app.LoadGambar.Position = [27 166 351 36];
app.LoadGambar.Text = 'Load Gambar';
% Create Reset
app.Reset = uibutton(app.figure1, 'push');
app.Reset.ButtonPushedFcn = createCallbackFcn(app, @Reset_Callback, true);
app.Reset.FontName = 'Open Sans';
app.Reset.FontSize = 19;
app.Reset.Position = [27 116 151 36];
app.Reset.Text = 'Reset';
% Create Exit
app.Exit = uibutton(app.figure1, 'push');
app.Exit.ButtonPushedFcn = createCallbackFcn(app, @Exit_Callback, true);
app.Exit.FontName = 'Open Sans';
app.Exit.FontSize = 19;
app.Exit.Position = [227 116 151 36];
app.Exit.Text = 'Exit';
% Create RGB
app.RGB = uibutton(app.figure1, 'push');
app.RGB.ButtonPushedFcn = createCallbackFcn(app, @RGB_Callback, true);
app.RGB.FontName = 'Open Sans';
app.RGB.FontSize = 19;
app.RGB.Position = [427 166 351 36];
app.RGB.Text = 'RGB';
% Create Grayscale
app.Grayscale = uibutton(app.figure1, 'push');
app.Grayscale.ButtonPushedFcn = createCallbackFcn(app, @Grayscale_Callback, true);
app.Grayscale.FontName = 'Open Sans';
app.Grayscale.FontSize = 19;
app.Grayscale.Position = [427 116 151 36];
app.Grayscale.Text = 'Grayscale';
% Create BnW
app.BnW = uibutton(app.figure1, 'push');
app.BnW.ButtonPushedFcn = createCallbackFcn(app, @BnW_Callback, true);
app.BnW.FontName = 'Open Sans';
app.BnW.FontSize = 19;
app.BnW.Position = [627 116 151 36];
app.BnW.Text = 'Black & White';
% Create pushbutton12
app.pushbutton12 = uibutton(app.figure1, 'push');
app.pushbutton12.ButtonPushedFcn = createCallbackFcn(app, @pushbutton12_Callback, true);
app.pushbutton12.FontSize = 11;
app.pushbutton12.Position = [616 21 91 51];
app.pushbutton12.Text = 'tes red';
% Create GreenAxes
app.GreenAxes = uiaxes(app.figure1);
title(app.GreenAxes, 'Green')
xlabel(app.GreenAxes, 'Intensitas')
ylabel(app.GreenAxes, 'Pixel')
app.GreenAxes.FontSize = 13;
app.GreenAxes.XTick = [0 128 255];
app.GreenAxes.XTickLabel = {'0'; '128'; '255'};
app.GreenAxes.NextPlot = 'replace';
app.GreenAxes.Position = [777 197 308 160];
% Create BlueAxes
app.BlueAxes = uiaxes(app.figure1);
title(app.BlueAxes, 'Blue')
xlabel(app.BlueAxes, 'Intensitas')
ylabel(app.BlueAxes, 'Pixel')
app.BlueAxes.FontSize = 13;
app.BlueAxes.XTick = [0 128 255];
app.BlueAxes.XTickLabel = {'0'; '128'; '255'};
app.BlueAxes.NextPlot = 'replace';
app.BlueAxes.Position = [777 29 308 160];
% Show the figure after all components are created
app.figure1.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = revisi_App
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.figure1)
% Execute the startup function
runStartupFcn(app, @revisi_OpeningFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.figure1)
end
end
end

採用された回答

Sampath Rachumallu
Sampath Rachumallu 2020 年 6 月 2 日
編集済み: Sampath Rachumallu 2020 年 6 月 3 日
You are directly calling uiaxes handle but not the child that has the image object. To access the image displayed in uiaxes Try to use a = findall(app.axes1, 'type', 'image')
If you have image processing toolbox you can use
imhandles(app.axes1) as well
Instead of using a=app.axes1

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by