当前位置:   article > 正文

OpenCV - C++ - cv::rectangle

cv::rectangle

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. 顶点,头顶,天顶
  • 1
  • 2

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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
//============================================================================
// 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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/801908
推荐阅读
相关标签
  

闽ICP备14008679号