Example #1
0
        public void DrawMarker()
        {
            const int markerSidePixels = 128;
            const int columns          = 4;
            const int rows             = 5;
            const int margin           = 20;

            // If you want to save markers image, you must change the following values.
            const string path = "C:\\markers_6x6_250.png";

            int width  = columns * markerSidePixels + margin * (columns + 1);
            int height = rows * markerSidePixels + margin * (rows + 1);

            var  id  = 0;
            Rect roi = new Rect(0, 0, markerSidePixels, markerSidePixels);

            using (var outputImage = new Mat(new Size(width, height), MatType.CV_8UC1, Scalar.White))
            {
                for (var y = 0; y < rows; y++)
                {
                    roi.Top = y * markerSidePixels + margin * (y + 1);

                    for (var x = 0; x < columns; x++)
                    {
                        roi.Left = x * markerSidePixels + margin * (x + 1);

                        using (var roiMat = new Mat(outputImage, roi))
                            using (var markerImage = new Mat())
                                using (var dict = CvAruco.GetPredefinedDictionary(PredefinedDictionaryName.Dict6X6_250))
                                {
                                    CvAruco.DrawMarker(dict, id++, markerSidePixels, markerImage, 1);
                                    markerImage.CopyTo(roiMat);
                                }
                    }
                }

                if (Debugger.IsAttached)
                {
                    Cv2.ImWrite(path, outputImage);
                    Process.Start(path);
                }
            }
        }
Example #2
0
        public static void BuildRegistrationCube(string path, double cmWidth = 10.4)
        {
            var definition = CoordinateDefinition.Cube(11 * 2.5, cmWidth);

            var dir   = Path.GetDirectoryName(path);
            var sides = new Dictionary <string, int>() //side, starting index id
            {
                { "Side1", 0 },
                { "Side2", 4 },
                { "Side3", 8 },
                { "Side4", 12 },
                { "Side5", 16 },
                { "Side6", 20 },
            };

            var pixelsPerCM = 300 / 2.54;

            //2 SQUARES WIDE, 2 SQUARES HIGH, 150 margin on width,
            //100 margin on height
            var paperPxWidth  = pixelsPerCM * cmWidth; //11in - 1in margin *300px/in
            var paperPxHeight = pixelsPerCM * cmWidth; //17in - 1in margin *300px/in
            var markerPxWidth = (int)(pixelsPerCM * 4.5 / 10.4 * cmWidth);
            var marginWidth   = (int)((paperPxWidth - 2.0 * markerPxWidth) / 4);
            var marginHeight  = marginWidth;
            var rows          = 2;
            var columns       = 2;

            var id = 0;

            foreach (var side in sides)
            {
                Rect roi = new Rect(0, 0, markerPxWidth, markerPxWidth);
                using (var outputImage = new Mat(new Size(paperPxWidth, paperPxHeight), MatType.CV_8UC1, Scalar.White))
                {
                    for (var y = 0; y < rows; y++)
                    {
                        roi.Top = y * markerPxWidth + marginHeight * (y * 2 + 1);

                        for (var x = 0; x < columns; x++)
                        {
                            roi.Left = x * markerPxWidth + marginWidth * (x * 2 + 1);

                            using (var roiMat = new Mat(outputImage, roi))
                                using (var markerImage = new Mat())
                                    using (var dict2 = CvAruco.GetPredefinedDictionary(PredefinedDictionaryName.Dict6X6_250))
                                    {
                                        CvAruco.DrawMarker(dict2, id++, markerPxWidth, markerImage, 1);
                                        markerImage.CopyTo(roiMat);
                                    }
                        }
                    }

                    var crossHairWidth = 1 * pixelsPerCM; // 1 cm
                    var crossHairColor = new Scalar(25, 25, 25);
                    Cv2.Line(outputImage, new Point((float)paperPxWidth / 2, (float)paperPxHeight / 2), new Point((float)paperPxWidth / 2, (float)(paperPxHeight / 2 - crossHairWidth)), crossHairColor);
                    Cv2.Line(outputImage, new Point((float)paperPxWidth / 2, (float)paperPxHeight / 2), new Point((float)paperPxWidth / 2, (float)(paperPxHeight / 2 + crossHairWidth)), crossHairColor);
                    Cv2.Line(outputImage, new Point((float)paperPxWidth / 2, (float)paperPxHeight / 2), new Point((float)(paperPxWidth / 2 - crossHairWidth), (float)(paperPxHeight / 2)), crossHairColor);
                    Cv2.Line(outputImage, new Point((float)paperPxWidth / 2, (float)paperPxHeight / 2), new Point((float)(paperPxWidth / 2 + crossHairWidth), (float)(paperPxHeight / 2)), crossHairColor);

                    Cv2.PutText(outputImage, side.Key, new Point((float)paperPxWidth / 2 - marginWidth / 1.3, (float)paperPxHeight), HersheyFonts.HersheyPlain, 1.5 / 10.4 * cmWidth, new Scalar(25, 25, 25), 1);
                    path = Path.Combine(dir, side.Key + ".png");
                    Cv2.ImWrite(path, outputImage);
                }
            }
        }