Ejemplo n.º 1
0
        static float[] convert_to_channel_first(Mat mat, float[] offsets)
        {
            var num_pixels = mat.Size().Height *mat.Size().Width;

            float[]    result  = new float[num_pixels * 3];
            MatOfByte3 mat3    = new MatOfByte3(mat);
            var        indexer = mat3.GetIndexer();
            var        pos     = 0;

            for (int y = 0; y < mat.Height; y++)
            {
                for (int x = 0; x < mat.Width; x++)
                {
                    var color = indexer[y, x];
                    result[pos] = color.Item0 - offsets[0];
                    result[pos + num_pixels]     = color.Item1 - offsets[1];
                    result[pos + 2 * num_pixels] = color.Item2 - offsets[2];
                    pos++;
                }
            }
            mat3.Dispose(); mat3 = null;
            return(result);
        }
Ejemplo n.º 2
0
        public bool ConnectVnc()
        {
            Connecting = true;

            try
            {
                m_tcpClient = new TcpClient();
                m_tcpClient.Connect(ClientConfig.Address, ClientConfig.Port);

                m_readStream  = m_readStreamCreator(m_tcpClient.Client);
                m_writeStream = m_writeStreamCreator(m_tcpClient.Client);

                // ZRLE encoding uses one zlib stream during connection.
                // Therefore, it generates at the timing of VNC connection.
                m_zrleReader?.Dispose();
                m_zrleReader = new ZrleDataReader();

                //-----------------------
                // Handshake
                //-----------------------
                // Server -> (ProtocolVersion) -> Client
                var version = VncComm.ReadProtocolVersion(m_readStream, ClientConfig.ForceVersion);

                // Server <- (ProtocolVersion) <- Client
                VncComm.WriteProtocolVersion(m_writeStream, version);

                // (Security)
                if (version == VncEnum.Version.Version33)
                {
                    // Server -> (Security) -> Client
                    var securityType = VncComm.ReadSecurityType(m_readStream);
                    if (securityType != VncEnum.SecurityType.None &&
                        securityType != VncEnum.SecurityType.VNCAuthentication)
                    {
                        throw new SecurityException($"VNC Version is 3.3. Security type is {securityType}.");
                    }

                    if (securityType == VncEnum.SecurityType.VNCAuthentication)
                    {
                        // Server -> (VNC Authentication Challenge) -> Client
                        var challenge = VncComm.ReadVncChallange(m_readStream);

                        // Server <- (VNC Authentication Response) <- Client
                        byte[] response = encryptChallenge(ClientConfig.Password, challenge);
                        VncComm.WriteVncResponse(m_writeStream, response);

                        // Server -> (Security Result) -> Client
                        VncComm.ReadSecurityResult(m_readStream, version); // Result is checked in method. So don't check here,
                    }
                }
                else
                {
                    // Server -> (SecurityTypes) -> Client
                    var securityTypes = VncComm.ReadSecurityTypes(m_readStream);

                    if (securityTypes.Contains(VncEnum.SecurityType.None))
                    {
                        // Server <- (SecurityType) <- Client
                        VncComm.WriteSecurityType(m_writeStream, VncEnum.SecurityType.None);

                        if (version == VncEnum.Version.Version38)
                        {
                            // Server -> (Security Result) -> Client
                            VncComm.ReadSecurityResult(m_readStream, version);
                        }
                    }
                    else if (securityTypes.Contains(VncEnum.SecurityType.VNCAuthentication))
                    {
                        // Server <- (SecurityType) <- Client
                        VncComm.WriteSecurityType(m_writeStream, VncEnum.SecurityType.VNCAuthentication);

                        // Server -> (VNC Authentication Challenge) -> Client
                        var challenge = VncComm.ReadVncChallange(m_readStream);

                        // Server <- (VNC Authentication Response) <- Client
                        byte[] response = encryptChallenge(ClientConfig.Password, challenge);
                        VncComm.WriteVncResponse(m_writeStream, response);

                        // Server -> (Security Result) -> Client
                        VncComm.ReadSecurityResult(m_readStream, version);
                    }
                    else
                    {
                        throw new SecurityException($"Unknown security-types. Server can use [{string.Join(",", securityTypes)}].");
                    }
                }

                //-----------------------
                // Initial Message
                //-----------------------
                // Server <- (ClientInit) <- Client
                VncComm.WriteClientInit(m_writeStream, VncEnum.SharedFlag.Share);

                // Server -> (ServerInit) -> Client
                m_serverInitBody = VncComm.ReadServerInit(m_readStream);

                //-----------------------
                // InitialSettings
                //-----------------------
                // Server <- (SetEncodings) <- Client
                VncComm.WriteSetEncodings(m_writeStream, ClientConfig.Encodings);

                // Server <- (SetPixelFormat) <- Client
                if (ClientConfig.IsColourSpecified)
                {
                    var pixelFormat = new PixelFormat(ClientConfig.SpecifiedColour.BytesPerPixel,
                                                      ClientConfig.SpecifiedColour.Depth,
                                                      m_serverInitBody.ServerPixelFormat.BigEndianFlag,
                                                      ClientConfig.SpecifiedColour.TrueColorFlag,
                                                      ClientConfig.SpecifiedColour.RedMax,
                                                      ClientConfig.SpecifiedColour.GreenMax,
                                                      ClientConfig.SpecifiedColour.BlueMax,
                                                      ClientConfig.SpecifiedColour.RedShift,
                                                      ClientConfig.SpecifiedColour.GreenShift,
                                                      ClientConfig.SpecifiedColour.BlueShift);
                    m_serverInitBody = new VncServerInitBody(m_serverInitBody.FramebufferWidth,
                                                             m_serverInitBody.FramebufferHeight,
                                                             pixelFormat,
                                                             m_serverInitBody.NameString);
                    VncComm.WriteSetPixelFormat(m_writeStream, m_serverInitBody.ServerPixelFormat);
                }

                //-----------------------
                // Refresh Framebuffer
                //-----------------------
                // Server <- (Refresh Framebuffer) <- Client
                VncComm.WriteFramebufferUpdateRequest(m_writeStream,
                                                      VncEnum.FramebufferUpdateRequestIncremental.UpdateAll,
                                                      0,
                                                      0,
                                                      m_serverInitBody.FramebufferWidth,
                                                      m_serverInitBody.FramebufferHeight);

                // Create Data
                m_pixelGetterNormal = VncPixelGetterFactory.CreateVncPixelGetter(m_serverInitBody.ServerPixelFormat, VncEnum.EncodeType.Raw);
                m_pixelGetterZrle   = VncPixelGetterFactory.CreateVncPixelGetter(m_serverInitBody.ServerPixelFormat, VncEnum.EncodeType.ZRLE);

                m_canvas?.Dispose();
                m_canvas = new MatOfByte3(m_serverInitBody.FramebufferHeight, m_serverInitBody.FramebufferWidth);

                // Successful connection
                m_connected = true;
                onConnected();
            }
            catch (Exception a_ex)
            {
                cleanupForDisconnect(a_ex);
                onConnectFailed(new VncCauseEventArgs(a_ex));
            }
            finally
            {
                Connecting = false;
            }

            return(m_connected);
        }
