How to specify different colors to multiple shape inserts on an image?

4 Ansichten (letzte 30 Tage)
Anisha Jamil
Anisha Jamil am 9 Jul. 2021
Bearbeitet: DGM am 9 Jul. 2021
Hello, I have 14 different coordinates on an image and I'm trying to insert circular shapes on each of them with different colors according to the RGB color code column on an excel sheet. I used insertShape to insert circles, but I don't know how to specify different colors for each circles according to the colorcode.
I am also trying to assign values to them. The values are in the first column on the excel sheet and the color code is on the third.
I have the following code. Could someone please help me? Thanks in advance.
%%
%fixed is the image
%biopsy_list is the excel file
figure; imagesc(fixed);
pos_2=[u,v]; %coordinates
box_color = biopsy_list(:,3)
values_2= biopsy_list(:,1)
shape_2=insertShape(fixed,'circle',[pos_2,50*ones(size(pos_2,1),1)],'LineWidth',15,'Color',box_color);
fixed_2=insertText(shape_2,pos_2,values_2,'FontSize',60,'TextColor',box_color,'BoxOpacity',0);
figure;imagesc(fixed_2)

Antworten (1)

DGM
DGM am 9 Jul. 2021
Bearbeitet: DGM am 9 Jul. 2021
Again, I can't test this because I don't have CVT, but as I understand it, insertShape() should be able to accept multiple color specifications -- either a cell array of color names, or a RGB color table (mx3). As you're loading the color tuples from an external source, you'll probably want to make sure they're scaled appropriately. Judging by how the documentation says the color parameter supports both integer and floating point classes, I'm assuming that it will expect normalized values for floating point classes and full range for integer classes.
For example, if your color values are of class 'double', make sure they're normalized such that black corresponds to [0 0 0] and white corresponds to [1 1 1]. If your color values are of class 'uint8', the values should range between 0-255. If the range of color values doesn't correspond to what's expected of the class, your circles will look either all black or all white.
That said, I'm assuming these are RGB color tuples. You mention that they are all in one column. If they're formatted as strings (e.g. '255,255,255') then you might need to process the values to make them into a numeric matrix. What all is required depends on how they're formatted.
For example:
ctuple = '255,255,255'
ctuple = '255,255,255'
out = regexp(ctuple,'([^ ,:]*)','tokens');
out = cellfun(@str2double,horzcat(out{:}))
out = 1×3
255 255 255
  2 Kommentare
Anisha Jamil
Anisha Jamil am 9 Jul. 2021
Bearbeitet: Anisha Jamil am 9 Jul. 2021
They are all into a numeric matrix so it's in the correct format.
Like:
'[31 119 180]'
'[255 127 14]' ....
But I am getting the following error:
Error in Burn_2_practice (line 30)
shape_2=insertShape(fixed,'circle',[pos_2,50*ones(size(pos,1),1)],'LineWidth',15,'Color',box_color);
Error using insertShape
Expected Color to be one of these types:
cell
Instead its type was table.
Error in insertShape>checkColor (line 575)
validateattributes(color, {'cell'}, {}, 'insertShape', 'Color');
Error in insertShape>validateAndParseOptInputs_sim (line 343)
color = checkColor(parser.Results.Color, 'Color');
Error in insertShape>validateAndParseOptInputs (line 307)
validateAndParseOptInputs_sim(defaults, varargin{:});
Error in insertShape>validateAndParseInputs (line 249)
validateAndParseOptInputs(inpClass,varargin{:});
Error in insertShape (line 101)
validateAndParseInputs(I, shape, position, varargin{:});
Error in Burn_2_practice (line 29)
shape_2=insertShape(fixed,'circle',[pos_2,50*ones(size(pos,1),1)],'LineWidth',15,'Color',box_color);
DGM
DGM am 9 Jul. 2021
Bearbeitet: DGM am 9 Jul. 2021
If box_color is strictly numeric data in a table, you should just be able to do
box_color = table2array(box_color);
If it's a table of chars, something like this should be a start:
% build a random test table for demonstration
box_color = cell(10,1);
for c = 1:10
box_color{c} = sprintf('[%d %d %d]',randi(255,1),randi(255,1),randi(255,1));
end
box_color = cell2table(box_color);
% convert the table of chars to a numeric table
box_color2 = zeros(size(box_color,1),3);
for c = 1:size(box_color,1)
ctuple = cell2mat(table2array(box_color(c,:)));
ctuple = ctuple(ctuple~=']' & ctuple~='[');
out = regexp(ctuple,'([^ ,:]*)','tokens');
box_color2(c,:) = cellfun(@str2double,horzcat(out{:}));
end
box_color2
box_color2 = 10×3
60 155 66 237 223 215 213 135 232 31 176 111 154 1 43 81 13 12 81 214 65 158 234 83 18 116 252 122 54 108
That said, I don't really use tables and am hardly familiar with the canonical ways of handling them. The above can probably be simplified.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Image Processing Toolbox 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