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

3 views (last 30 days)
Anisha Jamil
Anisha Jamil on 9 Jul 2021
Edited: DGM on 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)

Answers (1)

DGM on 9 Jul 2021
Edited: DGM on 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
DGM on 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));
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{:}));
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by