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?
2 个评论
采纳的回答
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:
"xmlread": https://www.mathworks.com/help/matlab/ref/xmlread.html#mw_9a3a5305-86fd-41b1-a9b0-8113f0653321
"jsondecode": https://www.mathworks.com/help/releases/R2024a/matlab/ref/jsondecode.html?searchHighlight=jsondecode&s_tid=doc_srchtitle
"insertObjectAnnotation": https://www.mathworks.com/help/vision/ref/insertobjectannotation.html#d126e276766
Note: You can utilize the MATLAB Image Labeller app if required to annotate image in MATLAB
Hope this helps! Thanks.
更多回答(1 个)
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!