How can I export Roboflow annotation to work in Matlab
Show older comments
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?
2 Comments
Animesh
on 20 Aug 2024
The following MATLAB answer might help:
Luiz Augusto Meleiro
on 21 Aug 2024
Accepted Answer
More Answers (1)
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!

Categories
Find more on Parallel and Cloud in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
