当前位置:网站首页>[200 opencv routines] 212 Draw a slanted rectangle
[200 opencv routines] 212 Draw a slanted rectangle
2022-06-27 09:38:00 【Xiaobai youcans】
OpenCV routine 200 piece General catalogue
【youcans Of OpenCV routine 200 piece 】212. Draw a slanted 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 .
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
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
routine A4.3: Draw a slanted rectangle on the image
cv.rectangle Only rectangles perpendicular to the boundary can be drawn on the image . If you need to draw a slanted rectangle , To obtain the coordinates of each vertex of the inclined rectangle , Construct a closed rectangle by drawing a straight line .
# A4.3 Draw a slanted rectangle on the image
height, width, channels = 600, 400, 3
img = np.ones((height, width, channels), np.uint8)*192 # Create a black image RGB=0
# Rotate around the center of the rectangle
x, y, w, h = (100, 200, 200, 100) # Top left coordinates (x,y), Width w, Height h
cx, cy = x+w//2, y+h//2 # Rectangular center
img1 = img.copy()
cv.circle(img1, (cx,cy), 4, (0,0,255), -1) # Center of rotation
angle = [15, 30, 45, 60, 75, 90] # Rotation Angle , clockwise
for i in range(len(angle)):
ang = angle[i] * np.pi / 180
x1 = int(cx + (w/2)*np.cos(ang) - (h/2)*np.sin(ang))
y1 = int(cy + (w/2)*np.sin(ang) + (h/2)*np.cos(ang))
x2 = int(cx + (w/2)*np.cos(ang) + (h/2)*np.sin(ang))
y2 = int(cy + (w/2)*np.sin(ang) - (h/2)*np.cos(ang))
x3 = int(cx - (w/2)*np.cos(ang) + (h/2)*np.sin(ang))
y3 = int(cy - (w/2)*np.sin(ang) - (h/2)*np.cos(ang))
x4 = int(cx - (w/2)*np.cos(ang) - (h/2)*np.sin(ang))
y4 = int(cy - (w/2)*np.sin(ang) + (h/2)*np.cos(ang))
color = (30*i, 0, 255-30*i)
cv.line(img1, (x1,y1), (x2,y2), color)
cv.line(img1, (x2,y2), (x3,y3), color)
cv.line(img1, (x3,y3), (x4,y4), color)
cv.line(img1, (x4,y4), (x1,y1), color)
# Rotate around the top left vertex of the rectangle
x, y, w, h = (200, 200, 200, 100) # Top left coordinates (x,y), Width w, Height h
img2 = img.copy()
cv.circle(img2, (x, y), 4, (0,0,255), -1) # Center of rotation
angle = [15, 30, 45, 60, 75, 90, 120, 150, 180, 225] # Rotation Angle , clockwise
for i in range(len(angle)):
ang = angle[i] * np.pi / 180
x1, y1 = x, y
x2 = int(x + w * np.cos(ang))
y2 = int(y + w * np.sin(ang))
x3 = int(x + w * np.cos(ang) - h * np.sin(ang))
y3 = int(y + w * np.sin(ang) + h * np.cos(ang))
x4 = int(x - h * np.sin(ang))
y4 = int(y + h * np.cos(ang))
color = (30 * i, 0, 255 - 30 * i)
cv.line(img2, (x1, y1), (x2, y2), color)
cv.line(img2, (x2, y2), (x3, y3), color)
cv.line(img2, (x3, y3), (x4, y4), color)
cv.line(img2, (x4, y4), (x1, y1), color)
plt.figure(figsize=(9, 6))
plt.subplot(121), plt.title("img1"), plt.axis('off')
plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB))
plt.subplot(122), plt.title("img2"), plt.axis('off')
plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB))
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
212. Draw a slanted rectangle
边栏推荐
- Markem Imaje Marken IMAS printer maintenance 9450e printer maintenance
- leetcode:968. 监控二叉树【树状dp,维护每个节点子树的三个状态,非常难想权当学习,类比打家劫舍3】
- 【OpenCV 例程200篇】211. 绘制垂直矩形
- Collection framework generic LinkedList TreeSet
- 隐私计算FATE-离线预测
- TDengine 邀请函:做用技术改变世界的超级英雄,成为 TD Hero
- Quartz(定时器)
- 手把手带你玩摄像头模组
- unity--newtonsoft.json解析
- Rockermq message sending and consumption mode
猜你喜欢

【生动理解】深度学习中常用的各项评价指标含义TP、FP、TN、FN、IoU、Accuracy

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

Imx8qxp DMA resources and usage (unfinished)

The markdown plug-in of the browser cannot display the picture

win10为任意文件添加右键菜单

Installation and use of SVN version controller

How do I get the STW (pause) time of a GC (garbage collector)?

Advanced mathematics Chapter 7 differential equations
![文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument](/img/08/2d4f425e6941af35616911672b6fed.png)
文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument

Rman-08137 main library failed to delete archive file
随机推荐
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
vector::data() 指针使用细节
Location and solution of network connection failure of primary online mobile terminal Report
Design of multiple classes
提高效率 Or 增加成本,开发人员应如何理解结对编程?
R语言plotly可视化:plotly可视化基础小提琴图(basic violin plot in R with plotly)
SVN版本控制器的安装及使用方法
Quartz(定时器)
最全H桥电机驱动模块L298N原理及应用
NoSQL database redis installation
技术与业务同等重要,偏向任何一方都是错误
Understand neural network structure and optimization methods
es 根据索引名称和索引字段更新值
There is no doubt that this is an absolutely elaborate project
ucore lab4
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy
分布式文件存储系统的优点和缺点
Some considerations on operation / method overloading for thread to release lock resources
Only one ConfirmCallback is supported by each RabbitTemplate 解决办法
快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])