当前位置:网站首页>[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
边栏推荐
- Apache POI的读写
- 小白也能看懂的网络基础 03 | OSI 模型是如何工作的(经典强推)
- I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
- Reading and writing Apache poi
- Freemarker
- ucore lab5
- Some exercises about binary tree
- Quelques exercices sur les arbres binaires
- 如何获取GC(垃圾回收器)的STW(暂停)时间?
- Installation and usage of source insight tool
猜你喜欢

E+H二次表维修PH变送器二次显示仪修理CPM253-MR0005

你睡觉时大脑真在自动学习!首个人体实验证据来了:加速1-4倍重放,深度睡眠阶段效果最好...

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

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

Markem imaje马肯依玛士喷码机维修9450E打码机维修
为智能设备提供更强安全保护 科学家研发两种新方法

Prometheus alarm process and related time parameter description

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

有关二叉树的一些练习题

ucore lab5
随机推荐
Tdengine invitation: be a superhero who uses technology to change the world and become TD hero
有關二叉樹的一些練習題
Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])
谷歌浏览器 chropath插件
【生动理解】深度学习中常用的各项评价指标含义TP、FP、TN、FN、IoU、Accuracy
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
TDengine 邀请函:做用技术改变世界的超级英雄,成为 TD Hero
别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
1098 Insertion or Heap Sort(堆排序解释)(PAT甲级)
.NET 中的引用程序集
ucore lab3
R language uses econcharts package to create microeconomic or macro-economic charts, demand function to visualize demand curve, and customize the parameters of demand function to enrich the visualizat
Preliminary understanding of pytorch
反编译jar包,修改后重新编译为jar包
Quelques exercices sur les arbres binaires
es 根据索引名称和索引字段更新值
[diffusion model]
Privacy computing fat offline prediction
How do I get the STW (pause) time of a GC (garbage collector)?
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