MATLAB Answers

Displaying an image on UIAxes in App Designer that doesn't appear

조회 수: 2(최근 30일)
Ahmed Mukhtar
Ahmed Mukhtar 2021년 5월 13일
댓글: Ahmed Mukhtar 2021년 5월 14일
Hey there,
I've been trying to design a GUI in the app designer that estimates noise, it:
  1. Takes an image specified by the user, image appears on UIAxes2
  2. User selects a type of noise to add
  3. Image with noise added is displayed again on UIAxes2
Please advise.
The original selected image appears just fine but when I want the noisy image to appear, it doesn't. Here are screenshots of the code + the App
  1. Original Image button callback code
function UploadImageButtonPushed(app, event)
[filename,filepath] = uigetfile({ '*.*;*.jpg;*.png;*.bmp;*.oct' }, 'Select File to Open');
fullname = [filepath, filename];
file = imread(fullname);
global pic
pic=im2gray(file);
image1=im2double(pic);
imshow(image1, 'parent', app.UIAxes2)
end
2. Noisy image button callback code
% Button pushed function: ApplyNoiseButton
function ApplyNoiseButtonPushed(app, event)
global image1
global noisy
if (strcmp(app.SelectTypeofNoisetoAddDropDown.Value,'Salt & Pepper'))
noisy = imnoise(image1, 'salt & pepper');
elseif(strcmp(app.SelectTypeofNoisetoAddDropDown.Value, 'Gaussian'))
noisy = imnoise(image1, 'gaussian', 0, 0.01);
elseif(strcmp(app.SelectTypeofNoisetoAddDropDown.Value,'Speckle'))
noisy = imnoise(image1, 'speckle');
else
noisy=image1;
end
imshow(noisy,'parent', app.UIAxes2)
end
Screenshots of GUI:

채택된 답변

Geoff Hayes
Geoff Hayes 2021년 5월 14일
Ahmed - I suspect the problem is with your variable for image1. It is declared as a global variable in ApplyNoiseButtonPushed function, but it is a local variable in the UploadImageButtonPushed function. I suppose that you could use pic instead or find an alternative to using global variables. Perhaps save the image as a property to the app object.
  댓글 수: 1
Ahmed Mukhtar
Ahmed Mukhtar 2021년 5월 14일
I globalized image1 and the entire app worked, thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by