예제 #1
0
        public void Rectangle()
        {
            var color = Scalar.Red;

            using Mat img1 = Mat.Zeros(100, 100, MatType.CV_8UC3);
            using Mat img2 = img1.Clone();
            using Mat img3 = img1.Clone();
            using Mat img4 = img1.Clone();
            using InputOutputArray ioa3 = InputOutputArray.Create(img3);
            using InputOutputArray ioa4 = InputOutputArray.Create(img4);

            Cv2.Rectangle(img1, new Rect(10, 10, 80, 80), color, 1);
            Cv2.Rectangle(img2, new Point(10, 10), new Point(89, 89), color, 1);
            Cv2.Rectangle(ioa3, new Rect(10, 10, 80, 80), color, 1);
            Cv2.Rectangle(ioa4, new Point(10, 10), new Point(89, 89), color, 1);

            ShowImagesWhenDebugMode(img1, img2);

            var colorVec = color.ToVec3b();
            var expected = new Vec3b[100, 100];

            for (int x = 10; x <= 89; x++)
            {
                expected[10, x] = colorVec;
                expected[89, x] = colorVec;
            }
            for (int y = 10; y <= 89; y++)
            {
                expected[y, 10] = colorVec;
                expected[y, 89] = colorVec;
            }

            TestImage(img1, expected);
            TestImage(img2, expected);
            TestImage(img3, expected);
            TestImage(img4, expected);
        }
예제 #2
0
        void LoadPageShow()
        {
            currentShowTag = images[currentIndex];

            var image = Cv2.ImRead(currentShowTag.GetTrueImageFile());

            var tps = currentShowTag.OpenCvParts;

            if (tps == null || tps.Count == 0)
            {
                // 网上有看到有人先转灰度再识别,实际效果也没好多少 var gray_img = image.CvtColor(ColorConversionCodes.RGB2GRAY);
                var ract = FaceDetect.OpenCvDetectMultiScale(image);

                tps = new List <TagPart>();

                foreach (var face in ract)
                {
                    var body = AITag.Common.Utils.GetBodyRect(face, (double)(64 - 8) / 128, image.Width, image.Height);

                    if (body != Rect.Empty)
                    {
                        tps.Add(new TagPart
                        {
                            Face = face,
                            Body = body
                        });
                    }
                }
            }

            if (tps.Count == 0)
            {
                UpdateIamgeTagStatus(currentShowTag, "opencv_fail");
                btnNextImage_Click(null, null);
                return;
            }

            currentShowTag.OpenCvParts = tps;

            panelParts.Children.Clear();

            foreach (var item in tps)
            {
                var face = item.Face;
                var body = item.Body;

                Cv2.Rectangle(image, face, Scalar.Red);

                var i2  = image.Clone(body);
                var utp = new UserTagParts();

                utp.InitData(item, i2);

                panelParts.Children.Add(utp);

                Cv2.Rectangle(image, body, Scalar.Red);

                var ioa = InputOutputArray.Create(image);
                // 顺便标注一下尺寸
                Cv2.PutText(ioa, $"top:{face.Top} left:{face.Left} w:{face.Width} h:{face.Height}",
                            new Point(face.Left, face.Bottom + 2),
                            HersheyFonts.HersheySimplex, 1, Scalar.Blue);

                ioa = InputOutputArray.Create(image);
                Cv2.PutText(ioa, $"top:{body.Top} left:{body.Left} w:{body.Width} h:{body.Height}",
                            new Point(body.Left, body.Top - 10),
                            HersheyFonts.HersheySimplex, 1, Scalar.Blue);
            }

            imgShow.Source = image.MatToBitmapImage();

            panelCurrentImgTags.Children.Clear();

            if (!string.IsNullOrEmpty(currentShowTag.TagsName))
            {
                AppendTagtoCurrentTagPanel(currentShowTag.TagsName);
            }
        }