当前位置:网站首页>[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
边栏推荐
- Only one ConfirmCallback is supported by each RabbitTemplate 解决办法
- Rman-08137 main library failed to delete archive file
- Five page Jump methods for wechat applet learning
- Nosql 数据库 -Redis 安装
- Demand visual Engineer
- Design of multiple classes
- E+H二次表维修PH变送器二次显示仪修理CPM253-MR0005
- 小白也能看懂的网络基础 03 | OSI 模型是如何工作的(经典强推)
- R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行center中心化(每个数据列减去平均值)、设置method参数为center
- 你睡觉时大脑真在自动学习!首个人体实验证据来了:加速1-4倍重放,深度睡眠阶段效果最好...
猜你喜欢

One week's experience of using Obsidian (configuration, theme and plug-in)

Installation and usage of source insight tool

Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher

基于STM32设计的蓝牙健康管理设备

Decompile the jar package and recompile it into a jar package after modification

【报名】基础架构设计:从架构热点问题到行业变迁 | TF63

Privacy computing fat offline prediction

视频文件太大?使用FFmpeg来无损压缩它

快速入门CherryPy(1)

leetcode:968. 监控二叉树【树状dp,维护每个节点子树的三个状态,非常难想权当学习,类比打家劫舍3】
随机推荐
es 根据索引名称和索引字段更新值
Prometheus alarm process and related time parameter description
借助原子变量,使用CAS完成并发操作
Design of multiple classes
多個類的設計
Process 0, process 1, process 2
Preliminary understanding of pytorch
R语言plotly可视化:plotly可视化二维直方图等高线图、在等高线上添加数值标签、自定义标签字体色彩、设置鼠标悬浮显示效果(Styled 2D Histogram Contour)
Quelques exercices sur les arbres binaires
集合框架 泛型LinkedList TreeSet
【OpenCV 例程200篇】212. 绘制倾斜的矩形
隐私计算FATE-离线预测
Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
有關二叉樹的一些練習題
R语言使用econocharts包创建微观经济或宏观经济图、demand函数可视化需求曲线(demand curve)、自定义配置demand函数的参数丰富可视化效果
R语言plotly可视化:plotly可视化基础小提琴图(basic violin plot in R with plotly)
Some exercises about binary tree
JS 客户端存储
MySQL proficient-01 addition, deletion and modification
js的数组拼接「建议收藏」