rectToBbox
Compute bounding boxes from outputs returned by MATLAB interface object for OpenCV Rect
class
Since R2021b
Syntax
Description
Examples
Draw Rectangle on Image Using OpenCV in MATLAB
Draw a rectangle on an image by using the prebuilt MATLAB interface for the OpenCV function cv::rectangle
. Compute the corresponding bounding box values in MATLAB to use to crop the image region.
Add the MATLAB interface to OpenCV package names to the import list.
import clib.opencv.*; import vision.opencv.util.*;
Read an image into the MATLAB workspace.
img = imread("highway.png");
Create interface objects for the OpenCV Mat
and InputOutputArray
classes to store the input image.
[inputMat,ocvArray] = createMat(img,"InputOutput");
Define Rectangular Region
Call the OpenCV function cv::Rect2i
using MATLAB, and specify the coordinates and dimensions for the rectangle. Display the values.
rec = cv.Rect2i(140,60,100,100)
rec = Rect2i with properties: x: 140 y: 60 width: 100 height: 100
Draw Rectangle and Display Results
Specify the properties of the line to use for drawing the rectangle. Set these values:
Color of the line to red. To set this value, use the OpenCV function
cv::Scalar
.Thickness of the line to
2
.Line type to
4
.Shift to
0
.
color = cv.Scalar(255,0,0); thickness = 2; lineType = 4; shift = 0;
Draw the defined rectangle on the image by using the OpenCV function cv::rectangle
.
cv.rectangle(ocvArray,rec,color,thickness,lineType,shift);
Read and display the output image.
outputImg = getImage(ocvArray); figure imshow(outputImg)
Compute Bounding Box
Use the utility function rectToBbox
to compute the bounding box value from the values returned by OpenCV function cv::Rect2i
.
bbox = rectToBbox(rec);
Display the bounding box values. Notice that the bounding box values in the MATLAB workspace have one-based indexing.
bbox
bbox = 1x4 int32 row vector
141 61 100 100
Crop the region within the bounding box by using the imcrop
function and display the cropped image.
croppedImg = imcrop(img,bbox); figure imshow(croppedImg)
Input Arguments
input
— OpenCV class for rectangles
Rect__unsignedChar_
interface object | Rect2d
interface object | Rect2f
interface object | Rect2i
interface object
OpenCV class for rectangles, specified as one of these values:
Rect__unsignedChar_
interface object — This MATLAB interface object is a representation of the OpenCV classcv::Rect_<unsigned char>
.Rect2d
interface object — This MATLAB interface object is a representation of the OpenCV classcv::Rect_<double>
.Rect2f
interface object — This MATLAB interface object is a representation of the OpenCV classcv::Rect_<float>
.Rect2i
interface object — This MATLAB interface object is a representation of the OpenCV classcv::Rect_<int>
.Rect2i
interface object — This MATLAB interface object is a representation of the OpenCV classcv::Rect_<int>
.
Output Arguments
bbox
— Bounding box values
four-element row vector
Bounding box values, returned as a four-element row vector of the form
[x
y
width
height
].
Version History
Introduced in R2021b
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de 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)