OpenCV - 在python中创建一个Ellipse形状的蒙版

时间:2022-06-02 00:20:31

I've extracted a Circle shaped mask from an image in OpenCV. I used the following code for the same:

我从OpenCV中的图像中提取了一个圆形蒙版。我使用以下代码:

H, W = img.shape
x, y = np.meshgrid(np.arange(W), np.arange(H))**
d2 = (x - xc)**2 + (y - yc)**2**
mask = d2 < r **2**

And, used the mask value to find the average color outside the circle.

并且,使用蒙版值来查找圆外的平均颜色。

outside = np.ma.masked_where(mask, img)**
average_color = outside.mean()**

I want to extract an Ellipse from an image in the same above process in OpenCV Python.

我想在OpenCV Python的上述过程中从图像中提取一个Ellipse。

Thank You.

1 个解决方案

#1


0  

Drawing Ellipse

To draw the ellipse, we need to pass several arguments. One argument is the center location (x,y). Next argument is axes lengths (major axis length, minor axis length). angle is the angle of rotation of ellipse in anti-clockwise direction. startAngle and endAngle denotes the starting and ending of ellipse arc measured in clockwise direction from major axis. i.e. giving values 0 and 360 gives the full ellipse. For more details, check the documentation of cv2.ellipse(). Below example draws a half ellipse at the center of the image.

要绘制椭圆,我们需要传递几个参数。一个参数是中心位置(x,y)。下一个参数是轴长度(长轴长度,短轴长度)。角度是椭圆在逆时针方向上的旋转角度。 startAngle和endAngle表示从主轴顺时针方向测量的椭圆弧的起点和终点。即给出值0和360给出完整的椭圆。有关更多详细信息,请查看cv2.ellipse()的文档。下面的示例在图像的中心绘制一个半椭圆。

cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

Taken from Miki's Link in the Question Comments

摘自问题评论中的三木链接

#1


0  

Drawing Ellipse

To draw the ellipse, we need to pass several arguments. One argument is the center location (x,y). Next argument is axes lengths (major axis length, minor axis length). angle is the angle of rotation of ellipse in anti-clockwise direction. startAngle and endAngle denotes the starting and ending of ellipse arc measured in clockwise direction from major axis. i.e. giving values 0 and 360 gives the full ellipse. For more details, check the documentation of cv2.ellipse(). Below example draws a half ellipse at the center of the image.

要绘制椭圆,我们需要传递几个参数。一个参数是中心位置(x,y)。下一个参数是轴长度(长轴长度,短轴长度)。角度是椭圆在逆时针方向上的旋转角度。 startAngle和endAngle表示从主轴顺时针方向测量的椭圆弧的起点和终点。即给出值0和360给出完整的椭圆。有关更多详细信息,请查看cv2.ellipse()的文档。下面的示例在图像的中心绘制一个半椭圆。

cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

Taken from Miki's Link in the Question Comments

摘自问题评论中的三木链接