如何在MATLAB中获取图像ROI坐标以适配R-CNN训练数据集
How to Get Bounding Box Coordinates in MATLAB for R-CNN Training
1. Use MATLAB's Image Labeler App (Manual Annotation)
This is the most efficient built-in tool for manual bounding box labeling, even for large datasets. Here's a step-by-step breakdown:
- Launch the app by typing
ImageLabelerin the MATLAB command window. - Click Add Images to import your entire dataset folder (no need to load images one by one).
- Create a label (e.g., "stop_sign") using the Add Label button—this ties all your bounding boxes to the target object.
- Select the Rectangle tool, then drag to draw boxes around every stop sign in each image.
- For faster annotation with large datasets:
- Use keyboard shortcuts:
nfor next image,pfor previous,deleteto remove a misplaced box. - Enable Auto-Progress in the app settings to automatically jump to the next image after you finish annotating the current one.
- Mark images with no stop signs as negative examples via the Label Image option—this helps the model learn to ignore non-target objects.
- Use keyboard shortcuts:
2. Export Bounding Box Coordinates
Once annotation is complete, export your labels and coordinates to use for training:
- In the Image Labeler, go to Export > Export Labels to Workspace.
- Name the output variable (e.g.,
stopSignGT) and click OK. This creates agroundTruthobject containing all your data. - To extract the raw coordinates and image paths, use these lines of code:
% Get list of image paths imagePaths = stopSignGT.ImageFilename; % Extract bounding boxes (each entry is a matrix of [xmin, ymin, width, height]) bboxes = stopSignGT.LabelData.stop_sign;
3. Prepare Data for R-CNN Training
You can directly use the groundTruth object to train your R-CNN detector, or format it into a text file like your example:
Option 1: Directly use with MATLAB's R-CNN training function
% Use a pre-trained base network (e.g., ResNet50) baseNet = resnet50; % Create R-CNN layers tailored to your image size and target label inputSize = size(imread(imagePaths(1))); layers = rcnnLayers(inputSize, "stop_sign", baseNet); % Train the detector detector = trainRCNNObjectDetector(stopSignGT, layers, ... "MiniBatchSize", 16, "MaxEpochs", 10);
Option 2: Save to a text file (matching your example format)
fileID = fopen('stop_sign_annotations.txt', 'w'); for i = 1:length(imagePaths) % Write image path fprintf(fileID, '%s', imagePaths(i)); % Write each bounding box for the image currentBoxes = bboxes{i}; for j = 1:size(currentBoxes, 1) fprintf(fileID, ' %.2f %.2f %.2f %.2f', ... currentBoxes(j,1), currentBoxes(j,2), ... currentBoxes(j,3), currentBoxes(j,4)); end fprintf(fileID, '\n'); end fclose(fileID);
4. Tips for Large Datasets
- Semi-Automated Annotation: Use the app's Automated Labeling feature (powered by pre-trained detectors) to generate initial bounding boxes, then refine them manually. This cuts down annotation time drastically.
- Batch Annotation: Split your dataset into smaller folders if it's massive, annotate one batch at a time, then merge the
groundTruthobjects later usingmergeGroundTruth. - Collaborate: If you have a team, export partial labels from each member and combine them to speed up the process.
内容的提问来源于stack exchange,提问作者AAA
相关产品推荐
相关产品推荐

