Grayscale Image cannot be output in the GUI Matlab

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

Dong Yi
Dong Yi 2020년 11월 20일
편집: Dong Yi 2020년 11월 20일
Do you have FB? I want to ask you a question.
I don’t know how to use this website
it is better to be ask here . in comment

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

 채택된 답변

Cris LaPierre
Cris LaPierre 2020년 11월 20일
편집: Cris LaPierre 2020년 11월 20일
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

Oh, I understand. Seems that the image need to be in mxn array if using the app right?
When using the axes it has not problem with it. Thank you!
After change into the Axes feature i manage to upload the pictures and change into the 2D images!
You can use images in an app, but they have to be mxnx3, where the 3 is the rgb color values. A grayscale image is 2D, so it cannot be used in an image component. That is when you should use an axes instead.
I'm not using app designer for other reasons until they improve it, but it looks like you're saying there is both an axes component, and an image component. And you're saying that the image component can only handle RGB images while the axes component can handle either RGB or gray scale. (Looks like yet another reason to avoid App Designer, but anyway...) I can see this as being a common source of confusion, and I'm sure Mohd was not the first and only person to have that confusion. Why don't they make the image component handle gray scale images? This seems like a major oversight that needs to be fixed in the next release.
Cris LaPierre
Cris LaPierre 2020년 11월 21일
편집: Cris LaPierre 2020년 12월 1일
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.
Thank you guys for the information . This is help alot in terms to understand more the GUI system :D

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

추가 답변 (1개)

Ali Ridha Ali
Ali Ridha Ali 2023년 2월 16일
편집: Ali Ridha Ali 2023년 2월 17일
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.

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by