You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 ImageLabeler in 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: n for next image, p for previous, delete to 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.

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 a groundTruth object 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 groundTruth objects later using mergeGroundTruth.
  • Collaborate: If you have a team, export partial labels from each member and combine them to speed up the process.

内容的提问来源于stack exchange,提问作者AAA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:42:17