// Methods /// <summary> /// Create the image and the image texture of the <see cref="ArucoObject"/>. /// </summary> public virtual void Create() { Cv.Mat image = null; ImageTexture = null; // In case of a marker ArucoMarker marker = ArucoObject as ArucoMarker; if (marker != null) { marker.Dictionary.DrawMarker(marker.MarkerId, (int)marker.MarkerSideLength, out image, marker.MarkerBorderBits); } // In case of a grid board ArucoGridBoard arucoGridBoard = ArucoObject as ArucoGridBoard; if (arucoGridBoard != null) { Aruco.GridBoard gridBoard = arucoGridBoard.Board as Aruco.GridBoard; gridBoard.Draw(arucoGridBoard.ImageSize, out image, arucoGridBoard.MarginsSize, arucoGridBoard.MarkerBorderBits); } // In case of a charuco board ArucoCharucoBoard arucoCharucoBoard = ArucoObject as ArucoCharucoBoard; if (arucoCharucoBoard != null) { Aruco.CharucoBoard charucoBoard = arucoCharucoBoard.Board as Aruco.CharucoBoard; charucoBoard.Draw(arucoCharucoBoard.ImageSize, out image, arucoCharucoBoard.MarginsSize, arucoCharucoBoard.MarkerBorderBits); } // In case of a diamond ArucoDiamond diamond = ArucoObject as ArucoDiamond; if (diamond != null && diamond.Ids.Length == 4) { Cv.Vec4i ids = new Cv.Vec4i(); for (int i = 0; i < diamond.Ids.Length; ++i) { ids.Set(i, diamond.Ids[i]); } Aruco.DrawCharucoDiamond(diamond.Dictionary, ids, (int)diamond.SquareSideLength, (int)diamond.MarkerSideLength, out image); } // Set the properties Image = image; if (Image != null) { // Vertical flip to correctly display the image on the texture int verticalFlipCode = 0; Cv.Mat imageForTexture = Image.Clone(); Cv.Flip(imageForTexture, imageForTexture, verticalFlipCode); // Load the image to the texture int markerDataSize = (int)(Image.ElemSize() * Image.Total()); ImageTexture = new Texture2D(Image.Cols, Image.Rows, TextureFormat.RGB24, false); ImageTexture.LoadRawTextureData(imageForTexture.DataIntPtr, markerDataSize); ImageTexture.Apply(); } }
// Methods /// <summary> /// Create the image and the image texture of the <see cref="ArucoObject"/>. /// </summary> public virtual void Create() { Cv.Core.Mat image = null; ImageTexture = null; // In case of a marker ArucoMarker marker = ArucoObject as ArucoMarker; if (marker != null) { marker.Dictionary.DrawMarker(marker.MarkerId, (int)marker.MarkerSideLength, out image, marker.MarkerBorderBits); } // In case of a grid board ArucoGridBoard arucoGridBoard = ArucoObject as ArucoGridBoard; if (arucoGridBoard != null) { Aruco.GridBoard gridBoard = arucoGridBoard.Board as Aruco.GridBoard; gridBoard.Draw(arucoGridBoard.ImageSize, out image, arucoGridBoard.MarginsSize, arucoGridBoard.MarkerBorderBits); } // In case of a charuco board ArucoCharucoBoard arucoCharucoBoard = ArucoObject as ArucoCharucoBoard; if (arucoCharucoBoard != null) { Aruco.CharucoBoard charucoBoard = arucoCharucoBoard.Board as Aruco.CharucoBoard; charucoBoard.Draw(arucoCharucoBoard.ImageSize, out image, arucoCharucoBoard.MarginsSize, arucoCharucoBoard.MarkerBorderBits); } // In case of a diamond ArucoDiamond diamond = ArucoObject as ArucoDiamond; if (diamond != null && diamond.Ids.Length == 4) { Cv.Core.Vec4i ids = new Cv.Core.Vec4i(); for (int i = 0; i < diamond.Ids.Length; ++i) { ids.Set(i, diamond.Ids[i]); } Aruco.DrawCharucoDiamond(diamond.Dictionary, ids, (int)diamond.SquareSideLength, (int)diamond.MarkerSideLength, out image); } // Vertical flip to convert the image from Unity's left-handed coordinate system to OpenCV's right-handed coordinate system int verticalFlipCode = 0; Cv.Core.Flip(image, image, verticalFlipCode); // Set the properties Image = image; if (Image != null) { int markerDataSize = (int)(Image.ElemSize() * Image.Total()); ImageTexture = new Texture2D(Image.cols, Image.rows, TextureFormat.RGB24, false); ImageTexture.LoadRawTextureData(Image.dataIntPtr, markerDataSize); ImageTexture.Apply(); } }
/// <summary> /// Save the <see cref="ImageTexture"/> on a image file in the <see cref="OutputFolder"/>. Use <see cref="ImageFilename"/> as filename is /// specified or generate one automatically. /// </summary> public virtual void Save() { string imageFilePath = ImageFilename; if (imageFilePath == null || imageFilePath.Length == 0) { imageFilePath = "ArUcoUnity_"; // In case of a marker ArucoMarker marker = ArucoObject as ArucoMarker; if (marker != null) { imageFilePath += "Marker_" + marker.Dictionary.name + "_Id_" + marker.MarkerId; } // In case of a grid board ArucoGridBoard gridBoard = ArucoObject as ArucoGridBoard; if (gridBoard != null) { imageFilePath += "GridBoard_" + gridBoard.Dictionary.name + "_X_" + gridBoard.MarkersNumberX + "_Y_" + gridBoard.MarkersNumberY + "_MarkerSize_" + gridBoard.MarkerSideLength; } // In case of a charuco board ArucoCharucoBoard charucoBoard = ArucoObject as ArucoCharucoBoard; if (charucoBoard != null) { imageFilePath += "ChArUcoBoard_" + charucoBoard.Dictionary.name + "_X_" + charucoBoard.SquaresNumberX + "_Y_" + charucoBoard.SquaresNumberY + "_SquareSize_" + charucoBoard.SquareSideLength + "_MarkerSize_" + charucoBoard.MarkerSideLength; } // In case of a diamond ArucoDiamond diamond = ArucoObject as ArucoDiamond; if (diamond != null && diamond.Ids.Length == 4) { imageFilePath += "DiamondMarker_" + diamond.Dictionary.name + "_Ids_" + diamond.Ids[0] + "_" + diamond.Ids[1] + "_" + diamond.Ids[2] + "_" + diamond.Ids[3] + "_SquareSize_" + diamond.SquareSideLength + "_MarkerSize_" + diamond.MarkerSideLength; } } imageFilePath += ".png"; string outputFolderPath = Path.Combine((Application.isEditor) ? Application.dataPath : Application.persistentDataPath, OutputFolder); if (!Directory.Exists(outputFolderPath)) { Directory.CreateDirectory(outputFolderPath); } imageFilePath = outputFolderPath + imageFilePath; File.WriteAllBytes(imageFilePath, ImageTexture.EncodeToPNG()); }
/// <summary> /// Returns a generated filemame with the <see cref="ArucoObject"/> properties. /// </summary> public virtual string GenerateImageFilename() { string imageFilename = "ArUcoUnity_"; ArucoMarker marker = ArucoObject as ArucoMarker; if (marker != null) { imageFilename += "Marker_" + marker.Dictionary.Name + "_Id_" + marker.MarkerId; } ArucoGridBoard gridBoard = ArucoObject as ArucoGridBoard; if (gridBoard != null) { imageFilename += "GridBoard_" + gridBoard.Dictionary.Name + "_X_" + gridBoard.MarkersNumberX + "_Y_" + gridBoard.MarkersNumberY + "_MarkerSize_" + gridBoard.MarkerSideLength; } ArucoCharucoBoard charucoBoard = ArucoObject as ArucoCharucoBoard; if (charucoBoard != null) { imageFilename += "ChArUcoBoard_" + charucoBoard.Dictionary.Name + "_X_" + charucoBoard.SquaresNumberX + "_Y_" + charucoBoard.SquaresNumberY + "_SquareSize_" + charucoBoard.SquareSideLength + "_MarkerSize_" + charucoBoard.MarkerSideLength; } ArucoDiamond diamond = ArucoObject as ArucoDiamond; if (diamond != null && diamond.Ids.Length == 4) { imageFilename += "DiamondMarker_" + diamond.Dictionary.Name + "_Ids_" + diamond.Ids[0] + "_" + diamond.Ids[1] + "_" + diamond.Ids[2] + "_" + diamond.Ids[3] + "_SquareSize_" + diamond.SquareSideLength + "_MarkerSize_" + diamond.MarkerSideLength; } imageFilename += ".png"; return(imageFilename); }