Open In App

How To Use Different Images or Colormaps on a Same Figure into MATLAB?

Last Updated : 01 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A color map is a matrix-based value that is used to define the colors for graphical representation in MATLAB. MATLAB has some functions that are used to draw the objects by mapping data values to the color map. The color map is formulated in three columns wide and each row contains three elements RGB (red, green, blue) triplet. in colormap RGB the lower color limits are mapped to the first entry, the upper color limit is mapped to the second to last, and data is mapped linearly may between twice upper and lower.

A color map is a mapping from the matrix data values to generate real-time visual structures data, the colormaps are mostly used in computer science fields like computers graphic, CV computer vision, Visualization, and Digital image processing.

Syntax:

colormap map  % sets the colormap for figure.
colormap (map)  % sets the colormap for the figure specified by map.
colormap(target, map) % sets colormap for figure, axes,or chart  specified by target.
 cmap = colormap  % return the colormap for figure as 3-D column matrix of RGB.
cmap = colormap(target) % return the colormap for figure, axes, or chart which specified by target.

Here we will take an example of performing different images or colormap on the same figure into MATLAB figure, plot, and graph. Use the colormap in MATLAB. First of all, open your MATLAB editor and make a new file for color mapping write the following line of code. 

Example 1:

Matlab




% Code for colormap in same figure.
% contains the invisible grid of tiles.
tiledlayout(2,1) 
fig1 = nexttile;
 
% create a 3-D surface plot.
surf(peaks)  
 
% define the color graphics on image.
colormap(fig1,spring) 
 
fig2 = nexttile;
surf(peaks)
colormap(fig2,winter)


  • tiledlayout() – it’s a tiled chart layout that contains an invisible grid of tiles that covers the entire figure. and also each tile contains axes for displaying the figure(plot).
  • nexttitle – this is creating an axes object and placing it into the next tiled chart.
  • surf() – this function is used to create a three-dimensional surface plot (figure).
  • colormap() – colormap() function is matrix values that define the colors for graphical representation, it contains three-column arrays containing RGB triplet.

After running this sample code you will get this three-dimensional figure.

Output:

 

Use the different images in the same figure in MATLAB. So in this example First of all open your MATLAB editor and make a new file for display the image in same figure. write the following line of code.

Example 2:

Matlab




% MATLAB code
Imdstore = imageDatastore('images','IncludeSubfolders'
,Data,'LabelSource','foldernames');
 
% read the subfolder and count image.
A = countEachLabel(Imdstore.Files);
i = readimage(Imdstore, 18025);
 
% count the total image of all subplots
img_total = length(Imdstore.Files);
imshow(i);
 
% reading the and display multiple image in same figure.
a=2;
b=2;
 
% return the random permutation of integer 1 to n.
N = randperm(img_total , a*b );
figure ('Color','white'),
Idx = 1;
for j = 1:a
    for k= 1:b
    % reads the image from the datastore.
        img = readimage(Imdstore,N(Idx));
        subplot(a,b,Idx)
        imshow(img);
        Idx = idx+1;
    end
end


In the above MATLAB code we use the ImageDatastore() function for managing the collection of image files, and displaying multiple images in the same figure. here we take the image as a GFG logo that should be inside any folder and also inside the subfolder then only the image will be read.

  • countEachLabel() – it’s used to count the number of times each label occurs in the data store.
  • readimage() – this function is used to reads the Ith image file from the datastore.
  • randperm() – it returns a row vector which contains random permutation of integer from 1 to n.
  • subplot() – it divides the current figure into m by n grid and creates axes.

Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads