当前位置:网站首页>[200 opencv routines] 211 Draw vertical rectangle
[200 opencv routines] 211 Draw vertical rectangle
2022-06-27 09:38:00 【Xiaobai youcans】
OpenCV routine 200 piece General catalogue
【youcans Of OpenCV routine 200 piece 】211. Draw vertical rectangle
7.1 Basic parameters of drawing function
OpenCV It provides drawing function , You can draw a line on the image 、 rectangular 、 round 、 Ellipse and other geometric figures .
function cv.line()、cv.rectangle()、cv.circle()、cv.polylines() And so on are used to draw straight lines in the image 、 rectangular 、 circular 、 Polygon and other geometric shapes , There are some setting parameters in these drawing functions , Introduce the following :
- img: Input / output image , Format is unlimited
- color: The color of the drawing line ,(b,g,r) Tuple of format , Or a scalar representing the gray value
- thickness: Draw the thickness of the line , The default value is 1px,-1 Indicates internal padding
- lineType: Draw the linear of the line segment , The default is LINE_8
- cv.FILLED: Inside filling ( Solid graphics )
- cv.LINE_4:4 Adjacency Linetype
- cv.LINE_8:8 Adjacency Linetype
- cv.LINE_AA: Anti aliasing , Smoother image
- shift: The number of decimal places of point coordinates , The default is 0
7.3 Draw a rectangle
The function prototype :
function cv.rectangle() Used to draw a rectangle perpendicular to the image boundary on the image .
cv.rectangle(img, pt1, pt2, color[, thickness=1, lineType=LINE_8, shift=0]) → img
cv.rectangle(img, rec, color[, thickness=1, lineType=LINE_8, shift=0]) → img
Parameter description :
- img: Input / output image , Allows single channel grayscale images or multi-channel color images
- pt1: Coordinates of the first point of the matrix ,(x1, y1) Tuple of format
- pt2: And pt1 Coordinates of the second point of the diagonal matrix ,(x2, y2) Tuple of format
- color: The color of the drawing line ,(b,g,r) Tuple of format , Or a scalar representing the gray value
- thickness: Draw the line width of the rectangle , The default value is 1px, A negative number means that the inside of the rectangle is filled
- lineType: Draw the linear of the line segment , The default is LINE_8
- shift: The number of decimal places of point coordinates , The default is 0
matters needing attention :
- The drawing operation will be performed directly on the incoming image img Make changes , Whether to accept the return value of the function or not . If you want to keep the input image unchanged, use img.copy() replicate .
- Use the opposite corner of the rectangle pt1、pt2 Draw a rectangle .pt1、pt2 Order independent and interchangeable , It can be top left 、 Lower right corner , It can also be the lower left 、 Upper right corner .
- If the diagonal coordinates exceed the image boundary , The drawn rectangle is clipped by the image boundary , That is, only part of the rectangular border within the image boundary is drawn .
- Draw on a color image , line color color You can tuple (b,g,r) Express , Such as (0,0,255) It means red ; It can also be scalar b, But it doesn't mean grayscale lines , It means color (b,0,0).
- Only gray lines can be drawn on a single channel gray image , Cannot draw colored lines . however , line color color It can be scalar b, It can also be tuples (b,g,r), Will be interpreted as grayscale values b. The parameters of the last two channels in the tuple are invalid .
- When drawing a filled rectangle , Recommended choice thickness=-1 Set up , key word “thickness” It can be omitted .
- In some applications , Use (x , y, x+w, y+h) Define the coordinates of the diagonal point , Be careful (x,y) Must be The top left corner of the rectangle coordinate .
- Use
recParameter draw rectangle ,r.tl()andr.br()Is the diagonal of the rectangle .
Rect Rectangle class :
stay OPenCV/C++ It defines Rect class , It is called rectangle class , contain Point Member variables of class x、y( rectangular Top left vertex coordinate ) and Size Member variables of class width and height( The width and height of the rectangle ).
stay OpenCV/Python in , You can't create... Directly Rect Rectangle class . But some internal functions use or return Rect, Such as cv.boundingRect.
rect = cv.boundingRect(contours[c])
cv.rectangle(img, (rect[0], rect[1]), (rect[0]+rect[2], rect[1]+rect[3]), (0,0,255))
rect stay C++ Is returned Rect Rectangle class , have access to rect.tl() and rect.br() Returns the coordinates of the upper left and lower right corners . And in the python What's back is 4 Tuples of elements (x , y, w, h), respectively Top left vertex Coordinates of (x,y)、 The width of the rectangle w And height h.
routine A4.2: Draw a vertical rectangle on the image
# A4.2 Draw a vertical rectangle on the image
height, width, channels = 400, 300, 3
img = np.ones((height, width, channels), np.uint8)*160 # Create a black image RGB=0
img1 = img.copy()
cv.rectangle(img1, (0,20), (100,200), (255,255,255)) # white
cv.rectangle(img1, (20,0), (300,100), (255,0,0), 2) # Blue B=255
cv.rectangle(img1, (300,400), (250,300), (0,255,0), -1) # green , fill
cv.rectangle(img1, (0,400), (50,300), 255, -1) # color=255 Blue
cv.rectangle(img1, (20,220), (25,225), (0,0,255), 4) # Influence of line width
cv.rectangle(img1, (60,220), (67,227), (0,0,255), 4)
cv.rectangle(img1, (100,220), (109,229), (0,0,255), 4)
img2 = img.copy()
x, y, w, h = (50, 50, 200, 100) # Top left coordinates (x,y), Width w, Height h
cv.rectangle(img2, (x,y), (x+w,y+h), (0,0,255), 2)
text = "({},{}),{}*{}".format(x, y, w, h)
cv.putText(img2, text, (x, y-5), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255))
# Drawing lines can be used for grayscale images , Parameters color Only the first channel value is valid , And is set to the gray value
gray = np.zeros((height, width), np.uint8) # Create a grayscale image
img3 = cv.line(gray, (0,10), (300,10), 64, 2)
cv.line(img3, (0,30), (300,30), (128,128,255), 2)
cv.line(img3, (0,60), (300,60), (192,64,255), 2)
cv.rectangle(img3, (0,200), (30,150), 128, -1) # Gray=128
cv.rectangle(img3, (60,200), (90,150), (128,0,0), -1) # Gray=128
cv.rectangle(img3, (120,200), (150,150), (128,255,255), -1) # Gray=128
cv.rectangle(img3, (180,200), (210,150), 192, -1) # Gray=192
cv.rectangle(img3, (240,200), (270,150), 255, -1) # Gray=255
plt.figure(figsize=(9, 6))
plt.subplot(131), plt.title("img1"), plt.axis('off')
plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB))
plt.subplot(132), plt.title("img2"), plt.axis('off')
plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB))
plt.subplot(133),plt.title("img3"), plt.axis('off')
plt.imshow(img3, cmap="gray")
plt.tight_layout()
plt.show()
Routine results :

【 At the end of this section 】
Copyright notice :
reference : Use the Photoshop Levels adjustment (adobe.com)
[email protected] Original works , Reprint must be marked with the original link :(https://blog.csdn.net/youcans/article/details/125432101)
Copyright 2022 youcans, XUPT
Crated:2022-6-20
Welcome to your attention 『youcans Of OpenCV routine 200 piece 』 series , Ongoing update
Welcome to your attention 『youcans Of OpenCV Learning lessons 』 series , Ongoing update
210. There are so many holes in drawing a straight line ?
211. Draw vertical rectangle
边栏推荐
- 有关二叉树的一些练习题
- The background prompt module for accessing fastadmin after installation does not exist
- C# Any()和AII()方法
- Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])
- Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介
- R语言使用econocharts包创建微观经济或宏观经济图、demand函数可视化需求曲线(demand curve)、自定义配置demand函数的参数丰富可视化效果
- Obsidian 一周使用心得(配置、主题和插件)
- vector::data() 指针使用细节
- Pakistani security forces killed 7 terrorists in anti-terrorism operation
- 快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])
猜你喜欢

Process 0, process 1, process 2

Your brain is learning automatically when you sleep! Here comes the first human experimental evidence: accelerate playback 1-4 times, and the effect of deep sleep stage is the best

There is no doubt that this is an absolutely elaborate project

Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介

【系统设计】邻近服务
![[system design] proximity service](/img/02/57f9ded0435a73f86dce6eb8c16382.png)
[system design] proximity service

Imx8qxp DMA resources and usage (unfinished)

Getting started with webrtc: 12 Rtendpoint and webrtcendpoint under kurento

Markem Imaje Marken IMAS printer maintenance 9450e printer maintenance

高等数学第七章微分方程
随机推荐
leetcode:522. 最长特殊序列 II【贪心 + 子序列判断】
Understand neural network structure and optimization methods
C# Any()和AII()方法
.NET 中的引用程序集
【OpenCV 例程200篇】212. 绘制倾斜的矩形
ucore lab4
JS 文件上传下载
【SO官方采访】为何使用Rust的开发者如此深爱它
Video file too large? Use ffmpeg to compress it losslessly
Vector:: data() pointer usage details
QT运行显示 This application failed to start because it could not find or load the Qt platform plugin
Your brain is learning automatically when you sleep! Here comes the first human experimental evidence: accelerate playback 1-4 times, and the effect of deep sleep stage is the best
The background prompt module for accessing fastadmin after installation does not exist
【报名】基础架构设计:从架构热点问题到行业变迁 | TF63
Design of multiple classes
Privacy computing fat offline prediction
[system design] proximity service
Parameters argc and argv of main()
R语言使用econocharts包创建微观经济或宏观经济图、demand函数可视化需求曲线(demand curve)、自定义配置demand函数的参数丰富可视化效果
Summary of three basic interview questions