How can I export Roboflow annotation to work in Matlab

19 次查看(过去 30 天)
Hello! I'm using annotations to create bounding boxes on my images to train a model. To export the dataset created on Roboflow, we can export in different ways, like COCO segmentation Json files, or TXT YOLO oriented bounding boxes, or CSV tensorflow/ CSV keras. The downloaded files comes with the images and labels created in train, test and validation. So I have the images, the labels with coordinates from each image that I'm creating the Annotation. But I don't know how am I work with those files on Matlab. Anyone can help me with this problem?

采纳的回答

Rahul
Rahul 2024-8-21
I understand that you are trying to use the annotated data obtained from "Roboflow" in MATLAB.
One of the approaches you can utilize for your case is that you export the data in XML or JSON or CSV formats which can be converted easily to be used in MATLAB using functions like "xml2struct", "jsondecode", "csvread" as mentioned in this answer: https://www.mathworks.com/matlabcentral/answers/1833448-how-to-export-roboflow-annotation-to-mat-file
Another method that you can use is to use "insertObjectAnnotation" function where you can pass the images with their corresponding labels and annotation coordinates to obtain annotated images in MATLAB.
I = imread('test1.png');
position = [23,56,134,88];
RGB = insertObjectAnnotation(I,"rectangle",position,'l1',TextBoxOpacity=0.9,FontSize=18);
imshow(RGB)
% This is just an example
You can refer to the following documentations for the detailed instructions on how to use these functions:
Note: You can utilize the MATLAB Image Labeller app if required to annotate image in MATLAB
Hope this helps! Thanks.
  1 个评论
Luiz Augusto Meleiro
Hello, thanks for the information, but the annotations that we are creating is not rectangle, is more complicated shape. I attached the photos and the coordinates that are created is in the another attached, so I'm facing this challenge, how to export that data to matlab and create the same shape there. Hope you can help me again, thank you!

请先登录,再进行评论。

更多回答(1 个)

Kenneth Ligutom
Kenneth Ligutom 2024-9-18
编辑:Kenneth Ligutom 2024-9-18
For those who want rectangular ROIs in MATLAB, you can convert it from YOLO using the following code:
function gTruth = annotateFromYOLO(fileName, imgName)
% fileName - filename of annotation file
% imgName - name of the image reference
% Create the data source from the image name
dataSource = groundTruthDataSource(imgName);
% Define the labels
ldc = labelDefinitionCreator();
addLabel(ldc, 'GradeJK', 'Rectangle', 'Group', 'AbacaFibers', 'Description', 'Grade JK abaca fibers');
addLabel(ldc, 'GradeSI', 'Rectangle', 'Group', 'AbacaFibers', 'Description', 'Grade S-I abaca fibers');
addLabel(ldc, 'GradeSS2', 'Rectangle', 'Group', 'AbacaFibers', 'Description', 'Grade S-S2 abaca fibers');
labelDefs = create(ldc);
% Read YOLO data from the file
fileID = fopen(fileName, 'r');
formatSpec = '%f';
yoloData = fscanf(fileID, formatSpec);
fclose(fileID); % Close the file after reading
% Extract bounding box data
center_x = yoloData(2);
center_y = yoloData(3);
width = yoloData(4);
height = yoloData(5);
% Convert to [x, y, w, h] format
x = center_x - width / 2;
y = center_y - height / 2;
w = width;
h = height;
% Create the transposed bounding box data
transposedBBoxData = [x, y, w, h];
% Read the image to get dimensions
img = imread(imgName);
[imgHeight, imgWidth, ~] = size(img);
% Scale bounding box data based on image dimensions
% Assuming YOLO data is normalized between 0 and 1
% Convert normalized coordinates to pixel coordinates
bBoxDataScaled = transposedBBoxData .* [imgWidth, imgHeight, imgWidth, imgHeight];
% Create table with scaled bounding box data
labelNames = {labelDefs.Name{yoloData(1) + 1}};
labelData = table(bBoxDataScaled, 'VariableNames', labelNames);
% Create the ground truth object
gTruth = groundTruth(dataSource, labelDefs, labelData);
% Construct the .mat filename
[~, baseFileName, ~] = fileparts(fileName);
matFileName = strcat(baseFileName, '.mat');
% Save the ground truth object to a .mat file
save(matFileName, 'gTruth');
% Display the gTruth object to verify it was created successfully
disp(gTruth);
end
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by