Borrar filtros
Borrar filtros

how to read a grey scale image in call back function and display it as matrix?

4 visualizaciones (últimos 30 días)
% Callback function for the "Load Image" button
function loadImageCallback(hObject, eventdata, handles)
% Open a file dialog to select an image
[fileName, filePath] = uigetfile({'*.jpg;*.png;*.bmp;*.tif', 'All Image Files'; '*.*', 'All Files'}, 'Select an Image');
% Check if a file was selected
if isequal(fileName, 0)
return; % User canceled the operation
end
% Read the selected image
img = imread(fullfile(filePath, fileName));
% Check if the image was read successfully
if isempty(img)
errordlg('Failed to read the image', 'Error');
return;
end
% Store the image data in the handles structure
handles.img = img;
% Update the handles structure
guidata(hObject, handles);
% Optionally, display a message to indicate successful loading
msgbox('Image loaded successfully', 'Success');
end

Respuestas (1)

Adam Danz
Adam Danz el 29 de Mayo de 2024
Editada: Adam Danz el 30 de Mayo de 2024
If you need to convert the image to grayscale use rgb2gray or im2gray (thanks DGM).
I'm not sure what you mean to display an image as a matrix. The grayscale image should already be a matrix. You could use imagesc or pcolor to plot the matrix (or imshow, image, or others). Set the colormap to gray.
  2 comentarios
DGM
DGM el 30 de Mayo de 2024
Editada: DGM el 30 de Mayo de 2024
Just to get ahead of any complications, I'm of the opinion that any users of versions R2020b or newer should not be using rgb2gray() unless they're catering to older versions with the appropriate care. The only difference between rgb2gray() and im2gray() is that im2gray() will pass single-channel inputs as if gray, whereas rgb2gray() will throw an error.
Otherwise, all blind usage of rgb2gray() necessarily must come wrapped in a check for the size of dim3 to prevent errors. I don't know why this basic improvement was implemented as a mostly redundant function instead of improving the existing rgb2gray() (maybe the implications of the function name itself), but it is what it is.

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by