欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenCV - C++ - cv::rectangle

程序员文章站 2022-05-21 09:46:13
...

OpenCV - C++ - cv::rectangle

https://docs.opencv.org/4.2.0/d6/d6e/group__imgproc__draw.html

1. cv::rectangle

C++
void cv::rectangle (InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
void cv::rectangle (InputOutputArray img, Rect rec, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)

Python
img = cv.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
img = cv.rectangle(img, rec, color[, thickness[, lineType[, shift]]])

Draws a simple, thick, or filled up-right rectangle.
绘制一个简单的,粗的或实心的直角矩形。

The function cv::rectangle draws a rectangle outline or a filled rectangle whose two opposite corners are pt1 and pt2.
函数 cv::rectangle 绘制一个矩形轮廓或一个填充的矩形,两个相对的角分别为 pt1 and pt2

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
这是一个重载的成员函数,为方便起见而提供。它与上面的函数的不同之处仅在于它接受什么参数。

use rec parameter as alternative specification of the drawn rectangle: r.tl() and r.br()-Point(1,1) are opposite corners
使用 rec 参数作为绘制矩形的替代规范:r.tl() and r.br()-Point(1,1) 位于对角

2. Parameters

img - Image.
pt1 - Vertex of the rectangle.
pt2 - Vertex of the rectangle opposite to pt1.
color - Rectangle color or brightness (grayscale image).
thickness - Thickness of lines that make up the rectangle. Negative values, like FILLED, mean that the function has to draw a filled rectangle. (组成矩形的线的粗细。负值 (如FILLED) 表示该函数必须绘制一个填充的矩形。)
lineType - Type of the line. See LineTypes
shift - Number of fractional bits in the point coordinates.

thickness [ˈθɪknəs]:n. 厚度,层,浓度,含混不清
vertex [ˈvɜːteks]:n. 顶点,头顶,天顶

3. Example

//============================================================================
// Name        : cv::rectangle
// Author      : Yongqiang Cheng
// Version     : Feb 22, 2020
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

int main(int argc, char** argv)
{
	// First create a black image.
	cv::Mat image(500, 500, CV_8UC3, cv::Scalar(0, 0, 0));

	// Check if the image is created successfully.
	if (!image.data)
	{
		std::cout << "Could not open or find the image" << std::endl;
		exit(EXIT_FAILURE);
	}

	// unfilled
	cv::Point p3(400, 400), p4(450, 450);
	cv::Scalar colorRectangle1(0, 0, 255);
	int thicknessRectangle1 = 3;

	cv::rectangle(image, p3, p4, colorRectangle1, thicknessRectangle1);

	//   filled
	cv::Point p5(100, 400), p6(150, 450);
	cv::Scalar colorRectangle2(255, 0, 0);

	cv::rectangle(image, p5, p6, colorRectangle2, cv::CV_FILLED);

	cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
	cv::imshow("Display window", image);

	cv::waitKey(0);

	return 0;
}

11:56:10 **** Incremental Build of configuration Debug for project DisplayImage ****
make all 
Building file: ../src/DisplayImage.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/local/include -I/usr/local/include/opencv4 -I/usr/local/include/opencv4/opencv2 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/DisplayImage.d" -MT"src/DisplayImage.o" -o "src/DisplayImage.o" "../src/DisplayImage.cpp"
../src/DisplayImage.cpp: In function ‘int main(int, char**)’:
../src/DisplayImage.cpp:37:48: error: ‘CV_FILLED’ is not a member of ‘cv’
  cv::rectangle(image, p5, p6, colorRectangle2, cv::CV_FILLED);
                                                ^
make: *** [src/DisplayImage.o] Error 1
src/subdir.mk:18: recipe for target 'src/DisplayImage.o' failed

11:56:11 Build Finished (took 1s.281ms)
//============================================================================
// Name        : cv::rectangle
// Author      : Yongqiang Cheng
// Version     : Feb 22, 2020
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

int main(int argc, char** argv)
{
	// First create a black image.
	cv::Mat image(500, 500, CV_8UC3, cv::Scalar(0, 0, 0));

	// Check if the image is created successfully.
	if (!image.data)
	{
		std::cout << "Could not open or find the image" << std::endl;
		exit(EXIT_FAILURE);
	}

	// unfilled
	cv::Point p3(400, 400), p4(450, 450);
	cv::Scalar colorRectangle1(0, 0, 255);
	int thicknessRectangle1 = 3;

	cv::rectangle(image, p3, p4, colorRectangle1, thicknessRectangle1);

	//   filled
	cv::Point p5(100, 400), p6(150, 450);
	cv::Scalar colorRectangle2(255, 0, 0);

	cv::rectangle(image, p5, p6, colorRectangle2, cv::FILLED);

	cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
	cv::imshow("Display window", image);

	cv::waitKey(0);

	return 0;
}

OpenCV - C++ - cv::rectangle