# How can set colormap for below matirx?

2 ビュー (過去 30 日間)
Amir Torabi 2019 年 12 月 5 日
Commented: Amir Torabi 2019 年 12 月 6 日
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 件のコメント

Amir Torabi 2019 年 12 月 6 日
I thank you for your help. Please load tha attached m.file. It contains the values of etas. If you use it, my question will be clear. I'd like to set custom colors for each of grains which are shown in imagesc.
Thanks again.
%code
Nx=64;
Ny=Nx;
eta2 = zeros(64,64);
% original code
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
imagesc(eta2);
Image Analyst 2019 年 12 月 6 日
For completeness, here is the image that your code and data create:
Amir Torabi 2019 年 12 月 6 日
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!.

サインイン to comment.

### 採用された回答

Image Analyst 2019 年 12 月 6 日
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
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]

#### 1 件のコメント

Amir Torabi 2019 年 12 月 6 日
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?

サインイン to comment.

Kristoffer Walker 2019 年 12 月 5 日

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

#### 0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

Translated by