Ejemplo n.º 3
0
        public static void ExtractCharacters(string pathFile, string resultDir, bool removeEmptyBoxes = false)
        {
            int groupId = 0;

            try
            {
                var filename = Path.GetFileNameWithoutExtension(pathFile);
                var result   = FormExtraction.ProcessImage(pathFile);
                if (result.ReturnCode != 0)
                {
                    throw new Exception("ReturnCode: " + result.ReturnCode);
                }

                Console.WriteLine("Processing: " + Path.GetFileNameWithoutExtension(pathFile) + ", Duration: " + result.Duration);

                using (var image = new Mat(pathFile, ImreadModes.GrayScale))
                {
                    Cv2.AdaptiveThreshold(image, image, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 9, 4);

                    // TODO: we should not resize. (keep maximum quality)
                    if (image.Width > 800)
                    {
                        var height = 800 * image.Height / image.Width;
                        Cv2.Resize(image, image, new Size(800, height));
                    }

                    // You may want to use ".OrderBy(m => m.Min(x => x.TopLeft.Y)).Take(1)" to select the first box on top.
                    foreach (var group in result.Boxes)
                    {
                        // Console.WriteLine("\nGroup #" + numGroup + " (" + group.Count + ")");

                        groupId++;

                        int characterNum = 1;
                        foreach (var box in group)
                        {
                            // Console.WriteLine(box.TopLeft + " " + box.TopRight + "\n" + box.BottomLeft + " " + box.BottomRight + "\n");

                            var xTopLeft = Math.Min(box.TopLeft.X, box.BottomLeft.X);
                            var yTopLeft = Math.Min(box.TopLeft.Y, box.TopRight.Y);

                            var xBottomRight = Math.Max(box.TopRight.X, box.BottomRight.X);
                            var yBottomRight = Math.Max(box.BottomLeft.Y, box.BottomRight.Y);

                            var estimatedWidth  = xBottomRight - xTopLeft;
                            var estimatedHeight = yBottomRight - yTopLeft;

                            try
                            {
                                using (var subImg = new Mat(image, new Rect(xTopLeft, yTopLeft, estimatedWidth, estimatedHeight)))
                                {
                                    MatOfByte3         mat3    = new MatOfByte3(subImg);
                                    MatIndexer <Vec3b> indexer = mat3.GetIndexer();

                                    int borderPixelX = 4;
                                    int borderPixelY = 4;
                                    var minY         = Math.Min(borderPixelX, subImg.Height);
                                    var maxY         = Math.Max(0, subImg.Height - borderPixelX);
                                    var minX         = Math.Min(borderPixelX, subImg.Width);
                                    var maxX         = Math.Max(0, subImg.Width - borderPixelY);

                                    var outputFilename = filename + "_g-" + groupId + "_n-" + characterNum;

                                    if (removeEmptyBoxes)
                                    {
                                        // Basic empty box detection.
                                        int whitePixelCounter = 0;
                                        int pixelCounter      = 0;
                                        for (int y = minY; y <= maxY; y++)
                                        {
                                            for (int x = minX; x <= maxX; x++)
                                            {
                                                var pixel = indexer[y, x].Item0;                                                 // Grayscale only

                                                if (pixel == 255)
                                                {
                                                    whitePixelCounter++;
                                                }
                                                pixelCounter++;
                                            }
                                        }
                                        mat3.Dispose();

                                        int percentRatio = 100 * whitePixelCounter / pixelCounter;

                                        // Exclude empty boxes.
                                        if (percentRatio < 95)
                                        {
                                            Cv2.ImWrite(resultDir + Path.DirectorySeparatorChar + outputFilename + ".jpg", subImg);
                                        }
                                    }
                                    else
                                    {
                                        Cv2.ImWrite(resultDir + Path.DirectorySeparatorChar + outputFilename + ".jpg", subImg);
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                // Ignore it. Outside image.
                            }

                            characterNum++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Processing: " + Path.GetFileNameWithoutExtension(pathFile) + ", Error: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.ReadLine();
            }
        }