如何为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
相关产品推荐
相关产品推荐

