How can assign a color for two member of a matrix?

1 Ansicht (letzte 30 Tage)
Amir Torabi
Amir Torabi am 7 Jan. 2020
Kommentiert: Meg Noah am 16 Jan. 2020
Hello everyone.
I have written the below code. The aim of the below code is defining a red color for two bubble 26 and 27(eta(:,:,26) and eta(:,:,27).
I did it for one of them, but i have no idea about the assigning red color for bubble 27.
The eta matrix contain the bubbles 26 and 27.
Kindly appreciated for your help.
load eta
mag=1;
phi=sum(eta(:,:,:).^2,3);
graymap=1/(max(max(phi-min(min(phi)))))*(phi-min(min(phi)));
% subplot(2,1,1)
%Assiging color for particle-26
cmap(:,:,1)=graymap;
cmap(:,:,2)=imcomplement(eta(:,:,26)).*graymap;
cmap(:,:,3)=imcomplement(eta(:,:,26)).*graymap;
%Display
imagesc(cmap);

Akzeptierte Antwort

Meg Noah
Meg Noah am 7 Jan. 2020
Bearbeitet: Meg Noah am 7 Jan. 2020
There are a couple of different ways to do it. But I really like just making a false color composite over making a pseudocolor. The trick for your data is picking a threshold for establishing the region that will be colored - I chose 0.1
clc
close all
clear all
load('eta.mat');
mag=1;
phi=sum(eta(:,:,:).^2,3);
graymap=1/(max(max(phi-min(min(phi)))))*(phi-min(min(phi)));
codedImageG = 255*graymap;
codedImageR = codedImageG;
codedImageB = codedImageG;
% make 27 in red only
codedImageG(eta(:,:,27)>0.1) = 0;
codedImageB(eta(:,:,27)>0.1) = 0;
% make 26 in blue only
codedImageR(eta(:,:,26)>0.1) = 0;
codedImageG(eta(:,:,26)>0.1) = 0;
codedImage = zeros(size(codedImageR,1),size(codedImageR,2),3);
codedImage(:,:,1) = codedImageR;
codedImage(:,:,2) = codedImageG;
codedImage(:,:,3) = codedImageB;
codedImage(codedImage > 255) = 255;
codedImage(codedImage < 0) = 0;
imwrite(uint8(codedImage),'codedImage.png');
myImage = imread('codedImage.png');
x1D = -63.5:63.5;
y1D = -63.5:63.5;
figure('color','white');
image(x1D,y1D,myImage);
axis equal
axis tight
This code produces the following image:
And this is the figure:
  2 Kommentare
Amir Torabi
Amir Torabi am 8 Jan. 2020
Bearbeitet: Amir Torabi am 8 Jan. 2020
Thank for your help. However my aim is that bubble 26-27 and even more numbers, have a same color. For example, each of 26 and 27 have red color. And is there any ways to change the color of graymap into another color?
Meg Noah
Meg Noah am 9 Jan. 2020
See next answer.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Meg Noah
Meg Noah am 9 Jan. 2020
So the new constraints are:
  • change the background color from grayscale to single tone color scale
  • arbitrary set of bubbles to be same color
  • a couple of different bubble classes with a set of bubbles to be a particular color
This code lets you define the bubble classes and corresponding bubbles for special classes. There are three special classes in the example but it is flexible and extensible. The bubble assignments to each class are also flexible and extensible. Additional metrics could be implemented to classify bubbles. The background bubble color is also user-defined.
For the example: the background bubbles are light teal-blue. Bubbles 8, 26, and 27 are red. Bubbles 1 and 5 are green. Bubbles 6 and 7 are blue. The code is easy to edit to change background bubble color and to add or remove special bubble classes, change membership (indices of bubbles), and change their color.
clc
close all
clear all
load('eta.mat');
mag=1;
phi=sum(eta(:,:,:).^2,3);
graymap=1/(max(max(phi-min(min(phi)))))*(phi-min(min(phi)));
% most of the image (not special bubbles) will be this color in a grayscale
bkgColor = [0 0.5 1];
% special bubbles
% Color = R G B weights for the bubble class
% Bubbles = index into eta array for highlighted bubbles
highlight(1).Color = [1 0 0]; % type 1 bubbles
highlight(1).Bubbles = [8 26 27];
highlight(2).Color =[0 0.9 0]; % type 2 bubbles
highlight(2).Bubbles = [1 5];
highlight(3).Color =[0 0 1]; % type 3 bubbles
highlight(3).Bubbles = [6 7];
% threshold for determining a bubble region
thresh = 0.1;
[ny,nx] = size(graymap);
nClass = length(highlight);
% mask image for the special bubbles
maskSpecial = zeros(ny,nx,nClass);
maskBackground = ones(ny,nx);
for iClass = 1:length(highlight)
idxBubble = highlight(iClass).Bubbles;
tmpSpecial = zeros(ny,nx);
for iBubble = 1:length(idxBubble)
% add each special bubble to the temporary mask
tmpSpecial(eta(:,:,idxBubble(iBubble))>thresh) = 1;
end
maskSpecial(:,:,iClass) = tmpSpecial;
maskBackground(tmpSpecial > 0) = 0;
end
% scale between 0 and 255 for a 8-bit color image
graymap = 255*graymap;
graymap(graymap > 255) = 255;
graymap(graymap < 0) = 0;
% create the 3-band (RGB) image
codedImage = zeros(size(graymap,1),size(graymap,2),3);
for iBand = 1:3
codedImage(:,:,iBand) = bkgColor(iBand).*maskBackground.*graymap;
for iClass = 1:length(highlight)
codedImage(:,:,iBand) = codedImage(:,:,iBand) + ...
highlight(iClass).Color(iBand).*maskSpecial(:,:,iClass).*graymap;
end
end
codedImage(codedImage > 255) = 255;
codedImage(codedImage < 0) = 0;
imwrite(uint8(codedImage),'codedImage.png');
myImage = imread('codedImage.png');
x1D = -63.5:63.5;
y1D = -63.5:63.5;
figure('color','white');
image(x1D,y1D,myImage);
axis equal
axis tight
The code produces these images:
figureOfcodedImage2.png
  2 Kommentare
Amir Torabi
Amir Torabi am 11 Jan. 2020
Kindly appreciated.
Meg Noah
Meg Noah am 16 Jan. 2020
You're quite welcome. Thank you for accepting this answer.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by