Skip to content

Add DetectCharucoBoard and related functions #1769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ public static extern ExceptionStatus aruco_drawDetectedDiamonds(
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] corners, int cornerSize1, int[] contoursSize2,
IntPtr ids, Scalar borderColor);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus aruco_detectCharucoBoard(
IntPtr image,
int squaresX, int squaresY, float squareLength, float markerLength, int arucoDictId,
IntPtr charucoCorners, IntPtr charucoIds, IntPtr markerCorners, IntPtr markerIds);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus aruco_interpolateCornersCharuco(
IntPtr image,
int squaresX, int squaresY, float squareLength, float markerLength, int arucoDictId,
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] markerCorners, int markerCornersSize1, int[] markerCornersSize2, IntPtr markerIds,
IntPtr charucoCorners, IntPtr charucoIds);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus aruco_drawDetectedCornersCharuco(
IntPtr image,
IntPtr corners, IntPtr ids, Scalar cornerColor);

#region Dictionary

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
Expand Down
133 changes: 133 additions & 0 deletions src/OpenCvSharp/Modules/aruco/CvAruco.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,137 @@ public static void DrawDetectedDiamonds(InputArray image,

GC.KeepAlive(image);
}

/// <summary>
/// Detect ChArUco Board.
/// </summary>
/// <param name="image">input image necessary for corner refinement.</param>
/// <param name="squaresX">number of chessboard squares in x directions.</param>
/// <param name="squaresY">number of chessboard squares in y directions.</param>
/// <param name="squareLength">chessboard square side length (normally in meters).</param>
/// <param name="markerLength">marker side length (same unit than squareLength).</param>
/// <param name="arucoDictId">dictionary of markers indicating the type of markers.</param>
/// <param name="charucoCorners">output list of detected charuco corners.</param>
/// <param name="charucoIds">ids of the charucos in charucoCorners.</param>
/// <param name="markerCorners">output list of detected marker corners.</param>
/// <param name="markerIds">ids of the corners in markerCorners.</param>
public static void DetectCharucoBoard(InputArray image, int squaresX, int squaresY, float squareLength, float markerLength,
PredefinedDictionaryName arucoDictId,
out Point2f[] charucoCorners, out int[] charucoIds,
out Point2f[][] markerCorners, out int[] markerIds)
{
if (image is null)
throw new ArgumentNullException(nameof(image));

image.ThrowIfDisposed();

using var charucoCornersVec = new VectorOfPoint2f();
using var charucoIdsVec = new VectorOfInt32();
using var markerCornersVec = new VectorOfVectorPoint2f();
using var markerIdsVec = new VectorOfInt32();

NativeMethods.HandleException(
NativeMethods.aruco_detectCharucoBoard(
image.CvPtr,
squaresX, squaresY, squareLength, markerLength, (int)arucoDictId,
charucoCornersVec.CvPtr, charucoIdsVec.CvPtr,
markerCornersVec.CvPtr, markerIdsVec.CvPtr
));

charucoCorners = charucoCornersVec.ToArray();
charucoIds = charucoIdsVec.ToArray();
markerCorners = markerCornersVec.ToArray();
markerIds = markerIdsVec.ToArray();

GC.KeepAlive(image);
}

/// <param name="image">input image necessary for corner refinement.</param>
/// <param name="squaresX">number of chessboard squares in x directions.</param>
/// <param name="squaresY">number of chessboard squares in y directions.</param>
/// <param name="squareLength">chessboard square side length (normally in meters).</param>
/// <param name="markerLength">marker side length (same unit than squareLength).</param>
/// <param name="arucoDictId">dictionary of markers indicating the type of markers.</param>
/// <param name="markerCorners">list of detected marker corners.</param>
/// <param name="markerIds">ids of the corners in markerCorners.</param>
/// <param name="charucoCorners">output list of detected charuco corners.</param>
/// <param name="charucoIds">ids of the charucos in charucoCorners.</param>
public static void InterpolateCornersCharuco(InputArray image,
int squaresX, int squaresY, float squareLength, float markerLength, PredefinedDictionaryName arucoDictId,
Point2f[][] markerCorners, IEnumerable<int> markerIds,
out Point2f[] charucoCorners, out int[] charucoIds)
{
if (image is null)
throw new ArgumentNullException(nameof(image));
if (markerCorners is null)
throw new ArgumentNullException(nameof(markerCorners));
if (markerIds is null)
throw new ArgumentNullException(nameof(markerIds));

image.ThrowIfDisposed();

using var markerCornersAddress = new ArrayAddress2<Point2f>(markerCorners);
using var markerIdsVec = new VectorOfInt32(markerIds);
using var charucoCornersVec = new VectorOfPoint2f();
using var charucoIdsVec = new VectorOfInt32();

NativeMethods.HandleException(
NativeMethods.aruco_interpolateCornersCharuco(
image.CvPtr,
squaresX, squaresY, squareLength, markerLength, (int)arucoDictId,
markerCornersAddress.GetPointer(), markerCornersAddress.GetDim1Length(), markerCornersAddress.GetDim2Lengths(), markerIdsVec.CvPtr,
charucoCornersVec.CvPtr, charucoIdsVec.CvPtr)
);

charucoCorners = charucoCornersVec.ToArray();
charucoIds = charucoIdsVec.ToArray();

GC.KeepAlive(image);
}

