Example #1
0
        private ConnectedComponent GetConnectedComponent(int contor, Point startingPoint, Bitmap bmp)
        {
            List <Point>  newConnectedComponentListOfPoints = new List <Point>();
            Queue <Point> queue = new Queue <Point>();

            m_visitedCells[startingPoint.X, startingPoint.Y] = true;
            queue.Enqueue(startingPoint);
            m_connectedComponentsMatrixForAtOnce[startingPoint.X, startingPoint.Y] = contor;

            while (queue.Count > 0)
            {
                Point currentPoint = queue.Dequeue();
                newConnectedComponentListOfPoints.Add(currentPoint);

                for (int k = 0; k < 8; k++)
                {
                    Point nextPoint = new Point(currentPoint.X + dx8[k], currentPoint.Y + dy8[k]);

                    if (IsInternalPoint(nextPoint) && m_visitedCells[nextPoint.X, nextPoint.Y] == false && bmp.GetPixel(nextPoint.Y, nextPoint.X).ToArgb() != Color.White.ToArgb())
                    {
                        m_visitedCells[nextPoint.X, nextPoint.Y] = true;
                        m_connectedComponentsMatrixForAtOnce[nextPoint.X, nextPoint.Y] = contor;
                        queue.Enqueue(nextPoint);
                    }
                }
            }

            ConnectedComponent ret = new ConnectedComponent(contor, newConnectedComponentListOfPoints);

            return(ret);
        }
Example #2
0
        public List <ConnectedComponent> GetAllConnectedComponentsAsOneUsingBoundingBox(Bitmap bmp)
        {
            // this method will be used for special characters such as 'i' and 'j' which consist of more than one connected component
            // and will use a bounding box for all the non-white pixels to return a list consisting of only one ConnectedComponent object

            List <ConnectedComponent> ret = new List <ConnectedComponent>();
            List <Point> newConnectedComponentPointsList = new List <Point>();

            m_connectedComponentsCounter++;



            for (int i = 0; i < bmp.Height; i++)
            {
                for (int j = 0; j < bmp.Width; j++)
                {
                    Color drawColor = bmp.GetPixel(j, i);
                    if (drawColor.ToArgb() != Color.White.ToArgb())           // it's a non white pixel, presumably a black one
                    {
                        newConnectedComponentPointsList.Add(new Point(i, j)); // 99% sure it's (i,j) and NOT (j,i)
                    }
                }
            }
            ConnectedComponent newConnectedComponent = new ConnectedComponent(m_connectedComponentsCounter, newConnectedComponentPointsList);

            newConnectedComponent.NormalizeUsingTranslation();
            ret.Add(newConnectedComponent);

            return(ret);
        }
Example #3
0
        private bool CheckIfListOfPointsIsAPoint(List <Point> newComponentPointsList)
        {
            //return true; // COMMENT IF BAD THINGS HAPPEN
            ConnectedComponent auxComponent = new ConnectedComponent(-1, newComponentPointsList);

            auxComponent.NormalizeUsingTranslation();

            CharacterImage chrImg = new CharacterImage(auxComponent);

            chrImg.NormalizeTo32x32(CharacterImage.NormalizeTo32x32Type.UnbiasedRatio);
            chrImg.MakeOnlyBlackAndWhite();
            chrImg.ThickenBlackPixels();

            return(chrImg.CheckIfIsPoint());
            //return false;
        }
Example #4
0
        public List <ConnectedComponent> InspectAllConnectedComponentsAtOnce(Bitmap bmp)
        {
            //use this method to get all the connected components from the bitmap using only one traversal of the bitmap
            // this method is particularly useful when gathering examples for training because repeated calls
            // to InspectConnectedComponents delay the app

            if (!m_initialized)
            {
                throw new Exception("ConnectedComponentsTool not initialized yet!");
            }

            for (int i = 0; i < m_height; i++)
            {
                for (int j = 0; j < m_width; j++)
                {
                    m_visitedCells[i, j] = false;
                    m_connectedComponentsMatrixForAtOnce[i, j] = 0;
                }
            }
            List <ConnectedComponent> ret = new List <ConnectedComponent>();
            int contor = 0;

            for (int i = 0; i < m_height; i++)
            {
                for (int j = 0; j < m_width; j++)
                {
                    Color drawnColor     = bmp.GetPixel(j, i);
                    int   drawnColorArgb = drawnColor.ToArgb();

                    if (drawnColorArgb != Color.White.ToArgb() && m_visitedCells[i, j] == false)
                    {
                        contor++;
                        ConnectedComponent currentConnectedComponent = GetConnectedComponent(contor, new Point(i, j), bmp);
                        currentConnectedComponent.NormalizeUsingTranslation();
                        ret.Add(currentConnectedComponent);
                    }
                }
            }

            return(ret);
        }
