Grayscale Image cannot be output in the GUI Matlab

30 visualizaciones (últimos 30 días)
Mohd Sukry Mohd Taib
Mohd Sukry Mohd Taib el 20 de Nov. de 2020
Editada: Ali Ridha Ali el 17 de Feb. de 2023
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 comentarios
Dong Yi
Dong Yi el 20 de Nov. de 2020
Editada: Dong Yi el 20 de Nov. de 2020
Do you have FB? I want to ask you a question.
I don’t know how to use this website
Mohd Sukry Mohd Taib
Mohd Sukry Mohd Taib el 20 de Nov. de 2020
it is better to be ask here . in comment

Iniciar sesión para comentar.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 20 de Nov. de 2020
Editada: Cris LaPierre el 20 de Nov. de 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 comentarios
Cris LaPierre
Cris LaPierre el 21 de Nov. de 2020
Editada: Cris LaPierre el 1 de Dic. de 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 el 24 de Nov. de 2020
Thank you guys for the information . This is help alot in terms to understand more the GUI system :D

Iniciar sesión para comentar.

Más respuestas (1)

Ali Ridha Ali
Ali Ridha Ali el 16 de Feb. de 2023
Editada: Ali Ridha Ali el 17 de Feb. de 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!

Translated by