0
votes

I have a gui for calculating and plot the image of my project. It shows the images in 'Jet' colormap. However, I want the display images in the gray colormap.

I have tried the following it. But it showed the image in black colormap.
cmap = gray(255); J1 = ind2gray(J,cmap);
set(handle_fig,'Colormap',J1);

The actual code is like that:

    n = ceil(m/4);
    u = [(1:1:n)/n ones(1,n-1) (n:-1:1)/n]';
    g = ceil(n/2) - (mod(m,4)==1) + (1:length(u))';
    r = g + n;
    b = g - n;
    g(g>m) = [];
    r(r>m) = [];
    b(b<1) = [];
    J = zeros(m,3);
    J(r,1) = u(1:length(r));
    J(g,2) = u(1:length(g));
    J(b,3) = u(end-length(b)+1:end);

    if verLessThan('matlab','9.1')
         set(handle_fig,'Colormap',J); 


    else
        % Get all axes and set their colormap. This is a fix for 2016b+ 
        % (9.1+) which now sets colormaps on a per-axis basis for imshow()
        % instead of for the entire figure. 
        handle_axes = findobj(handle_fig,'type','axes');
        for i = 1:length(handle_axes)
            colormap(handle_axes(i),J);
        end
    end

Default colomap is like this which is jet. I want to convert this jet colormap to grayscale.

UPDATE

I have tried the answer of Cris Luengo it gives me this output In this figure, the lower and upper colorbar both starts and ends at black. However, I want just the lower scale starts value from white and upper scale at black.

The code in the GUI for the colobar is as follow:

% Set colorbar
            handle_colorbar = colorbar('peer',handles_gui_sub.axes_formatplot);        
            set(handle_colorbar,'UIContextMenu','');
            set(get(handle_colorbar,'child'),'YData',[cmin cmax]);
            set(handle_colorbar,'YLim',[cmin cmax]);
            set(handle_colorbar,'Units','Pixels');
1
I would recommend using the "gray" (grayscale) colormap... if you convert the jet colormap to grayscale the high values and low values will show up black, and middle values will show up lighter. So just use: set(handle_fig,'Colormap','gray'); - Daniel Aldrich
I have tried as you suggested set(handle_fig,'Colormap','gray') It shows me this error: Error using matlab.ui.Figure/set While setting the 'Colormap' property of Figure: Value must be either: an Mx3 array of type single or double in the range [0 1] an Mx3 array of type uint8 - Khubaib Ilyas Khan

1 Answers

1
votes

ind2gray converts an indexed image to grayscale. Obviously you don’t have an indexed image, so this function is not useful.

Instead, use rgb2gray to convert an RGB color map to a gray-scale color map:

J1 = rgb2gray(J);