Example #5
0
        public CharacterImage(ConnectedComponent connectedComponent)
        {
            Rectangle boundingBox = connectedComponent.GetBoundingBox();

            m_bitmap = new Bitmap(boundingBox.Height, boundingBox.Width);
            for (int i = 0; i < m_bitmap.Height; i++)
            {
                for (int j = 0; j < m_bitmap.Width; j++)
                {
                    m_bitmap.SetPixel(j, i, Color.White);
                }
            }

            for (int i = 0; i < connectedComponent.Points.Count; i++)
            {
                int x = connectedComponent.Points[i].X;
                int y = connectedComponent.Points[i].Y;
                m_bitmap.SetPixel(y, x, Color.Black);
            }
            normalizedTo32x32     = false;
            madeOnlyBlackAndWhite = false;
        }
Example #6
0
        private void AdjustExistingComponents(int newComponentIndex, List <Point> newComponentPointsList, HashSet <int> oldConnectedComponentsIDs)
        {
            // check if newComponentPointsList is a point
            if (true) //this if can be removed
            {
                int stepsLimit = 0;
                if (CheckIfListOfPointsIsAPoint(newComponentPointsList))
                {
                    stepsLimit = 50;
                }
                else
                {
                    stepsLimit = 18;
                }
                // check the pixels below this points.
                Point leftPoint  = new Point();
                Point rightPoint = new Point();
                GetLeftAndRightExtremes(newComponentPointsList, ref leftPoint, ref rightPoint);
                int   connectedComponentIndexOfPoint  = newComponentIndex;
                int   connectedComponentIndexSearched = -1;
                Point aPointInSearchedComponent       = new Point();
                bool  stop = false;
                for (int i = Math.Max(leftPoint.X, rightPoint.X), steps = 1; !stop && steps <= stepsLimit; i++, steps++)
                {
                    if (i >= m_height)
                    {
                        stop = true;
                        break;
                    }
                    for (int j = leftPoint.Y - 1; j <= rightPoint.Y + 1; j++) // -3, +2 = a little extension for search area below point
                    {
                        if (!IsInternalPoint(new Point(i, j)))
                        {
                            continue;
                        }
                        if (m_connectedComponentsMatrix[i, j] == 0 || m_connectedComponentsMatrix[i, j] == connectedComponentIndexOfPoint)
                        {
                            continue;
                        }
                        connectedComponentIndexSearched = m_connectedComponentsMatrix[i, j];
                        stop = true;
                        aPointInSearchedComponent = new Point(i, j);
                        break;
                    }
                }
                if (connectedComponentIndexSearched != -1)
                {
                    AddTheSearchedComponentToThePoint(aPointInSearchedComponent, connectedComponentIndexSearched, connectedComponentIndexOfPoint, newComponentPointsList);
                    oldConnectedComponentsIDs.Add(connectedComponentIndexSearched);
                }
            }

            m_latestRemovedConnectedComponents = m_existingConnectedComponents.FindAll(x => oldConnectedComponentsIDs.Contains(x.ID));
            ObtainTheNewPointsForTheNewComponent(ref newComponentPointsList, oldConnectedComponentsIDs); // COMMENT IF BAD THINGS HAPPEN
            m_existingConnectedComponents.RemoveAll(x => oldConnectedComponentsIDs.Contains(x.ID));

            ConnectedComponent newComponent = new ConnectedComponent(newComponentIndex, newComponentPointsList);

            newComponent.NormalizeUsingTranslation();

            m_latestAddedConnectedComponent = newComponent;

            m_adjustmentsDoneCounter++;
            m_adjustmentsDone.Add(m_adjustmentsDoneCounter, new Tuple <List <ConnectedComponent>, ConnectedComponent>(m_latestRemovedConnectedComponents, m_latestAddedConnectedComponent));

            m_existingConnectedComponents.Add(newComponent);
        }