rectangle opencv code example
Example 1: cv2.rectangle
import cv2
cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)
x1,y1 ------
| |
| |
| |
--------x2,y2
Example 2: python opencv draw rectangle
image = cv2.imread(path)
start_point = (5, 5)
end_point = (220, 220)
# Blue color in BGR
color = (255, 0, 0)
# Line thickness of 2 px
thickness = 2
# Using cv2.rectangle() method
# Draw a rectangle with blue line borders of thickness of 2 px
image = cv2.rectangle(image, start_point, end_point, color, thickness)
Example 3: draw rectangle opencv c++
int x = 0;
int y = 0;
int width = 10;
int height = 20;
cv::Rect rect(x, y, width, height);
cv::Point pt1(x, y);
cv::Point pt2(x + width, y + height);
cv::rectangle(img, pt1, pt2, cv::Scalar(0, 255, 0));
cv::rectangle(img, rect, cv::Scalar(0, 255, 0))