You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为OpenCV中cv2.ellipse绘制的椭圆填充颜色?

解决OpenCV椭圆填充颜色的问题

我来帮你搞定这个椭圆内部填充的问题!你现在的代码里画的是空心椭圆,原因很简单——你设置的thickness参数是正数,这只会绘制椭圆的轮廓。要填充内部颜色,只需要把这个参数改成**-1**就行,OpenCV里所有封闭图形的绘图函数,只要把thickness设为负数,就会自动填充图形内部。

修改后的代码

def annotate_image(annotations, i):
    file_name = annotations[i][0]
    PATH= "/content/content/train/Class1_def/"+file_name+'.png'
    img=cv2.imread(PATH)
    #print(img.shape)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    semi_major= int(float(annotations[i][1]))
    semi_minor= int(float(annotations[i][2]))
    rotation= int(float(annotations[i][3]))
    x_pos_ellip= int(float(annotations[i][4]))
    y_pos_ellip= int(float(annotations[i][5]))
    center_coordinates= (x_pos_ellip, y_pos_ellip)
    axesLength= (semi_major,semi_minor)
    angle= int(float(rotation))
    startAngle = 0
    endAngle = 360
    # Red color
    color = (255, 0, 0)
    # 把thickness改成-1实现填充
    thickness = -1
    cv2.ellipse(img, center_coordinates, axesLength, angle, startAngle, endAngle, color, thickness)
    return img

关键说明

  • 当thickness为正数时:绘制图形的轮廓,数值代表线条的粗细程度
  • 当thickness为负数时:填充整个封闭图形的内部空间

这样修改后,你的椭圆就会被红色完全填充啦!

内容的提问来源于stack exchange,提问作者Hitesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:25:50