上述代码使用python代码
时间: 2025-05-26 18:21:33 浏览: 18
要在 Python 中利用 OpenCV 绘制一个黄色线条的五角星,并且背景为白色、五角星内部未填充,可以按照以下方法实现:
---
### 1. 创建空白图像并设定背景颜色
首先需要创建一幅尺寸合适的空白图像,并将其初始化为纯白背景。
```python
import cv2
import numpy as np
# 创建一张800x800像素的三通道图片,初始化为黑色
img = np.zeros((800, 800, 3), dtype=np.uint8)
# 设置背景颜色为白色 (B,G,R)=(255,255,255)
img.fill(255) # 或者 img[:] = [255, 255, 255]
```
这里的 `np.zeros` 函数生成了一幅初始全黑的矩阵,而通过 `.fill(255)` 方法或者切片赋值 `[255, 255, 255]` 可以将所有像素值替换为白色[^1]。
---
### 2. 计算五角星顶点坐标
为了绘制五角星形状,需先定义其五个主要顶点的位置。这通常基于极坐标系来计算各点相对于中心位置的角度偏移与半径长度的关系。
假设五角星中心位于 `(cx, cy)` ,外接圆半径为 `R` 。则可通过如下方式获取各个顶点的具体二维坐标:
```python
def calculate_pentagram_points(center_x, center_y, radius):
points = []
for i in range(5):
angle_deg = 90 + i * 72 - 18 # 起始方向调整和间隔角度
angle_rad = np.deg2rad(angle_deg)
x = int(center_x + radius * np.cos(angle_rad))
y = int(center_y - radius * np.sin(angle_rad)) # 注意 y 坐标反向
points.append((x, y))
return points
center_x, center_y = 400, 400 # 中心坐标
radius = 300 # 外接圆半径
pentagram_points = calculate_pentagram_points(center_x, center_y, radius)
```
此部分代码实现了根据给定的中心点和半径自动计算五角星顶点的功能[^2]。
---
### 3. 连接顶点形成五角星图案
有了前面准备好的一系列离散点之后,就可以借助 OpenCV 的 `cv2.line()` 接口把这些点两两相连从而勾勒出完整的星星轮廓来了。特别注意的是,在连接过程中应当遵循特定跳跃规律才能得到正确的结果而不是简单闭合多边形而已哦!
```python
yellow_color = (0, 255, 255) # 黄色 BGR 颜色表示法
line_thickness = 5 # 线条粗细
connections = [(0, 2), (2, 4), (4, 1), (1, 3), (3, 0)] # 正确的连线顺序
for start_idx, end_idx in connections:
pt_start = pentagram_points[start_idx]
pt_end = pentagram_points[end_idx]
cv2.line(img, pt_start, pt_end, yellow_color, line_thickness)
```
上述代码片段分别指定了起点终点配对列表以及对应的颜色参数还有线宽属性等等细节配置项共同决定了最终呈现出来的视觉效果特征表现形式特点等方面的内容[^3]。
---
### 4. 显示结果图像
最后一步就是展示我们精心制作而成的作品啦~
```python
cv2.imshow("Yellow Pentagram on White Background", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这样就完成了整个流程的任务目标要求了呢😊
---
#### 完整代码示例
综合以上步骤,给出完整代码如下所示:
```python
import cv2
import numpy as np
def calculate_pentagram_points(center_x, center_y, radius):
points = []
for i in range(5):
angle_deg = 90 + i * 72 - 18 # 起始方向调整和间隔角度
angle_rad = np.deg2rad(angle_deg)
x = int(center_x + radius * np.cos(angle_rad))
y = int(center_y - radius * np.sin(angle_rad)) # 注意 y 坐标反向
points.append((x, y))
return points
if __name__ == "__main__":
# Step 1: Create white background canvas
img = np.zeros((800, 800, 3), dtype=np.uint8)
img.fill(255) # Set all pixels to white color
# Step 2: Define and compute pentagram vertex positions
center_x, center_y = 400, 400
radius = 300
pentagram_points = calculate_pentagram_points(center_x, center_y, radius)
# Step 3: Connect specific pairs of vertices using lines to create the star pattern
yellow_color = (0, 255, 255)
line_thickness = 5
connections = [(0, 2), (2, 4), (4, 1), (1, 3), (3, 0)]
for start_idx, end_idx in connections:
pt_start = pentagram_points[start_idx]
pt_end = pentagram_points[end_idx]
cv2.line(img, pt_start, pt_end, yellow_color, line_thickness)
# Step 4: Show resulting image containing drawn yellow pentagram over white bg.
cv2.imshow("Yellow Pentagram on White Background", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
---
阅读全文
相关推荐



