/// <summary>
/// Draw a set of detected ChArUco Diamond markers.
/// </summary>
/// <param name="image">input/output image. It must have 1 or 3 channels. The number of channels is not altered.</param>
/// <param name="charucoCorners">vector of detected charuco corners.</param>
/// <param name="charucoIds">list of identifiers for each corner in charucoCorners.</param>
public static void DrawDetectedCornersCharuco(InputArray image, Point2f[] charucoCorners, IEnumerable<int>? charucoIds = null)
{
DrawDetectedCornersCharuco(image, charucoCorners, charucoIds, new Scalar(0, 0, 255));
}

/// <summary>
/// Draw a set of detected ChArUco Diamond markers.
/// </summary>
/// <param name="image">input/output image. It must have 1 or 3 channels. The number of channels is not altered.</param>
/// <param name="charucoCorners">vector of detected charuco corners.</param>
/// <param name="charucoIds">list of identifiers for each corner in charucoCorners.</param>
/// <param name="cornerColor">color of the square surrounding each corner.</param>
public static void DrawDetectedCornersCharuco(InputArray image,
Point2f[] charucoCorners, IEnumerable<int>? charucoIds, Scalar cornerColor)
{
if (image is null)
throw new ArgumentNullException(nameof(image));
if (charucoCorners is null)
throw new ArgumentNullException(nameof(charucoCorners));

using var charucoCornersVec = new VectorOfPoint2f(charucoCorners);

if (charucoIds is null)
{
NativeMethods.HandleException(
NativeMethods.aruco_drawDetectedCornersCharuco(image.CvPtr,
charucoCornersVec.CvPtr, IntPtr.Zero, cornerColor));
}
else
{
using var ids = new VectorOfInt32(charucoIds);

NativeMethods.HandleException(
NativeMethods.aruco_drawDetectedCornersCharuco(image.CvPtr,
charucoCornersVec.CvPtr, ids.CvPtr, cornerColor));
}

GC.KeepAlive(image);
}
}
71 changes: 71 additions & 0 deletions src/OpenCvSharpExtern/aruco.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,74 @@ CVAPI(ExceptionStatus) aruco_drawDetectedDiamonds(
cv::aruco::drawDetectedDiamonds(*image, cornerVec, idArray, cpp(borderColor));
END_WRAP
}

CVAPI(ExceptionStatus) aruco_detectCharucoBoard(
cv::_InputArray* image,
int squaresX,
int squaresY,
float squareLength,
float markerLength,
int arucoDictId,
std::vector<cv::Point2f>* charucoCorners,
std::vector<int>* charucoIds,
std::vector< std::vector<cv::Point2f> >* markerCorners,
std::vector<int>* markerIds
)
{
BEGIN_WRAP
cv::aruco::Dictionary dictionary = cv::aruco::getPredefinedDictionary(arucoDictId);
cv::aruco::CharucoBoard charucoBoard(cv::Size(squaresX, squaresY), squareLength, markerLength, dictionary);
cv::aruco::CharucoDetector detector(charucoBoard);
cv::Mat charucoCorners_mat, charucoIds_mat;
//detector.detectBoard(*image, *charucoCorners, *charucoIds, *markerCorners, *markerIds);
detector.detectBoard(*image, charucoCorners_mat, charucoIds_mat, *markerCorners, *markerIds);
for (int i = 0; i < charucoCorners_mat.rows; ++i)
{
charucoCorners->push_back(charucoCorners_mat.at<cv::Point2f>(i, 0));
charucoIds->push_back(charucoIds_mat.at<int>(i, 0));
}
END_WRAP
}

CVAPI(ExceptionStatus) aruco_interpolateCornersCharuco(
cv::_InputArray* image,
int squaresX,
int squaresY,
float squareLength,
float markerLength,
int arucoDictId,
cv::Point2f** markerCorners,
int markerCornersSize1,
int* markerCornersSize2,
std::vector<int>* markerIds,
std::vector<cv::Point2f>* charucoCorners,
std::vector<int>* charucoIds
)
{
BEGIN_WRAP
std::vector< std::vector<cv::Point2f> > markerCornerVec(markerCornersSize1);
for (int i = 0; i < markerCornersSize1; i++)
markerCornerVec[i] = std::vector<cv::Point2f>(markerCorners[i], markerCorners[i] + markerCornersSize2[i]);

cv::aruco::Dictionary dictionary = cv::aruco::getPredefinedDictionary(arucoDictId);
cv::Ptr<cv::aruco::CharucoBoard> ptr_charucoBoard = new cv::aruco::CharucoBoard(cv::Size(squaresX, squaresY), squareLength, markerLength, dictionary);
cv::Mat charucoCorners_mat, charucoIds_mat;
cv::aruco::interpolateCornersCharuco(markerCornerVec, *markerIds, *image, ptr_charucoBoard, charucoCorners_mat, charucoIds_mat);
for (int i = 0; i < charucoCorners_mat.rows; ++i)
{
charucoCorners->push_back(charucoCorners_mat.at<cv::Point2f>(i, 0));
charucoIds->push_back(charucoIds_mat.at<int>(i, 0));
}
END_WRAP
}

CVAPI(ExceptionStatus) aruco_drawDetectedCornersCharuco(
cv::_InputOutputArray* image,
std::vector<cv::Point2f>* corners,
std::vector<int>* ids,
MyCvScalar cornerColor)
{
BEGIN_WRAP
cv::aruco::drawDetectedCornersCharuco(*image, *corners, *ids, cpp(cornerColor));
END_WRAP
}