The class for OCR region.
示例#1
0
        private BoundingBox findPictureBoundaries(Region[] regions, bool pictureGoesOnTheLeft)
        {
            int minBoundary = (int) (0.2 * this.CardWidth); /* approx to remove lines that are close to border */
            BoundingBox picBBox = new BoundingBox();
            
            // yuk, I know
            if (pictureGoesOnTheLeft)
            {
                picBBox.x = 0;
                picBBox.y = 0;
                picBBox.Width = this.CardWidth;
            }
            else
            {
                picBBox.x = 0;
                picBBox.y = 0;
                picBBox.Width = this.CardWidth;
            }


            BoundingBox bBox;

            // Initially lowest point on card; will be modified in loop
            // if ever there is some text under the picture
            int bottomBoundary = CardHeight;

            foreach (Region reg in regions)
            {
                foreach (Line line in reg.Lines)
                {
                    bBox = new BoundingBox(line.BoundingBox);
                    // this makes me sick
                    if (pictureGoesOnTheLeft)
                    {
                        if (bBox.x < minBoundary) /* Assumption: Picture for sure doesn't span this line. Now, is this line over or under picture? */
                        {
                            if (bBox.y < (0.5 * this.CardHeight)) /* current line is within top half of card. Thus, lower y. */
                            {
                                picBBox.y = bBox.y + bBox.Height;
                            }
                            else
                            {
                                bottomBoundary = bBox.y; /* assume you're at bottom of where picture goes. */
                                break; 
                            }
                        }
                        else /* This is a good line - i.e. picture spans this line */ 
                        {
                            if (bBox.x < picBBox.Width) /* found a line that reduces possible boundary */
                            {
                                picBBox.Width = bBox.x;
                            }
                        }
                    }
                    else // picture is on the right
                    {
                        if ((this.CardWidth - (bBox.x + bBox.Width)) < minBoundary) /* Assumption: Picture for sure doesn't span this line. Now, is this line over or under picture? */
                        {
                            if (bBox.y < (0.5 * this.CardHeight)) /* current line is within top half of card. Thus, lower y. */
                            {
                                picBBox.y = bBox.y + bBox.Height;
                            }
                            else
                            {
                                bottomBoundary = bBox.y; /* assume you're at bottom of where picture goes. */
                                break;
                            }
                        }
                        else /* This is a good line - i.e. picture spans this line */
                        {
                            if (bBox.x + bBox.Width > picBBox.x) /* found a line that reduces possible boundary */
                            {
                                picBBox.x = bBox.x + bBox.Width;
                            }
                        }
                    }
                }
            }

            // set width & height from what we discovered of x & y
            picBBox.Height = bottomBoundary - picBBox.y;
            if (!pictureGoesOnTheLeft)
            {
                picBBox.Width = this.CardWidth - picBBox.x;
            }
            return picBBox;
        }
示例#2
0
 private bool isRightmostRegion(Region[] regions, int targetIndex)
 {
     BoundingBox targetBBox = new BoundingBox(regions[targetIndex].BoundingBox);
     BoundingBox otherBBox;
     for (int i = 0; i < regions.Length; i++)
     {
         if (i == targetIndex) continue;
         otherBBox = new BoundingBox(regions[i].BoundingBox);
         if (otherBBox.x + otherBBox.Width > targetBBox.x + targetBBox.Width)
         {
             return targetBBox.Height > otherBBox.Height; /* If they overlap, take the one that takes the most space */
         }
     }
     return true;
 }
示例#3
0
        private async Task writeIdPicture(Region[] regions)
        {
            // Calculate area between regions and left & right borders.
            // The assumption is that the greater the area, the greater the chance that
            // it's the area for the picture.
            int leftArea = 0, rightArea = 0;
            BoundingBox bBox;
            for (int i = 0; i < regions.Length; i++)
            {
                if (isLeftmostRegion(regions, i))
                {
                    bBox = new BoundingBox(regions[i].BoundingBox);
                    leftArea += (bBox.x * bBox.Height);
                }
                if (isRightmostRegion(regions, i))
                {
                    bBox = new BoundingBox(regions[i].BoundingBox);
                    rightArea += ((this.CardWidth - (bBox.x + bBox.Width)) * bBox.Height);
                }
            }

            bool pictureGoesOnTheLeft = leftArea > rightArea;

            // TODO: find minimal box
            BoundingBox pictureBox = findPictureBoundaries(regions, pictureGoesOnTheLeft);
            string imgWrapperDiv = "    <div style=\"position: absolute; " +
                                                "width: " + pictureBox.Width + "px; " +
                                                "height: " + pictureBox.Height + "px; " +
                                                "left: " + pictureBox.x + "px; " +
                                                "top : " + pictureBox.y + "px;\">\n";

            // place it. Margins are 15% of width/height
            string img = "      <img src=\"" + this.ReplacementPicturePath + "\" style=\"position: absolute; " +
                                                                                        "width: " + (0.7 * pictureBox.Width) + "px; " +
                                                                                        "height: " + (0.7 * pictureBox.Height) + "px; " +
                                                                                        "margin-left: " + (0.15 * pictureBox.Width) + "px; " +
                                                                                        "margin-top: " + (0.15 * pictureBox.Height) + "px;\"/>";
            await FileIO.AppendTextAsync(this.htmlFile, imgWrapperDiv);
            await FileIO.AppendTextAsync(this.htmlFile, img);
            await FileIO.AppendTextAsync(this.htmlFile, "</div>\n");
        }