How can set colormap for below matirx?

4 views (last 30 days)
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);
  4 Comments
Image Analyst
Image Analyst on 6 Dec 2019
For completeness, here is the image that your code and data create:
0001 Screenshot.png
Amir Torabi
Amir Torabi on 6 Dec 2019
Yes i know what my output is. But as you see the colors of grains are same. Please take a below photo.It is extraced from a code like mine. But i don't know how it is possible to dedicate spesified color for each of grains. I am looking for generating a microstructure like it. Colorful!.sample.jpg

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 6 Dec 2019
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
  1 Comment
Amir Torabi
Amir Torabi on 6 Dec 2019
very grateful!. Yes the last image is what i looked for. However, your code contains several new commands. I have a question about the color of last image. Accidentaly, some grains have same color again!. Is there a way that the color to be more unique?

Sign in to comment.

More Answers (1)

Kristoffer Walker
Kristoffer Walker on 5 Dec 2019
Edited: Kristoffer Walker on 5 Dec 2019
Amir,
Look into "caxis" and "colormap" functions. Basically, to manipulate the color map (so that it highlights different pixels differently) you need to adjust the range of the colormap matrix by making the first row correspond to the end range of the values of interest and the last row correspond to the other edge of the range. Imagesc will automatically scale the min and max values of the matrix to the edges of the colormap.
Use ColorMapEditor to create custom colormap matricies. For higher dynamic range colormaps, make the number of rows in your colormap large. The number of columns is always 3. Execute "colormap" to see the current colormap. Use "colormap(cm)" to assign your own matrix cm to be the colormap. Red, Green, Blue columns from 0 to 1 in amplitude. So white is [1 1 1].
You could also plot symbols individually in a for loop over all pixels of interest if you do not want to be restricted to a grid:
nRows = 64; % Increase this for better dynamic range
dCol = (maxAmp-minAmp)/nRows;
cm = colormap(jet); % execute "help graph3d" to see built-in (e.g. parula, jet, etc.)
for i=1:nSymbols
% find nearest row of colormap for current pixel
iRow = (amp(i) - minAmp) / dCol + 1;
h = plot(x, y, 'MarkerFaceColor', cm(iRow,:));
end

Community Treasure Hunt

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

Start Hunting!