MATLAB: How can set colormap for below matirx

colormapMATLABmatrix manipulation

Hello Friends.
The below code calculates an matrix eta2. and then display some shapes that each of them is on specified positions. For displaying it, I used imagesc(eta2).
Also, etas is a matrix that its value varies between 0 and 1 randomly. Indeed, each grain has own values between grid points i and j.
Now I am looking for determining a custom color for each of grains. How can do it.
Thanks.
%code
Nx=128;%grid
Ny=Nx;
eta2 = zeros(Nx,Ny);
etas = rand(Nx*Ny,25));
for igrain=1:25
ncount=0;%counter
for i=1:Nx
for j=1:Nx
ii =(i-1)*Nx+j;
eta2(i,j) =eta2(i,j)+etas(ii,igrain)^2; %calculating the eta2.
if(etas(ii,igrain) >= 0.5)
ncount=ncount+1;
end
%
ncount=ncount/(Nx*Ny);
end
end%Nx
end%igrain
%%Display
figure
imagesc(eta2);

Best Answer

I think this is what you want:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
% Existing code
Nx=64;
Ny=Nx;
eta2 = zeros(64,64);
% original code
load storeEtas
for igrain=1:25
ncount=0;
for i=1:Nx
for j=1:Nx
ii =(i-1)*Nx+j;
eta2(i,j) =eta2(i,j)+etas(ii,igrain)^2;
if(etas(ii,igrain) >= 0.5)
ncount=ncount+1;
end
ncount=ncount/(Nx*Ny);
end
end
end
figure
% New code below by Image Analyst:
subplot(2, 3, 1);
imshow(eta2, [], 'ColorMap', hsv(256));
colorbar;
title('eta2, Pseudo-colored', 'FontSize', fontSize);
impixelinfo;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
drawnow;
% Plot the image in gray scale
subplot(2, 3, 2);
imshow(eta2, [], 'ColorMap', gray(256));
colorbar;
title('eta2, Actual values (gray scale)', 'FontSize', fontSize);
impixelinfo;
% Show the histogram
subplot(2, 3, [3, 6]);
imhist(eta2);
grid on;
title('Histogram of eta2', 'FontSize', fontSize);
% Threshold the image
thresholdValue = 0.7;
line([thresholdValue, thresholdValue], ylim, 'Color', 'r', 'LineWidth', 2);
binaryImage = eta2 > thresholdValue;
subplot(2, 3, 4);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontSize);
% Label the image so we can assign (almost) unique colors to each region,
% and so we can make measurements of it, such as the area of each blob.
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);
fprintf('Found %d blobs.\n', numberOfBlobs);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(2, 3, 5);
imshow(coloredLabelsImage);
title('Individually colored regions of eta2', 'FontSize', fontSize);
% Get all the blob properties.
blobMeasurements = regionprops(labeledImage, eta2, 'all')
% blobMeasurements is an array of structures. Each structure has a bunch of measurements in it.
% For example, get all the areas into a single vector.
allAreas = [blobMeasurements.Area]
01.png