How to assign different color for different classes in object detection model?

Greetings, I have two classes, which are labelled as normal and cancer cells. How to draw bounding boxes with different colors that referred to each class?
newp2 = imread([pathname filename]);
[bboxes,scores,labels] = detect(detector,newp2,'Threshold',0.7);
annotations = string(labels) + ": " + string(scores);
I = insertObjectAnnotation(newp2,'rectangle',bboxes,cellstr(annotations));
figure
imshow(I)

回答 (1 件)

Sachin
Sachin 2023 年 4 月 13 日
Hi
I understand that you want to assign the different color for different classes in object detection model.
To draw bounding boxes with different colors for each class, you can modify the ‘Color’ parameter based on the class label,
Here is an example:
newp2 = imread([pathname filename]);
[bboxes,scores,labels] = detect(detector,newp2,'Threshold',0.7);
annotations = string(labels) + ": " + string(scores);
%loop through each detected object
for i=1:numel(bboxes)
label = labels(i);
bbox = bboxes(i,:);
score = scores(i);
if label=='inormal'
color = 'b'
elseif label == 'cancer'
color = 'r'
else
color = 'g'
insertObjectAnnotation(newp2,'rectangle',bboxes,cellstr(annotations),'Color',color);
end
Please refer the following page for more information about ‘insertobjectannotation’

5 件のコメント

Fahmi Akmal Dzulkifli
Fahmi Akmal Dzulkifli 2023 年 4 月 13 日
First of all, thank you Sir for the respond. When I execute the code, there is an error, where the index exceeds the number of array elements. For example, the bboxes is equal to (219x4 single), but the i is equal to 220. How can I correct it?
Sachin
Sachin 2023 年 4 月 13 日
Ok Then just try finding the number of rows in bboxes in this case 219. So replace the numel with number of rows
I already tried it but the values for 'label' and 'labels are not same.The 'label' has (1x1 categorical) while the 'labels' has (219x1 categorical). Can you identify the error?
[filename, pathname] = uigetfile(('*.jpg;*.png;*.tiff;*.bmp,'));
newp2 = imread([pathname filename]);
[bboxes,scores,labels] = detect(detector,newp2,'Threshold',0.8);
annotations = string(labels) + ": " + string(scores);
%loop through each detected object
no_rows = height(bboxes);
for i=1:no_rows
label = labels(i);
bbox = bboxes(i,:);
score = scores(i);
if label=='Cancer'
color = 'red';
elseif label == 'Normal'
color = 'green';
I = insertObjectAnnotation(newp2,'rectangle',bboxes,cellstr(annotations),'Color',color);
end
end
figure; imshow(I);
Sachin
Sachin 2023 年 4 月 22 日
Hi
Can you please tell what kind of error you are getting?
Fahmi Akmal Dzulkifli
Fahmi Akmal Dzulkifli 2023 年 4 月 28 日
The error is the colour of the bounding boxes are same for all cells. Like in the above example, all the cells have green bounding boxes colour, includes the cancer cells. When I checked at the 'labels', it has two classes, but when to assign the colour, it only has one class only.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGenomics and Next Generation Sequencing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by