Exemple #1
0
        private Mat _0A_StandardFrame(Mat frame, ref Point[] ptR, bool DispB)    //input:gray
        {
            Size sz = frame.Size();
            int  W = sz.Width, H = sz.Height, szD = 32 * 9, szDP = szD + 20;
            Mat  frame2 = new Mat(szDP, szDP, MatType.CV_8UC1);

            Point[] ptI = new Point[4];
            ptI[0] = new Point(10, 10); ptI[1] = new Point(10 + szD, 10); ptI[2] = new Point(10 + szD, 10 + szD); ptI[3] = new Point(10, 10 + szD);
            PT.SetPoint(ptR, ptI);
            PT.ProjectionSolver();
            Point ptXY = new Point();

            unsafe {
                byte *S = frame.DataPointer;
                byte *D = frame2.DataPointer;
                for (int y = 0; y < szDP; y++)
                {
                    ptXY.Y = y;
                    for (int x = 0; x < szDP; x++)
                    {
                        ptXY.X = x;
                        Point P = PT.Convert_ItoR(ptXY);
                        if (P.X >= 0 && P.X < W && P.Y >= 0 && P.Y < H)
                        {
                            *D = S[P.Y * W + P.X];
                        }
                        D++;
                    }
                }
            }
            ptR = ptI;
            if (DispB)
            {
                new Window("_0A_StandardFrame", WindowMode.KeepRatio, frame2);
            }
            return(frame2);
        }
        public Mat _PTNormalize(Mat Mg0, int ctrlWD, byte thValue)
        {
            //Position adjustment
#if false
            Mat Mg5 = new Mat();
            Cv2.Resize(Mg0, Mg5, new Size(Mg0.Cols * 10, Mg0.Rows * 10), 0, 0, InterpolationFlags.Lanczos4);
                        using (new Window("PTNormalize", Mg5)){ /* Cv2.WaitKey();*/ }
#endif
            Point2d[] ptI = __GetCorner4(Mg0, thValue);
            Point2d[] ptR = new Point2d[4];
            int       wd;
            if (ctrlWD == 0)
            {
                wd = (int)((ptI[4].X / ptI[4].Y) * 28);     //wdCtrl:0,1-3
            }
            else
            {
                wd = 15 + ctrlWD * 2;
            }
            if (wd > 22)
            {
                wd = 22;
            }
            int w = (32 - wd) / 2;
            ptR[0] = new Point2d(w, 4);
            ptR[1] = new Point2d(w + wd, 4);
            ptR[2] = new Point2d(w + wd, 28);
            ptR[3] = new Point2d(w, 28);

            PT.SetPoint(ptR, ptI);
            PT.ProjectionSolver();

            Size sz = Mg0.Size();
            int  W  = sz.Width;

            Mat MgR = new Mat(sz, MatType.CV_8UC1, Scalar.All(255));

            byte B = 255, b0 = 0, b255 = 255, b128 = 128;
            int  cnt = 0;
            unsafe {
                byte *S = Mg0.DataPointer;
                byte *D = MgR.DataPointer;

                for (int x = 0; x < sz.Width; x++)
                {
                    for (int y = 0; y < sz.Height; y++)
                    {
                        Point Rxy = PT.Convert_RtoI(new Point(x, y));
                        B = 255;
                        if (Rxy.Y >= 0 && Rxy.Y < sz.Height && Rxy.X >= 0 && Rxy.X < sz.Width)
                        {
                            B = S[Rxy.Y * W + Rxy.X];
                            B = (B > thValue)? b255: b0;
                        }
                        D[y * W + x] = B;
                        if (B < b128)
                        {
                            cnt++;
                        }
                    }
                }
                if (cnt < 32)
                {
                    for (int k = 0; k < 32; k++)
                    {
                        D[k] = b0;
                    }
                }
                else if (cnt < 64)
                {
                    for (int k = 32; k < 64; k++)
                    {
                        D[k] = b0;
                    }
                }
            }

            return(MgR);
        }