- Load the gTruth MATLAB table.
- For each labeled image in gTruth, extract the bounding box information.
- Normalize the bounding box coordinates.
- Write the normalized values to a text file with the same name as the image file.
export polygon data for yoloV8
11 views (last 30 days)
Show older comments
Hi all,
I've segmented and labeled a large collection of images in MATLAB Image Labeler,
so i have the gTruth file and also a png for each image that contains the plygon info for each catagory.
I need to export this to a text file for use with YoloV8, I'm using 2018b i cant seam to find any prebuilt functions to achive this directly
Kind Regards
Tim
0 Comments
Answers (1)
Debraj Maji
on 4 Jan 2024
I understand that you are trying to export the ground truth file as a text file. As of MATLAB R2018b, Image Labeler doesn't have any inbuilt function for exporting the ground truth file in text format. The YOLOv8 model expects each image to have a corresponding text file with the same name, where each line in the text file represents one object's bounding box in the following format:
<object-class> <x_center> <y_center> <width> <height>
You can follow these steps to create a script for converting your gTruth data:
Here is the sample code for exporting Labels to a text file:
% Load the ground truth data
data = load('your_file_name.mat');
gTruth = data.gTruth;
% Get the list of image file names
imageFilenames = gTruth.DataSource.Source;
% Get the label data
labelData = gTruth.LabelData;
% Loop over each image
for i = 1:length(imageFilenames)
% Get the image size
img = imread(imageFilenames{i});
[imgHeight, imgWidth, ~] = size(img);
% Get the bounding boxes for the current image
bboxes = table2array(labelData(i,:));
% Open the text file for writing
[filepath, name, ext] = fileparts(imageFilenames{i});
txtFilename = fullfile(filepath, [name '.txt']);
fileID = fopen(txtFilename, 'w');
% Loop over each bounding box for the current image
for j = 1:size(bboxes, 1)
bbox = bboxes{j, :};
% Skip if no bounding box
if isempty(bbox)
continue;
end
% Normalize the bounding box coordinates
x_center = (bbox(1) + bbox(3) / 2) / imgWidth;
y_center = (bbox(2) + bbox(4) / 2) / imgHeight;
width = bbox(3) / imgWidth;
height = bbox(4) / imgHeight;
% Get the class index (assuming class labels are integers)
classIndex = j - 1; % Adjust based on your class labels
% Write to the text file
fprintf(fileID, '%d %.6f %.6f %.6f %.6f\n', classIndex, x_center, y_center, width, height);
end
% Close the text file
fclose(fileID);
end
Please note that the above code is for rectangular bounding boxes only. For other configurations please modify the code accordingly.
For more information on the Image Labeler you can visit the following documentation:
Hope this helps,
Regards,
Debraj.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!