export polygon data for yoloV8

73 views (last 30 days)
tim pearce
tim pearce on 4 Jan 2024
Commented: tim pearce on 4 Jan 2024
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

Answers (1)

Debraj Maji
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:
  1. Load the gTruth MATLAB table.
  2. For each labeled image in gTruth, extract the bounding box information.
  3. Normalize the bounding box coordinates.
  4. Write the normalized values to a text file with the same name as the image file.
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.
  1 Comment
tim pearce
tim pearce on 4 Jan 2024
wow thats fab thanks for coming back to me,
i'm specifically trying to get at the info stored in the "Image-store.png" as i understand it these are stored as per pixel classes i need to somehow turn these into polygons first and then write to the text file.
i'd had something roughly working to get the rectangle bounding box but your code is much neater. i originally used the image labeller app from the computer vision toolbox
thanks
Tim

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!