什么是EmguCV中的drawMarker?

时间:2019-12-30 15:57:16

标签: c# opencv emgucv

Opencv中有drawMarker函数,用于在图像的给定位置绘制标记。

在Emgu上找不到此函数...那么EmguCV中的drawMarker相当于什么?我需要标记Mat。

谢谢

2 个答案:

答案 0 :(得分:1)

它作为CvInvoke函数添加到DrawMarker类中,可以在此Git提交中找到:

https://github.com/emgucv/emgucv/commit/6a82dd6b6093934661a3b49155c9078016baef5f

它将包含在下一个EmguCV版本中。

答案 1 :(得分:0)

似乎该功能drawMarker在EmguCV代码中并未明确存在,尽管它存在于OpenCV Code的第1732至1795行中,所以我认为您唯一的解决方案是将其添加为在代码中起作用。

/// <summary>
/// Draws a marker on a given position in the image.
/// </summary>
/// <param name="img">The image.</param>
/// <param name="position">The point where the crosshair is positioned.</param>
/// <param name="color">Line color.</param>
/// <param name="markerType">The specific type of marker you want to use: 0 - Crosshair marker. 1 - 45 degree tilted crosshair marker. 2 - Star marker. 3 - Diamond marker. 4 - Upwards pointing triangle marker. 5 - Downwards pointing triangle marker.</param>
/// <param name="markerSize">The length of the marker axis [default = 20 pixels].</param>
/// <param name="thickness">Line thickness.</param>
/// <param name="line_type">Type of the line: -1 - Filled. 4 - 4-connected line. 8 - 8-connected line. 16 - Anti-aliased line.</param>
private void DrawMarker(IInputOutputArray img, Point position, MCvScalar color, MarkerType markerType, int markerSize, int thickness, LineType line_type)
{
    switch (markerType)
    {
        // The cross marker case
        case MarkerType.MarkerCross:
            CvInvoke.Line(img, new Point(position.X - markerSize / 2, position.Y), new Point(position.X + markerSize / 2, position.Y), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X, position.Y - markerSize / 2), new Point(position.X, position.Y + markerSize / 2), color, thickness, line_type);
            break;
        // The tilted cross marker case
        case MarkerType.MarkerTiltedCross:
            CvInvoke.Line(img, new Point(position.X - markerSize / 2, position.Y - markerSize / 2), new Point(position.X + markerSize / 2, position.Y + markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X + markerSize / 2, position.Y - markerSize / 2), new Point(position.X - markerSize / 2, position.Y + markerSize / 2), color, thickness, line_type);
            break;
        // The star marker case
        case MarkerType.MarkerStar:
            CvInvoke.Line(img, new Point(position.X - markerSize / 2, position.Y), new Point(position.X + markerSize / 2, position.Y), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X, position.Y - markerSize / 2), new Point(position.X, position.Y + markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X - markerSize / 2, position.Y - markerSize / 2), new Point(position.X + markerSize / 2, position.Y + markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X + markerSize / 2, position.Y - markerSize / 2), new Point(position.X - markerSize / 2, position.Y + markerSize / 2), color, thickness, line_type);
            break;
        // The diamond marker case
        case MarkerType.MarkerDiamond:
            CvInvoke.Line(img, new Point(position.X, position.Y - markerSize / 2), new Point(position.X + markerSize / 2, position.Y), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X + markerSize / 2, position.Y), new Point(position.X, position.Y + markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X, position.Y + markerSize / 2), new Point(position.X - markerSize / 2, position.Y), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X - markerSize / 2, position.Y), new Point(position.X, position.Y - markerSize / 2), color, thickness, line_type);
            break;
        // The square marker case
        case MarkerType.MarkerSquare:
            CvInvoke.Line(img, new Point(position.X - markerSize / 2, position.Y - markerSize / 2), new Point(position.X + markerSize / 2, position.Y - markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X + markerSize / 2, position.Y - markerSize / 2), new Point(position.X + markerSize / 2, position.Y + markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X + markerSize / 2, position.Y + markerSize / 2), new Point(position.X - markerSize / 2, position.Y + markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X - markerSize / 2, position.Y + markerSize / 2), new Point(position.X - markerSize / 2, position.Y - markerSize / 2), color, thickness, line_type);
            break;
        // The triangle up marker case
        case MarkerType.MarkerTriangleUp:
            CvInvoke.Line(img, new Point(position.X - markerSize / 2, position.Y + markerSize / 2), new Point(position.X + markerSize / 2, position.Y + markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X + markerSize / 2, position.Y + markerSize / 2), new Point(position.X, position.Y - markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X, position.Y - markerSize / 2), new Point(position.X - markerSize / 2, position.Y + markerSize / 2), color, thickness, line_type);
            break;
        // The triangle down marker case
        case MarkerType.MarkerTriangleDown:
            CvInvoke.Line(img, new Point(position.X - markerSize / 2, position.Y - markerSize / 2), new Point(position.X + markerSize / 2, position.Y - markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X + markerSize / 2, position.Y - markerSize / 2), new Point(position.X, position.Y + markerSize / 2), color, thickness, line_type);
            CvInvoke.Line(img, new Point(position.X, position.Y + markerSize / 2), new Point(position.X - markerSize / 2, position.Y - markerSize / 2), color, thickness, line_type);
            break;
        // If any number that doesn't exist is entered as marker type, draw a cross marker, to avoid crashes
        default:
            DrawMarker(img, position, color, MarkerType.MarkerCross, markerSize, thickness, line_type);
            break;
    }
}

您还需要为enum添加一个markerType,就像OpenCV's imgproc.hpp 第825至837行中的内容一样。

/// <summary>
/// Possible set of marker types used for the DrawMarker function
/// </summary>
enum MarkerType
{
    /// <summary>
    /// A crosshair marker shape
    /// </summary>
    MarkerCross = 0,
    /// <summary>
    /// A 45 degree tilted crosshair marker shape
    /// </summary>
    MarkerTiltedCross = 1,
    /// <summary>
    /// A star marker shape, combination of cross and tilted cross
    /// </summary>
    MarkerStar = 2,
    /// <summary>
    /// A diamond marker shape
    /// </summary>
    MarkerDiamond = 3,
    /// <summary>
    /// A square marker shape
    /// </summary>
    MarkerSquare = 4,
    /// <summary>
    /// An upwards pointing triangle marker shape
    /// </summary>
    MarkerTriangleUp = 5,
    /// <summary>
    /// A downwards pointing triangle marker shape
    /// </summary>
    MarkerTriangleDown = 6
}