Grayscale Image cannot be output in the GUI Matlab

24 views (last 30 days)
Hi guys, i got a problem here where i want to put my picture into the GUI , and then change the RGB image into grayscale image, but somehow the grayscale image keep getting error and i dont how to fix it. Help please.
Below is the coding that i had write,
[filename,filepath] = uigetfile({'*.*;*.jpg;*.png;*.bmp;*.oct'}, 'Select File to Open');
fullname = imread(strcat(filepath,filename));
app.Image.ImageSource = fullname;
grayImage = rgb2gray(fullname);
app.Image_2.ImageSource = grayImage;
The error that been pop-out is ,
Error using matlab.ui.control.Image/set.ImageSource (line 123)
You have specified an invalid CData.
Valid CData is m-by-n-by-3 truecolor image array.
Thank you in advance
  2 Comments
Dong Yi
Dong Yi on 20 Nov 2020
Edited: Dong Yi on 20 Nov 2020
Do you have FB? I want to ask you a question.
I don’t know how to use this website

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 20 Nov 2020
Edited: Cris LaPierre on 20 Nov 2020
The function rgb2gray returns an mxn matrix. However, as you can see in the error message, the image component in an app requires an mxnx3 array.
In order to display a grayscale image created using rgb2gray, you will need to insert an axes, and plot to the axes using imshow or other suitable plotting function
cimg = imread('peppers.png');
gr=rgb2gray(cimg);
ax = axes;
imshow(gr,'Parent',ax);
.
  5 Comments
Cris LaPierre
Cris LaPierre on 21 Nov 2020
Edited: Cris LaPierre on 1 Dec 2020
App Desiger now has most if not all the capabilites of Guide, with additional fetaures coming with each release. If you are going to create an app, there is no reason to avoid App Designer.
Mohd Sukry Mohd Taib
Mohd Sukry Mohd Taib on 24 Nov 2020
Thank you guys for the information . This is help alot in terms to understand more the GUI system :D

Sign in to comment.

More Answers (1)

Ali Ridha Ali
Ali Ridha Ali on 16 Feb 2023
Edited: Ali Ridha Ali on 17 Feb 2023
I've faced the same issue, and I got a technical solution by doing a small trick.
You can show grayscale in app.Image component by re-building the grayscale image to be in a 3-channels matrix so that app.Image component can deal with it as an RGB image. This is my code:
GrayImg = rgb2gray(RGBImg);
GrayImg3 = cat(3, GrayImg, GrayImg, GrayImg);
app.Image.ImageSource = GrayImg3;
I tested it, and it works for me :)
I'm showing the trick here in case someone is facing the same problem.

Community Treasure Hunt

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

Start Hunting!