detect
Detect objects using ACF object detector
Syntax
Description
detects objects within all the images returned by the detectionResults
= detect(detector
,ds
)read
function
of the input datastore.
[___] = detect(___,
specifies options using one or more name-value arguments in addition to any combination of
arguments from previous syntaxes. For example, Name=Value
)WindowStride=2
sets the
stride of the sliding window used to detects objects to 2
.
Examples
Train Stop Sign Detector Using ACF Object Detector
Use the trainACFObjectDetector
with training images to create an ACF object detector that can detect stop signs. Test the detector with a separate image.
Load the training data.
load('stopSignsAndCars.mat')
Prefix the full path to the stop sign images.
stopSigns = fullfile(toolboxdir('vision'),'visiondata',stopSignsAndCars{:,1});
Create datastores to load the ground truth data for stop signs.
imds = imageDatastore(stopSigns); blds = boxLabelDatastore(stopSignsAndCars(:,2));
Combine the image and box label datastores.
ds = combine(imds,blds);
Train the ACF detector. Set the number of negative samples to use at each stage to 2
. You can turn off the training progress output by specifying Verbose=false,
as a Name-Value
argument.
acfDetector = trainACFObjectDetector(ds,NegativeSamplesFactor=2);
ACF Object Detector Training The training will take 4 stages. The model size is 34x31. Sample positive examples(~100% Completed) Compute approximation coefficients...Completed. Compute aggregated channel features...Completed. -------------------------------------------- Stage 1: Sample negative examples(~100% Completed) Compute aggregated channel features...Completed. Train classifier with 42 positive examples and 84 negative examples...Completed. The trained classifier has 19 weak learners. -------------------------------------------- Stage 2: Sample negative examples(~100% Completed) Found 84 new negative examples for training. Compute aggregated channel features...Completed. Train classifier with 42 positive examples and 84 negative examples...Completed. The trained classifier has 20 weak learners. -------------------------------------------- Stage 3: Sample negative examples(~100% Completed) Found 84 new negative examples for training. Compute aggregated channel features...Completed. Train classifier with 42 positive examples and 84 negative examples...Completed. The trained classifier has 54 weak learners. -------------------------------------------- Stage 4: Sample negative examples(~100% Completed) Found 84 new negative examples for training. Compute aggregated channel features...Completed. Train classifier with 42 positive examples and 84 negative examples...Completed. The trained classifier has 61 weak learners. -------------------------------------------- ACF object detector training is completed. Elapsed time is 16.0231 seconds.
Test the ACF detector on a test image.
img = imread('stopSignTest.jpg');
[bboxes,scores] = detect(acfDetector,img);
Display the detection results and insert the bounding boxes for objects into the image.
for i = 1:length(scores) annotation = sprintf('Confidence = %.1f',scores(i)); img = insertObjectAnnotation(img,'rectangle',bboxes(i,:),annotation); end figure imshow(img)
Input Arguments
detector
— ACF object detector
acfObjectDetector
object
ACF object detector, specified as an acfObjectDetector
object. To create this object, call the trainACFObjectDetector
function with training data as input.
I
— Input image
grayscale image | RGB image
Input image, specified as a real, nonsparse, grayscale or RGB image.
Data Types: uint8
| uint16
| int16
| double
| single
ds
— Datastore
datastore
object
Datastore, specified as a datastore
object containing a
collection of images. Each image must be a grayscale or RGB. The function processes
only the first column of the datastore, which must contain images and must be cell
arrays or tables with multiple columns. Therefore, datastore read
function must return image data in the first column.
roi
— Search region of interest
[x y width height] vector
Search region of interest, specified as an [x y width height] vector. The vector specifies the upper left corner and size of a region in pixels.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: WindowStride=2
sets the stride of the sliding window used to
detects objects to 2
.
NumScaleLevels
— Number of scale levels per octave
8
(default) | positive integer
Number of scale levels per octave, specified a positive integer. Each octave is a power-of-two downscaling of the image. To detect people at finer scale increments, increase this number. Recommended values are in the range [4, 8].
WindowStride
— Stride for sliding window
4
(default) | positive integer
Stride for the sliding window, specified as a positive integer. This value indicates the distance for the function to move the window in both the x and y directions. The sliding window scans the images for object detection.
SelectStrongest
— Select strongest bounding box for each object
true
(default) | false
Select the strongest bounding box for each detected object, specified as
true
or false
.
true
— Return the strongest bounding box per object. To select these boxes,detect
calls theselectStrongestBbox
function, which uses nonmaximal suppression to eliminate overlapping bounding boxes based on their confidence scores.false
— Return all detected bounding boxes. You can then create your own custom operation to eliminate overlapping bounding boxes.
MinSize
— Minimum region size
[height width] vector
Minimum region size that contains a detected object, specified as a vector of the form [height width]. Units are in pixels.
By default, MinSize
is the smallest object that the trained detector
can detect.
MaxSize
— Maximum region size
size
(I
) (default) | [height width] vector
Maximum region size that contains a detected object, specified as a vector of the form [height width]. Units are in pixels.
To reduce computation time, set this value to the known maximum region size for the objects
being detected in the image. By default, 'MaxSize'
is set to
the height and width of the input image, I
.
Threshold
— Classification accuracy threshold
–1
(default) | numeric scalar
Classification accuracy threshold, specified as a numeric scalar. Recommended values are in the range [–1, 1]. During multiscale object detection, the threshold value controls the accuracy and speed for classifying image subregions as either objects or nonobjects. To speed up the performance at the risk of missing true detections, increase this threshold.
Output Arguments
bboxes
— Location of objects detected within image
M-by-4 matrix
Location of objects detected within the input image, returned as an M-by-4
matrix, where M is the number of bounding boxes. Each row of
bboxes
contains a four-element vector of the form
[x
y
width
height]. This vector specifies the upper left corner and size
of that corresponding bounding box in pixels.
scores
— Detection confidence scores
M-by-1 vector
Detection confidence scores, returned as an M-by-1 vector,
where M is the number of bounding boxes. Scores are returned
in the range [-inf
inf
]. A higher score indicates higher confidence in the
detection.
detectionResults
— Detection results
3-column table
Detection results, returned as a 3-column table with variable names, Boxes, Scores, and Labels. The Boxes column contains M-by-4 matrices, of M bounding boxes for the objects found in the image. Each row contains a bounding box as a 4-element vector in the format [x,y,width,height]. The format specifies the upper-left corner location and size in pixels of the bounding box in the corresponding image.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
To generate code, use
toStruct
function to pass theacfObjectDetector
object to thedetect
function. For more information, see Generate Code for Detecting Objects in Images by Using ACF Object Detector.
Version History
Introduced in R2017a
See Also
Apps
Functions
Objects
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)