当前位置:网站首页>[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
边栏推荐
- Rockermq message sending and consumption mode
- Digital ic-1.9 understands the coding routine of state machine in communication protocol
- R language plot visualization: visualize the normalized histograms of multiple data sets, add density curve KDE to the histograms, set different histograms to use different bin sizes, and add edge whi
- Rockermq message sending mode
- 使用Aspose.cells将Excel转成PDF
- 快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])
- Video file too large? Use ffmpeg to compress it losslessly
- vector::data() 指针使用细节
- 不容置疑,这是一个绝对精心制作的项目
- 为智能设备提供更强安全保护 科学家研发两种新方法
猜你喜欢
【OpenCV 例程200篇】211. 绘制垂直矩形
Understand neural network structure and optimization methods
NoSQL database redis installation
.NET 中的引用程序集
隐私计算FATE-离线预测
Quick start CherryPy (1)
1098 insertion or heap sort (PAT class a)
Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])
Scientists develop two new methods to provide stronger security protection for intelligent devices
Take you to play with the camera module
随机推荐
【生动理解】深度学习中常用的各项评价指标含义TP、FP、TN、FN、IoU、Accuracy
The largest rectangle in the bar graph of force buckle 84
CLassLoader
R语言plotly可视化:可视化多个数据集归一化直方图(historgram)并在直方图中添加密度曲线kde、设置不同的直方图使用不同的分箱大小(bin size)、在直方图的底部边缘添加边缘轴须图
【OpenCV 例程200篇】212. 绘制倾斜的矩形
Quelques exercices sur les arbres binaires
Use aspese slides to convert PPT to PDF
vector::data() 指针使用细节
快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])
js中的数组对象
借助原子变量,使用CAS完成并发操作
小哥凭“量子速读”绝技吸粉59万:看街景图0.1秒,“啪的一下”在世界地图精准找到!...
Only one confirmcallback is supported by each rabbittemplate
Imx8qxp DMA resources and usage (unfinished)
Static code block vs construction code block
Nosql 数据库 -Redis 安装
文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument
js的数组拼接「建议收藏」
Flow chart of Alipay wechat payment business
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!