Example #1
0
 public int FindExactMatchExpanded(BinaryOcrBitmap bob)
 {
     for (int i = 0; i < CompareImagesExpanded.Count; i++)
     {
         var b = CompareImagesExpanded[i];
         if (bob.Hash == b.Hash &&
             bob.Width == b.Width &&
             bob.Height == b.Height &&
             bob.NumberOfColoredPixels == b.NumberOfColoredPixels &&
             bob.ExpandCount == b.ExpandCount &&
             bob.AreColorsEqual(b))
         {
             bool ok = true;
             for (int k = 0; k < b.ExpandedList.Count; k++)
             {
                 if (bob.ExpandedList[k].Hash != b.ExpandedList[k].Hash ||
                     !bob.ExpandedList[k].AreColorsEqual(b.ExpandedList[k])) // expanded images
                 {
                     ok = false;
                 }
             }
             if (ok)
             {
                 return(i);
             }
         }
     }
     return(-1);
 }
Example #2
0
        public int Add(BinaryOcrBitmap bob)
        {
            int index;

            if (bob.ExpandCount > 0)
            {
                if (bob.ExpandedList == null || bob.ExpandCount - 1 != bob.ExpandedList.Count)
                {
                    throw new Exception("BinaryOcrDb.Add: There should be " + (bob.ExpandCount - 1) + " sub image(s)");
                }

                if (bob.ExpandedList[0].Text != null)
                {
                    throw new Exception("BinaryOcrDb.Add: sub image should have null text");
                }

                index = FindExactMatchExpanded(bob);
                if (index == -1 || CompareImagesExpanded[index].ExpandCount != bob.ExpandCount)
                {
                    CompareImagesExpanded.Add(bob);
                }
                else
                {
                    bool allAlike = true;
                    for (int i = 0; i < bob.ExpandCount - 1; i++)
                    {
                        if (bob.ExpandedList[i].Hash != CompareImagesExpanded[index].ExpandedList[i].Hash)
                        {
                            allAlike = false;
                        }

                        if (bob.ExpandedList[i].Text != null)
                        {
                            throw new Exception("BinaryOcrDb.Add: sub image should have null text");
                        }
                    }
                    if (!allAlike)
                    {
                        CompareImagesExpanded.Add(bob);
                    }
                    else
                    {
                        throw new Exception("BinaryOcrDb.Add: Expanded image already in db!");
                    }
                }
            }
            else
            {
                index = FindExactMatch(bob);
                if (index == -1)
                {
                    CompareImages.Add(bob);
                }
                else
                {
                    throw new Exception("BinaryOcrDb.Add: Image already in db!");
                }
            }
            return(index);
        }
        internal static int IsBitmapsAlike(NikseBitmap bmp1, Ocr.Binary.BinaryOcrBitmap bmp2)
        {
            int different = 0;
            int maxDiff   = bmp1.Width * bmp1.Height / 5;
            int w4        = bmp1.Width * 4;

            for (int y = 1; y < bmp1.Height; y++)
            {
                var alpha = y * w4 + 7;
                var pixel = y * bmp2.Width + 1;
                for (int x = 1; x < bmp1.Width; x++)
                {
                    if (bmp1.GetAlpha(alpha) < 100 && bmp2.GetPixel(pixel) > 0)
                    {
                        different++;
                    }

                    pixel++;
                    alpha += 4;
                }
                if (different > maxDiff)
                {
                    return(different + 10);
                }
            }
            return(different);
        }
 public int Add(BinaryOcrBitmap bob)
 {
     int index;
     if (bob.ExpandCount > 0)
     {
         index = FindExactMatchExpanded(bob);
         if (index == -1 || CompareImagesExpanded[index].ExpandCount != bob.ExpandCount)
         {
             CompareImagesExpanded.Add(bob);
         }
         else
         {
             bool allAlike = true;
             for (int i = 0; i < bob.ExpandCount - 1; i++)
             {
                 if (bob.ExpandedList[i].Hash != CompareImagesExpanded[index].ExpandedList[i].Hash)
                     allAlike = false;
             }
             if (!allAlike)
                 CompareImages.Add(bob);
             else
                 System.Windows.Forms.MessageBox.Show("Expanded image already in db!");
         }
     }
     else
     {
         index = FindExactMatch(bob);
         if (index == -1)
             CompareImages.Add(bob);
         else
             System.Windows.Forms.MessageBox.Show("Image already in db!");
     }
     return index;
 }
Example #5
0
 public static bool AllowEqual(BinaryOcrBitmap match, BinaryOcrBitmap newBob)
 {
     if (match.Text != null && (match.Text == "," || match.Text == "'") &&
         Math.Abs(match.Y - newBob.Y) > MaxCommaQuoteTopDiff)
     {
         return(false);
     }
     return(true);
 }
Example #6
0
 public static bool AllowEqual(BinaryOcrBitmap match, BinaryOcrBitmap newBob)
 {
     if (match.Text != null && (match.Text == "," || match.Text == "'") &&
         Math.Min(match.Y, newBob.Y) < MinYDiffPair && Math.Max(match.Y, newBob.Y) > MaxYDiffPair)
     {
         return(false);
     }
     return(true);
 }
        public void TestMethodBinOcrSaveLoad()
        {
            string tempFileName = Path.GetTempFileName();
            var db = new BinaryOcrDb(tempFileName);
            var nbmp = new NikseBitmap(2, 2);
            nbmp.SetPixel(0, 0, Color.Transparent);
            nbmp.SetPixel(1, 0, Color.Transparent);
            nbmp.SetPixel(1, 0, Color.Transparent);
            nbmp.SetPixel(1, 1, Color.White);

            var bob = new BinaryOcrBitmap(nbmp);
            bob.Text = "Debug";
            db.Add(bob);

            nbmp.SetPixel(0, 0, Color.White);
            var bob2 = new BinaryOcrBitmap(nbmp);
            bob2.X = 2;
            bob2.Y = 4;
            bob2.Text = "tt";
            bob2.Italic = true;
            bob2.ExpandCount = 2;
            bob2.ExpandedList = new System.Collections.Generic.List<BinaryOcrBitmap>();
            bob2.ExpandedList.Add(bob2);
            db.Add(bob2);
            db.Save();

            db = new BinaryOcrDb(tempFileName, true);
            Assert.IsTrue(db.CompareImages.Count == 1);
            Assert.IsTrue(db.CompareImagesExpanded.Count == 1);

            Assert.IsTrue(bob.Width == db.CompareImages[0].Width);
            Assert.IsTrue(bob.Height == db.CompareImages[0].Height);
            Assert.IsTrue(bob.NumberOfColoredPixels == db.CompareImages[0].NumberOfColoredPixels);
            Assert.IsTrue(bob.Hash == db.CompareImages[0].Hash);
            Assert.IsTrue(bob.Italic == db.CompareImages[0].Italic);
            Assert.IsTrue(bob.ExpandCount == db.CompareImages[0].ExpandCount);
            Assert.IsTrue(bob.Text == db.CompareImages[0].Text);

            Assert.IsTrue(bob2.Width == db.CompareImagesExpanded[0].Width);
            Assert.IsTrue(bob2.Height == db.CompareImagesExpanded[0].Height);
            Assert.IsTrue(bob2.NumberOfColoredPixels == db.CompareImagesExpanded[0].NumberOfColoredPixels);
            Assert.IsTrue(bob2.Hash == db.CompareImagesExpanded[0].Hash);
            Assert.IsTrue(bob2.Italic == db.CompareImagesExpanded[0].Italic);
            Assert.IsTrue(bob2.ExpandCount == db.CompareImagesExpanded[0].ExpandCount);
            Assert.IsTrue(bob2.Text == db.CompareImagesExpanded[0].Text);
            Assert.IsTrue(bob2.X == db.CompareImagesExpanded[0].X);
            Assert.IsTrue(bob2.Y == db.CompareImagesExpanded[0].Y);

            try
            {
                File.Delete(tempFileName);
            }
            catch
            {
            }
        }
        private void AddToListView(BinaryOcrBitmap bob)
        {
            _data.Add(new ListViewData { Checked = true, BinaryOcrBitmap = bob });

            var item = new ListViewItem(string.Empty) { Tag = bob, Checked = true };
            item.SubItems.Add(bob.ToString());
            listView1.Items.Add(item);
            imageList1.Images.Add(bob.ToOldBitmap(Color.Black));
            item.ImageIndex = imageList1.Images.Count - 1;
        }
 public int FindExactMatch(BinaryOcrBitmap bob)
 {
     for (int i = 0; i < CompareImages.Count; i++)
     {
         var b = CompareImages[i];
         if (bob.Hash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels)
             return i;
     }
     return -1;
 }
Example #10
0
        public void LoadCompareImages()
        {
            var list       = new List <BinaryOcrBitmap>();
            var expandList = new List <BinaryOcrBitmap>();

            if (!File.Exists(FileName))
            {
                CompareImages = list;
                return;
            }

            using (Stream gz = new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress))
            {
                bool done = false;
                while (!done)
                {
                    var bob = new BinaryOcrBitmap(gz);
                    if (bob.LoadedOk)
                    {
                        if (bob.ExpandCount > 0)
                        {
                            expandList.Add(bob);
                            bob.ExpandedList = new List <BinaryOcrBitmap>();
                            for (int i = 1; i < bob.ExpandCount; i++)
                            {
                                var expandedBob = new BinaryOcrBitmap(gz);
                                if (expandedBob.LoadedOk)
                                {
                                    if (expandedBob.Text != null)
                                    {
                                        throw new Exception("BinaryOcrDb.LoadCompareImages: sub image should have null text");
                                    }

                                    bob.ExpandedList.Add(expandedBob);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            list.Add(bob);
                        }
                    }
                    else
                    {
                        done = true;
                    }
                }
            }
            CompareImages         = list;
            CompareImagesExpanded = expandList;
        }
Example #11
0
 public int FindExactMatch(BinaryOcrBitmap bob)
 {
     for (int i = 0; i < CompareImages.Count; i++)
     {
         var b = CompareImages[i];
         if (bob.Hash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels)
         {
             return(i);
         }
     }
     return(-1);
 }
Example #12
0
        public void LoadCompareImages()
        {
            var list = new List<BinaryOcrBitmap>();
            var expandList = new List<BinaryOcrBitmap>();

            if (!File.Exists(FileName))
            {
                CompareImages = list;
                return;
            }

            using (Stream gz = new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress))
            {
                bool done = false;
                while (!done)
                {
                    var bob = new BinaryOcrBitmap(gz);
                    if (bob.LoadedOk)
                    {
                        if (bob.ExpandCount > 0)
                        {
                            expandList.Add(bob);
                            bob.ExpandedList = new List<BinaryOcrBitmap>();
                            for (int i = 1; i < bob.ExpandCount; i++)
                            {
                                var expandedBob = new BinaryOcrBitmap(gz);
                                if (expandedBob.LoadedOk)
                                {
                                    if (expandedBob.Text != null)
                                        throw new Exception("BinaryOcrDb.LoadCompareImages: sub image should have null text");
                                    bob.ExpandedList.Add(expandedBob);
                                }
                                else
                                    break;
                            }
                        }
                        else
                        {
                            list.Add(bob);
                        }
                    }
                    else
                    {
                        done = true;
                    }
                }
            }
            CompareImages = list;
            CompareImagesExpanded = expandList;
        }
Example #13
0
        public bool AreColorsEqual(BinaryOcrBitmap other)
        {
            if (_colors.Length != other._colors.Length)
            {
                return(false);
            }

            for (int i = 0; i < _colors.Length; i++)
            {
                if (_colors[i] != other._colors[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Example #14
0
        public int Add(BinaryOcrBitmap bob)
        {
            int index;

            if (bob.ExpandCount > 0)
            {
                index = FindExactMatchExpanded(bob);
                if (index == -1 || CompareImagesExpanded[index].ExpandCount != bob.ExpandCount)
                {
                    CompareImagesExpanded.Add(bob);
                }
                else
                {
                    bool allAlike = true;
                    for (int i = 0; i < bob.ExpandCount - 1; i++)
                    {
                        if (bob.ExpandedList[i].Hash != CompareImagesExpanded[index].ExpandedList[i].Hash)
                        {
                            allAlike = false;
                        }
                    }
                    if (!allAlike)
                    {
                        CompareImages.Add(bob);
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("Expanded image already in db!");
                    }
                }
            }
            else
            {
                index = FindExactMatch(bob);
                if (index == -1)
                {
                    CompareImages.Add(bob);
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Image already in db!");
                }
            }
            return(index);
        }
Example #15
0
        public void TestMethodBinOcrSaveLoadTestExceptions()
        {
            string tempFileName = Path.GetTempFileName();
            var db = new BinaryOcrDb(tempFileName);
            var nbmp = new NikseBitmap(2, 2);
            nbmp.SetPixel(0, 0, Color.Transparent);
            nbmp.SetPixel(1, 0, Color.Transparent);
            nbmp.SetPixel(1, 0, Color.Transparent);
            nbmp.SetPixel(1, 1, Color.White);

            var bob = new BinaryOcrBitmap(nbmp);
            bob.Text = "S";
            db.Add(bob);

            nbmp.SetPixel(0, 0, Color.White);
            var bob2 = new BinaryOcrBitmap(nbmp);
            bob2.X = 2;
            bob2.Y = 4;
            bob2.Text = null;
            bob2.Italic = true;
            bob2.ExpandCount = 3;
            bob2.ExpandedList = new System.Collections.Generic.List<BinaryOcrBitmap>();
            bob2.ExpandedList.Add(bob2);
            try
            {
                db.Add(bob2);
            }
            catch
            {
                return;
            }
            Assert.Fail();

            try
            {
                File.Delete(tempFileName);
            }
            catch
            {
            }
        }
Example #16
0
        internal static int IsBitmapsAlike(NikseBitmap bmp1, Ocr.Binary.BinaryOcrBitmap bmp2)
        {
            int different = 0;
            int maxDiff   = bmp1.Width * bmp1.Height / 5;

            for (int x = 1; x < bmp1.Width; x++)
            {
                for (int y = 1; y < bmp1.Height; y++)
                {
                    if (bmp1.GetAlpha(x, y) < 100 && bmp2.GetPixel(x, y) > 0)
                    {
                        different++;
                    }
                }
                if (different > maxDiff)
                {
                    return(different + 10);
                }
            }
            return(different);
        }
Example #17
0
        internal static int IsBitmapsAlike(Ocr.Binary.BinaryOcrBitmap bmp1, NikseBitmap bmp2)
        {
            int different = 0;
            int maxDiff   = bmp1.Width * bmp1.Height / 5;

            for (int x = 0; x < bmp1.Width; x++)
            {
                for (int y = 0; y < bmp1.Height; y++)
                {
                    //if (!IsColorClose(bmp1.GetPixel(x, y), bmp2.GetPixel(x, y), 20))
                    if (bmp1.GetPixel(x, y) > 0 && bmp2.GetAlpha(x, y) < 100)
                    {
                        different++;
                    }
                }
                if (different > maxDiff)
                {
                    return(different + 10);
                }
            }
            return(different);
        }
        internal static int IsBitmapsAlike(Ocr.Binary.BinaryOcrBitmap bmp1, Ocr.Binary.BinaryOcrBitmap bmp2)
        {
            int different = 0;
            int maxDiff = bmp1.Width * bmp1.Height / 5;
            for (int y = 0; y < bmp1.Height; y++)
            {
                var pixel = y * bmp1.Width;
                for (int x = 0; x < bmp1.Width; x++)
                {
                    if (bmp1.GetPixel(pixel) != bmp2.GetPixel(pixel))
                    {
                        different++;
                    }

                    pixel++;
                }
                if (different > maxDiff)
                {
                    return different + 10;
                }
            }
            return different;
        }
Example #19
0
        private static void FindBestMatchNew(ref int index, ref int smallestDifference, ref int smallestIndex, NikseBitmap target, BinaryOcrDb binOcrDb, BinaryOcrBitmap bob, double maxDiff)
        {
            var bobExactMatch = binOcrDb.FindExactMatch(bob);
            if (bobExactMatch >= 0)
            {
                index = bobExactMatch;
                smallestDifference = 0;
                smallestIndex = bobExactMatch;
                return;
            }

            if (maxDiff < 0.2 || target.Width < 3 || target.Height < 5)
                return;

            int numberOfForegroundColors = bob.NumberOfColoredPixels;
            const int minForeColorMatch = 90;

            index = 0;
            foreach (var compareItem in binOcrDb.CompareImages)
            {
                if (compareItem.Width == target.Width && compareItem.Height == target.Height) // precise math in size
                {
                    if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < 3)
                    {
                        int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target);
                        if (dif < smallestDifference)
                        {
                            smallestDifference = dif;
                            smallestIndex = index;
                            if (dif < 3)
                            {
                                break; // foreach ending
                            }
                        }
                    }
                }
                index++;
            }

            if (smallestDifference > 1)
            {
                index = 0;
                foreach (var compareItem in binOcrDb.CompareImages)
                {
                    if (compareItem.Width == target.Width && compareItem.Height == target.Height) // precise math in size
                    {
                        if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < 40)
                        {
                            int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target);
                            if (dif < smallestDifference)
                            {
                                smallestDifference = dif;
                                smallestIndex = index;
                                if (dif == 0)
                                {
                                    break; // foreach ending
                                }
                            }
                        }
                    }
                    index++;
                }
            }

            if (target.Width > 16 && target.Height > 16 && smallestDifference > 2) // for other than very narrow letter (like 'i' and 'l' and 'I'), try more sizes
            {
                index = 0;
                foreach (var compareItem in binOcrDb.CompareImages)
                {
                    if (compareItem.Width == target.Width && compareItem.Height == target.Height - 1)
                    {
                        if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch)
                        {
                            int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target);
                            if (dif < smallestDifference)
                            {
                                smallestDifference = dif;
                                smallestIndex = index;
                                if (dif == 0)
                                    break; // foreach ending
                            }
                        }
                    }
                    index++;
                }

                if (smallestDifference > 2)
                {
                    index = 0;
                    foreach (var compareItem in binOcrDb.CompareImages)
                    {
                        if (compareItem.Width == target.Width && compareItem.Height == target.Height + 1)
                        {
                            if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch)
                            {
                                int dif = NikseBitmapImageSplitter.IsBitmapsAlike(target, compareItem);
                                if (dif < smallestDifference)
                                {
                                    smallestDifference = dif;
                                    smallestIndex = index;
                                    if (dif == 0)
                                        break; // foreach ending
                                }
                            }
                        }
                        index++;
                    }
                }

                if (smallestDifference > 3)
                {
                    index = 0;
                    foreach (var compareItem in binOcrDb.CompareImages)
                    {
                        if (compareItem.Width == target.Width + 1 && compareItem.Height == target.Height + 1)
                        {
                            if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch)
                            {
                                int dif = NikseBitmapImageSplitter.IsBitmapsAlike(target, compareItem);
                                if (dif < smallestDifference)
                                {
                                    smallestDifference = dif;
                                    smallestIndex = index;
                                    if (dif == 0)
                                        break; // foreach ending
                                }
                            }
                        }
                        index++;
                    }
                }

                if (smallestDifference > 5)
                {
                    index = 0;
                    foreach (var compareItem in binOcrDb.CompareImages)
                    {
                        if (compareItem.Width == target.Width - 1 && compareItem.Height == target.Height - 1)
                        {
                            if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch)
                            {
                                int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target);
                                if (dif < smallestDifference)
                                {
                                    smallestDifference = dif;
                                    smallestIndex = index;
                                    if (dif == 0)
                                        break; // foreach ending
                                }
                            }
                        }
                        index++;
                    }
                }

                if (smallestDifference > 5)
                {
                    index = 0;
                    foreach (var compareItem in binOcrDb.CompareImages)
                    {
                        if (compareItem.Width - 1 == target.Width && compareItem.Height == target.Height)
                        {
                            if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch)
                            {
                                int dif = NikseBitmapImageSplitter.IsBitmapsAlike(target, compareItem);
                                if (dif < smallestDifference)
                                {
                                    smallestDifference = dif;
                                    smallestIndex = index;
                                    if (dif == 0)
                                        break; // foreach ending
                                }
                            }
                        }
                        index++;
                    }
                }

                if (smallestDifference > 9 && target.Width > 11)
                {
                    index = 0;
                    foreach (var compareItem in binOcrDb.CompareImages)
                    {
                        if (compareItem.Width == target.Width - 2 && compareItem.Height == target.Height)
                        {
                            if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch)
                            {
                                int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target);
                                if (dif < smallestDifference)
                                {
                                    smallestDifference = dif;
                                    smallestIndex = index;
                                    if (dif == 0)
                                        break; // foreach ending
                                }
                            }
                        }
                        index++;
                    }
                }

                if (smallestDifference > 9 && target.Width > 14)
                {
                    index = 0;
                    foreach (var compareItem in binOcrDb.CompareImages)
                    {
                        if (compareItem.Width == target.Width - 3 && compareItem.Height == target.Height)
                        {
                            if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch)
                            {
                                int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target);
                                if (dif < smallestDifference)
                                {
                                    smallestDifference = dif;
                                    smallestIndex = index;
                                    if (dif == 0)
                                        break; // foreach ending
                                }
                            }
                        }
                        index++;
                    }
                }

                if (smallestDifference > 9 && target.Width > 14)
                {
                    index = 0;
                    foreach (var compareItem in binOcrDb.CompareImages)
                    {
                        if (compareItem.Width == target.Width && compareItem.Height == target.Height - 3)
                        {
                            if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch)
                            {
                                int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target);
                                if (dif < smallestDifference)
                                {
                                    smallestDifference = dif;
                                    smallestIndex = index;
                                    if (dif == 0)
                                        break; // foreach ending
                                }
                            }
                        }
                        index++;
                    }
                }

                if (smallestDifference > 9 && target.Width > 14)
                {
                    index = 0;
                    foreach (var compareItem in binOcrDb.CompareImages)
                    {
                        if (compareItem.Width - 2 == target.Width && compareItem.Height == target.Height)
                        {
                            if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch)
                            {
                                int dif = NikseBitmapImageSplitter.IsBitmapsAlike(target, compareItem);
                                if (dif < smallestDifference)
                                {
                                    smallestDifference = dif;
                                    smallestIndex = index;
                                    if (dif == 0)
                                        break; // foreach ending
                                }
                            }
                        }
                        index++;
                    }
                }
            }

            if (smallestDifference == 0)
            {
                if (smallestIndex > 200)
                {
                    var hit = binOcrDb.CompareImages[smallestIndex];
                    binOcrDb.CompareImages.RemoveAt(smallestIndex);
                    binOcrDb.CompareImages.Insert(0, hit);
                    smallestIndex = 0;
                    index = 0;
                }
            }
        }
Example #20
0
        private CompareMatch GetCompareMatchNew(ImageSplitterItem targetItem, out CompareMatch secondBestGuess, List<ImageSplitterItem> list, int listIndex)
        {
            double maxDiff = _numericUpDownMaxErrorPct;
            secondBestGuess = null;
            int index = 0;
            int smallestDifference = 10000;
            int smallestIndex = -1;
            var target = targetItem.NikseBitmap;
            if (_binaryOcrDb == null)
            {
                return null;
            }

            var bob = new BinaryOcrBitmap(target) { X = targetItem.X, Y = targetItem.Top };

            for (int k = 0; k < _binaryOcrDb.CompareImagesExpanded.Count; k++)
            {
                var b = _binaryOcrDb.CompareImagesExpanded[k];
                if ((bob.Hash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels) || GetPixelDifPercentage(b, bob, target) <= maxDiff)
                {
                    bool ok = false;
                    for (int i = 0; i < b.ExpandedList.Count; i++)
                    {
                        if (listIndex + i + 1 < list.Count && list[listIndex + i + 1].NikseBitmap != null)
                        {
                            var bobNext = new BinaryOcrBitmap(list[listIndex + i + 1].NikseBitmap);
                            if (b.ExpandedList[i].Hash == bobNext.Hash)
                            {
                                ok = true;
                            }
                            else if (GetPixelDifPercentage(b.ExpandedList[i], bobNext, list[listIndex + i + 1].NikseBitmap) <= maxDiff)
                            {
                                ok = true;
                            }
                            else
                            {
                                ok = false;
                                break;
                            }
                        }
                    }
                    if (ok)
                    {
                        return new CompareMatch(b.Text, b.Italic, b.ExpandCount, b.Key);
                    }
                }
            }

            FindBestMatchNew(ref index, ref smallestDifference, ref smallestIndex, target, _binaryOcrDb, bob, maxDiff);
            if (maxDiff > 0)
            {
                if (target.Width > 16 && target.Height > 16 && (smallestIndex == -1 || smallestDifference * 100.0 / (target.Width * target.Height) > maxDiff))
                {
                    var t2 = target.CopyRectangle(new Rectangle(0, 1, target.Width, target.Height - 1));
                    FindBestMatchNew(ref index, ref smallestDifference, ref smallestIndex, t2, _binaryOcrDb, bob, maxDiff);
                }
                if (target.Width > 16 && target.Height > 16 && (smallestIndex == -1 || smallestDifference * 100.0 / (target.Width * target.Height) > maxDiff))
                {
                    var t2 = target.CopyRectangle(new Rectangle(1, 0, target.Width - 1, target.Height));
                    FindBestMatchNew(ref index, ref smallestDifference, ref smallestIndex, t2, _binaryOcrDb, bob, maxDiff);
                }
                if (target.Width > 16 && target.Height > 16 && (smallestIndex == -1 || smallestDifference * 100.0 / (target.Width * target.Height) > maxDiff))
                {
                    var t2 = target.CopyRectangle(new Rectangle(0, 0, target.Width - 1, target.Height));
                    FindBestMatchNew(ref index, ref smallestDifference, ref smallestIndex, t2, _binaryOcrDb, bob, maxDiff);
                }
            }

            if (smallestIndex >= 0)
            {
                double differencePercentage = smallestDifference * 100.0 / (target.Width * target.Height);
                if (differencePercentage <= maxDiff)
                {
                    var hit = _binaryOcrDb.CompareImages[smallestIndex];

                    string text = hit.Text;
                    if (smallestDifference > 0)
                    {
                        int h = hit.Height;
                        if (text == "V" || text == "W" || text == "U" || text == "S" || text == "Z" || text == "O" || text == "X" || text == "Ø" || text == "C")
                        {
                            if (_binOcrLastLowercaseHeight > 3 && h - _binOcrLastLowercaseHeight < 2)
                                text = text.ToLower();
                        }
                        else if (text == "v" || text == "w" || text == "u" || text == "s" || text == "z" || text == "o" || text == "x" || text == "ø" || text == "c")
                        {
                            if (_binOcrLastUppercaseHeight > 3 && _binOcrLastUppercaseHeight - h < 2)
                                text = text.ToUpper();
                        }
                    }
                    else
                    {
                        SetBinOcrLowercaseUppercase(hit.Height, text);
                    }
                    if (differencePercentage > 0)
                    {
                        bool dummy;
                        if ((hit.Text == "l" || hit.Text == "!") && bob.IsLowercaseI(out dummy))
                        {
                            hit = null;
                        }
                        else if ((hit.Text == "i" || hit.Text == "!") && bob.IsLowercaseL())
                        {
                            hit = null;
                        }
                        else if ((hit.Text == "o" || hit.Text == "O") && bob.IsC())
                        {
                            return new CompareMatch(hit.Text == "o" ? "c" : "C", false, 0, null);
                        }
                        else if ((hit.Text == "c" || hit.Text == "C") && !bob.IsC() && bob.IsO())
                        {
                            return new CompareMatch(hit.Text == "c" ? "o" : "O", false, 0, null);
                        }
                    }

                    if (hit != null)
                    {
                        if (_binOcrLastLowercaseHeight < 0 && differencePercentage < 9 && (text == "e" || text == "d" || text == "a"))
                            _binOcrLastLowercaseHeight = bob.Height;
                        return new CompareMatch(text, hit.Italic, hit.ExpandCount, hit.Key);
                    }
                }

                var guess = _binaryOcrDb.CompareImages[smallestIndex];
                secondBestGuess = new CompareMatch(guess.Text, guess.Italic, guess.ExpandCount, guess.Key);
            }

            if (bob.IsPeriod())
            {
                return new CompareMatch(".", false, 0, null);
            }
            if (bob.IsComma())
            {
                return new CompareMatch(",", false, 0, null);
            }
            if (maxDiff > 0 && bob.IsApostrophe())
            {
                return new CompareMatch("'", false, 0, null);
            }
            if (bob.IsLowercaseJ()) // "j" detection must be before "i"
            {
                return new CompareMatch("j", false, 0, null);
            }
            bool italicLowercaseI;
            if (bob.IsLowercaseI(out italicLowercaseI))
            {
                return new CompareMatch("i", italicLowercaseI, 0, null);
            }
            if (bob.IsColon())
            {
                return new CompareMatch(":", false, 0, null);
            }
            if (bob.IsExclamationMark())
            {
                return new CompareMatch("!", false, 0, null);
            }
            if (bob.IsDash())
            {
                return new CompareMatch("-", false, 0, null);
            }

            return null;
        }
        private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            labelImageInfo.Text = string.Empty;
            labelExpandCount.Text = string.Empty;

            if (listBoxInspectItems.SelectedIndex < 0)
                return;

            _selectedCompareNode = null;
            _selectedCompareBinaryOcrBitmap = null;

            pictureBoxInspectItem.Image = _imageSources[listBoxInspectItems.SelectedIndex];
            pictureBoxCompareBitmap.Image = null;
            pictureBoxCompareBitmapDouble.Image = null;

            int index = (listBoxInspectItems.SelectedIndex);
            var match = _matches[index];
            _selectedMatch = match;
            if (!string.IsNullOrEmpty(match.Name))
            {
                if (_binOcrDb != null)
                {
                    bool bobFound = false;
                    foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImages)
                    {
                        if (match.Name == bob.Key)
                        {
                            textBoxText.Text = bob.Text;
                            checkBoxItalic.Checked = bob.Italic;
                            _selectedCompareBinaryOcrBitmap = bob;

                            var bitmap = bob.ToOldBitmap();
                            pictureBoxCompareBitmap.Image = bitmap;
                            pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2;
                            pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2;
                            pictureBoxCompareBitmapDouble.Image = bitmap;

                            var matchBob = new BinaryOcrBitmap(new NikseBitmap(_imageSources[listBoxInspectItems.SelectedIndex]));
                            if (matchBob.Hash == bob.Hash && matchBob.Width == bob.Width && matchBob.Height == bob.Height && matchBob.NumberOfColoredPixels == bob.NumberOfColoredPixels)
                            {
                                buttonAddBetterMatch.Enabled = false; // exact match
                            }
                            else
                            {
                                buttonAddBetterMatch.Enabled = true;
                            }

                            bobFound = true;
                            break;
                        }
                    }
                    if (!bobFound)
                    {
                        foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImagesExpanded)
                        {
                            if (match.Name == bob.Key)
                            {
                                textBoxText.Text = bob.Text;
                                checkBoxItalic.Checked = bob.Italic;
                                _selectedCompareBinaryOcrBitmap = bob;

                                var bitmap = bob.ToOldBitmap();
                                pictureBoxCompareBitmap.Image = bitmap;
                                pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2;
                                pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2;
                                pictureBoxCompareBitmapDouble.Image = bitmap;
                                buttonAddBetterMatch.Enabled = false; // exact match
                                labelExpandCount.Text = string.Format("Expand count: {0}", bob.ExpandCount);
                                break;
                            }
                        }
                    }
                }
                else
                {
                    foreach (XmlNode node in ImageCompareDocument.DocumentElement.ChildNodes)
                    {
                        if (node.Attributes["Text"] != null && node.InnerText == match.Name)
                        {
                            string text = node.Attributes["Text"].InnerText;
                            textBoxText.Text = text;
                            checkBoxItalic.Checked = node.Attributes["Italic"] != null;
                            string databaseName = Path.Combine(_directoryPath, "Images.db");
                            using (var f = new FileStream(databaseName, FileMode.Open))
                            {
                                try
                                {
                                    string name = node.InnerText;
                                    int pos = Convert.ToInt32(name);
                                    f.Position = pos;
                                    var mbmp = new ManagedBitmap(f);
                                    var bitmap = mbmp.ToOldBitmap();
                                    pictureBoxCompareBitmap.Image = bitmap;
                                    pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2;
                                    pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2;
                                    pictureBoxCompareBitmapDouble.Image = bitmap;
                                    labelImageInfo.Text = string.Format(Configuration.Settings.Language.VobSubEditCharacters.Image + " - {0}x{1}", bitmap.Width, bitmap.Height);
                                }
                                catch (Exception exception)
                                {
                                    labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.Image;
                                    MessageBox.Show(exception.Message);
                                }
                            }

                            _selectedCompareNode = node;
                            break;
                        }
                    }
                }
            }

            buttonAddBetterMatch.Text = Configuration.Settings.Language.VobSubOcrCharacterInspect.AddBetterMatch;
            if (_selectedMatch.Text == Configuration.Settings.Language.VobSubOcr.NoMatch)
            {
                buttonUpdate.Enabled = false;
                buttonDelete.Enabled = false;
                buttonAddBetterMatch.Enabled = true;
                buttonAddBetterMatch.Text = Configuration.Settings.Language.VobSubOcrCharacterInspect.Add;
                textBoxText.Enabled = true;
                textBoxText.Text = string.Empty;
                checkBoxItalic.Enabled = true;
            }
            else if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null)
            {
                buttonUpdate.Enabled = false;
                buttonDelete.Enabled = false;
                buttonAddBetterMatch.Enabled = true;
                textBoxText.Enabled = true;
                textBoxText.Text = string.Empty;
                checkBoxItalic.Enabled = false;
            }
            else
            {
                buttonUpdate.Enabled = true;
                buttonDelete.Enabled = true;
                if (_selectedCompareNode != null)
                    buttonAddBetterMatch.Enabled = true;
                textBoxText.Enabled = true;
                checkBoxItalic.Enabled = true;
            }
        }
        private void buttonAddBetterMatch_Click(object sender, EventArgs e)
        {
            if (listBoxInspectItems.SelectedIndex < 0)
                return;

            if (listBoxInspectItems.Items[listBoxInspectItems.SelectedIndex].ToString() == textBoxText.Text)
            {
                textBoxText.SelectAll();
                textBoxText.Focus();
                return;
            }

            if (_selectedCompareBinaryOcrBitmap != null || (_binOcrDb != null && (_selectedMatch.Text == Configuration.Settings.Language.VobSubOcr.NoMatch || _selectedMatch.NOcrCharacter == null)))
            {
                var nbmp = new NikseBitmap((pictureBoxInspectItem.Image as Bitmap));
                int x = 0;
                int y = 0;
                if (_selectedMatch != null && _selectedMatch.ImageSplitterItem != null)
                {
                    if (_selectedMatch.ImageSplitterItem != null)
                    {
                        x = _selectedMatch.ImageSplitterItem.X;
                        y = _selectedMatch.ImageSplitterItem.Top;
                    }
                    else
                    {
                        x = _selectedMatch.X;
                        y = _selectedMatch.Y;
                    }
                }
                var bob = new BinaryOcrBitmap(nbmp, checkBoxItalic.Checked, 0, textBoxText.Text, x, y);
                _binOcrDb.Add(bob);

                int index = listBoxInspectItems.SelectedIndex;
                _matches[index].Name = bob.Key;
                _matches[index].ExpandCount = 0;
                _matches[index].Italic = checkBoxItalic.Checked;
                _matches[index].Text = textBoxText.Text;
                listBoxInspectItems.Items.Clear();
                for (int i = 0; i < _matches.Count; i++)
                    listBoxInspectItems.Items.Add(_matches[i].Text);
                listBoxInspectItems.SelectedIndex = index;
                listBoxInspectItems_SelectedIndexChanged(null, null);
                ShowCount();

                // update other letters that are exact matches
                for (int i = 0; i < _matches.Count; i++)
                {
                    if (i != index && i < _imageSources.Count && _matches[i].ExpandCount == 0)
                    {
                        var newMatch = _binOcrDb.FindExactMatch(new BinaryOcrBitmap(new NikseBitmap(_imageSources[i])));
                        if (newMatch >= 0 && _binOcrDb.CompareImages[newMatch].Hash == bob.Hash)
                        {
                            _matches[i].Name = bob.Key;
                            _matches[i].ExpandCount = 0;
                            _matches[i].Italic = checkBoxItalic.Checked;
                            _matches[i].Text = textBoxText.Text;
                            listBoxInspectItems.Items[i] = textBoxText.Text;
                        }
                    }
                }

                return;
            }

            if (_selectedCompareNode != null)
            {
                XmlNode newNode = ImageCompareDocument.CreateElement("Item");
                var text = newNode.OwnerDocument.CreateAttribute("Text");
                text.InnerText = textBoxText.Text;
                newNode.Attributes.Append(text);

                string databaseName = Path.Combine(_directoryPath, "Images.db");
                FileStream f;
                long pos;
                if (!File.Exists(databaseName))
                {
                    using (f = new FileStream(databaseName, FileMode.Create))
                    {
                        pos = f.Position;
                        new ManagedBitmap(pictureBoxInspectItem.Image as Bitmap).AppendToStream(f);
                    }
                }
                else
                {
                    using (f = new FileStream(databaseName, FileMode.Append))
                    {
                        pos = f.Position;
                        new ManagedBitmap(pictureBoxInspectItem.Image as Bitmap).AppendToStream(f);
                    }
                }
                string name = pos.ToString(CultureInfo.InvariantCulture);
                newNode.InnerText = name;

                SetItalic(newNode);
                ImageCompareDocument.DocumentElement.AppendChild(newNode);

                int index = listBoxInspectItems.SelectedIndex;
                _matches[index].Name = name;
                _matches[index].ExpandCount = 0;
                _matches[index].Italic = checkBoxItalic.Checked;
                _matches[index].Text = textBoxText.Text;
                listBoxInspectItems.Items.Clear();
                for (int i = 0; i < _matches.Count; i++)
                    listBoxInspectItems.Items.Add(_matches[i].Text);
                listBoxInspectItems.SelectedIndex = index;
                ShowCount();
                listBoxInspectItems_SelectedIndexChanged(null, null);
            }
        }
        /// <summary>
        /// The is bitmaps alike.
        /// </summary>
        /// <param name="bmp1">
        /// The bmp 1.
        /// </param>
        /// <param name="bmp2">
        /// The bmp 2.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        internal static int IsBitmapsAlike(NikseBitmap bmp1, BinaryOcrBitmap bmp2)
        {
            int different = 0;
            int maxDiff = bmp1.Width * bmp1.Height / 5;

            for (int x = 1; x < bmp1.Width; x++)
            {
                for (int y = 1; y < bmp1.Height; y++)
                {
                    if (bmp1.GetAlpha(x, y) < 100 && bmp2.GetPixel(x, y) > 0)
                    {
                        different++;
                    }
                }

                if (different > maxDiff)
                {
                    return different + 10;
                }
            }

            return different;
        }
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null)
                return;

            listBoxInspectItems.Items[listBoxInspectItems.SelectedIndex] = Configuration.Settings.Language.VobSubOcr.NoMatch;
            if (_selectedCompareBinaryOcrBitmap != null)
            {
                if (_selectedCompareBinaryOcrBitmap.ExpandCount > 0)
                    _binOcrDb.CompareImagesExpanded.Remove(_selectedCompareBinaryOcrBitmap);
                else
                    _binOcrDb.CompareImages.Remove(_selectedCompareBinaryOcrBitmap);
                _selectedCompareBinaryOcrBitmap = null;
            }
            else
            {
                ImageCompareDocument.DocumentElement.RemoveChild(_selectedCompareNode);
                _selectedCompareNode = null;
            }
            listBoxInspectItems_SelectedIndexChanged(null, null);
        }
        /// <summary>
        /// The add.
        /// </summary>
        /// <param name="bob">
        /// The bob.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int Add(BinaryOcrBitmap bob)
        {
            int index;
            if (bob.ExpandCount > 0)
            {
                index = this.FindExactMatchExpanded(bob);
                if (index == -1 || this.CompareImagesExpanded[index].ExpandCount != bob.ExpandCount)
                {
                    this.CompareImagesExpanded.Add(bob);
                }
                else
                {
                    bool allAlike = true;
                    for (int i = 0; i < bob.ExpandCount - 1; i++)
                    {
                        if (bob.ExpandedList[i].Hash != this.CompareImagesExpanded[index].ExpandedList[i].Hash)
                        {
                            allAlike = false;
                        }
                    }

                    if (!allAlike)
                    {
                        this.CompareImages.Add(bob);
                    }
                    else
                    {
                        MessageBox.Show("Expanded image already in db!");
                    }
                }
            }
            else
            {
                index = this.FindExactMatch(bob);
                if (index == -1)
                {
                    this.CompareImages.Add(bob);
                }
                else
                {
                    MessageBox.Show("Image already in db!");
                }
            }

            return index;
        }
        /// <summary>
        /// The find exact match expanded.
        /// </summary>
        /// <param name="bob">
        /// The bob.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int FindExactMatchExpanded(BinaryOcrBitmap bob)
        {
            for (int i = 0; i < this.CompareImagesExpanded.Count; i++)
            {
                BinaryOcrBitmap b = this.CompareImagesExpanded[i];
                if (bob.Hash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels)
                {
                    return i;
                }
            }

            return -1;
        }
        /// <summary>
        /// The load compare images.
        /// </summary>
        public void LoadCompareImages()
        {
            List<BinaryOcrBitmap> list = new List<BinaryOcrBitmap>();
            List<BinaryOcrBitmap> expandList = new List<BinaryOcrBitmap>();

            if (!File.Exists(this.FileName))
            {
                this.CompareImages = list;
                return;
            }

            using (Stream gz = new GZipStream(File.OpenRead(this.FileName), CompressionMode.Decompress))
            {
                bool done = false;
                while (!done)
                {
                    BinaryOcrBitmap bob = new BinaryOcrBitmap(gz);
                    if (bob.LoadedOk)
                    {
                        if (bob.ExpandCount > 0)
                        {
                            expandList.Add(bob);
                            bob.ExpandedList = new List<BinaryOcrBitmap>();
                            for (int i = 1; i < bob.ExpandCount; i++)
                            {
                                BinaryOcrBitmap expandedBob = new BinaryOcrBitmap(gz);
                                if (expandedBob.LoadedOk)
                                {
                                    bob.ExpandedList.Add(expandedBob);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            list.Add(bob);
                        }
                    }
                    else
                    {
                        done = true;
                    }
                }
            }

            this.CompareImages = list;
            this.CompareImagesExpanded = expandList;
        }
Example #28
0
        public Bitmap ToOldBitmap(Color color)
        {
            if (ExpandedList != null && ExpandedList.Count > 0)
            {
                int minX = X;
                int minY = Y;
                int maxX = X + Width;
                int maxY = Y + Height;
                var list = new List <BinaryOcrBitmap>();
                list.Add(this);
                foreach (BinaryOcrBitmap bob in ExpandedList)
                {
                    if (bob.X < minX)
                    {
                        minX = bob.X;
                    }
                    if (bob.Y < minY)
                    {
                        minY = bob.Y;
                    }
                    if (bob.X + bob.Width > maxX)
                    {
                        maxX = bob.X + bob.Width;
                    }
                    if (bob.Y + bob.Height > maxY)
                    {
                        maxY = bob.Y + bob.Height;
                    }
                    list.Add(bob);
                }
                var nbmp = new BinaryOcrBitmap(maxX - minX, maxY - minY);
                foreach (BinaryOcrBitmap bob in list)
                {
                    for (int y = 0; y < bob.Height; y++)
                    {
                        for (int x = 0; x < bob.Width; x++)
                        {
                            int c = bob.GetPixel(x, y);
                            if (c > 0)
                            {
                                nbmp.SetPixel(bob.X - minX + x, bob.Y - minY + y, 1);
                            }
                        }
                    }
                }

                return(nbmp.ToOldBitmap(color)); // Resursive
            }
            else
            {
                var nbmp = new NikseBitmap(Width, Height);
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        Color c = Color.Transparent;
                        if (GetPixel(x, y) > 0)
                        {
                            c = color;
                        }
                        nbmp.SetPixel(x, y, c);
                    }
                }
                return(nbmp.GetBitmap());
            }
        }
        public Bitmap ToOldBitmap()
        {
            if (ExpandedList != null && ExpandedList.Count > 0)
            {
                int minX = X;
                int minY = Y;
                int maxX = X + Width;
                int maxY = Y + Height;
                var list = new List<BinaryOcrBitmap>();
                list.Add(this);
                foreach (BinaryOcrBitmap bob in ExpandedList)
                {
                    if (bob.X < minX)
                        minX = bob.X;
                    if (bob.Y < minY)
                        minY = bob.Y;
                    if (bob.X + bob.Width > maxX)
                        maxX = bob.X + bob.Width;
                    if (bob.Y + bob.Height > maxY)
                        maxY = bob.Y + bob.Height;
                    list.Add(bob);
                }
                var nbmp = new BinaryOcrBitmap(maxX - minX, maxY - minY);
                foreach (BinaryOcrBitmap bob in list)
                {
                    for (int y = 0; y < bob.Height; y++)
                    {
                        for (int x = 0; x < bob.Width; x++)
                        {
                            int c = bob.GetPixel(x, y);
                            if (c > 0)
                                nbmp.SetPixel(bob.X - minX + x, bob.Y - minY + y, 1);
                        }
                    }
                }

                return nbmp.ToOldBitmap(); // Resursive
            }
            else
            {
                var nbmp = new NikseBitmap(Width, Height);
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        Color c = Color.Transparent;
                        if (GetPixel(x, y) > 0)
                            c = Color.White;
                        nbmp.SetPixel(x, y, c);
                    }
                }
                return nbmp.GetBitmap();
            }
        }
        /// <summary>
        /// The to old bitmap.
        /// </summary>
        /// <returns>
        /// The <see cref="Bitmap"/>.
        /// </returns>
        public Bitmap ToOldBitmap()
        {
            if (this.ExpandedList != null && this.ExpandedList.Count > 0)
            {
                int minX = this.X;
                int minY = this.Y;
                int maxX = this.X + this.Width;
                int maxY = this.Y + this.Height;
                List<BinaryOcrBitmap> list = new List<BinaryOcrBitmap>();
                list.Add(this);
                foreach (BinaryOcrBitmap bob in this.ExpandedList)
                {
                    if (bob.X < minX)
                    {
                        minX = bob.X;
                    }

                    if (bob.Y < minY)
                    {
                        minY = bob.Y;
                    }

                    if (bob.X + bob.Width > maxX)
                    {
                        maxX = bob.X + bob.Width;
                    }

                    if (bob.Y + bob.Height > maxY)
                    {
                        maxY = bob.Y + bob.Height;
                    }

                    list.Add(bob);
                }

                BinaryOcrBitmap nbmp = new BinaryOcrBitmap(maxX - minX, maxY - minY);
                foreach (BinaryOcrBitmap bob in list)
                {
                    for (int y = 0; y < bob.Height; y++)
                    {
                        for (int x = 0; x < bob.Width; x++)
                        {
                            int c = bob.GetPixel(x, y);
                            if (c > 0)
                            {
                                nbmp.SetPixel(bob.X - minX + x, bob.Y - minY + y, 1);
                            }
                        }
                    }
                }

                return nbmp.ToOldBitmap(); // Resursive
            }
            else
            {
                NikseBitmap nbmp = new NikseBitmap(this.Width, this.Height);
                for (int y = 0; y < this.Height; y++)
                {
                    for (int x = 0; x < this.Width; x++)
                    {
                        Color c = Color.Transparent;
                        if (this.GetPixel(x, y) > 0)
                        {
                            c = Color.White;
                        }

                        nbmp.SetPixel(x, y, c);
                    }
                }

                return nbmp.GetBitmap();
            }
        }
        private void buttonAddBetterMatch_Click(object sender, EventArgs e)
        {
            if (listBoxInspectItems.SelectedIndex < 0)
                return;

            if (listBoxInspectItems.Items[listBoxInspectItems.SelectedIndex].ToString() == textBoxText.Text)
            {
                textBoxText.SelectAll();
                textBoxText.Focus();
                return;
            }

            if (_selectedCompareNode != null)
            {
                XmlNode newNode = ImageCompareDocument.CreateElement("Item");
                XmlAttribute text = newNode.OwnerDocument.CreateAttribute("Text");
                text.InnerText = textBoxText.Text;
                newNode.Attributes.Append(text);

                string databaseName = Path.Combine(_directoryPath, "Images.db");
                FileStream f;
                long pos = 0;
                if (!File.Exists(databaseName))
                {
                    using (f = new FileStream(databaseName, FileMode.Create))
                    {
                        pos = f.Position;
                        new ManagedBitmap(pictureBoxInspectItem.Image as Bitmap).AppendToStream(f);
                    }
                }
                else
                {
                    using (f = new FileStream(databaseName, FileMode.Append))
                    {
                        pos = f.Position;
                        new ManagedBitmap(pictureBoxInspectItem.Image as Bitmap).AppendToStream(f);
                    }
                }
                string name = pos.ToString(CultureInfo.InvariantCulture);
                newNode.InnerText = name;

                SetItalic(newNode);
                ImageCompareDocument.DocumentElement.AppendChild(newNode);

                int index = listBoxInspectItems.SelectedIndex;
                _matches[index].Name = name;
                _matches[index].ExpandCount = 0;
                _matches[index].Italic = checkBoxItalic.Checked;
                _matches[index].Text = textBoxText.Text;
                listBoxInspectItems.Items.Clear();
                for (int i = 0; i < _matches.Count; i++)
                    listBoxInspectItems.Items.Add(_matches[i].Text);
                listBoxInspectItems.SelectedIndex = index;
                ShowCount();
                listBoxInspectItems_SelectedIndexChanged(null, null);
            }
            else if (_selectedCompareBinaryOcrBitmap != null)
            {
                var nbmp = new NikseBitmap((pictureBoxInspectItem.Image as Bitmap));
                int x = 0;
                int y = 0;
                if (_selectedMatch != null && _selectedMatch.ImageSplitterItem != null)
                {
                    x = _selectedMatch.X;
                    y = _selectedMatch.Y;
                }
                var bob = new BinaryOcrBitmap(nbmp, checkBoxItalic.Checked, 0, textBoxText.Text, x, y);
                _binOcrDb.Add(bob);

                int index = listBoxInspectItems.SelectedIndex;
                _matches[index].Name = bob.Key;
                _matches[index].ExpandCount = 0;
                _matches[index].Italic = checkBoxItalic.Checked;
                _matches[index].Text = textBoxText.Text;
                listBoxInspectItems.Items.Clear();
                for (int i = 0; i < _matches.Count; i++)
                    listBoxInspectItems.Items.Add(_matches[i].Text);
                listBoxInspectItems.SelectedIndex = index;
                listBoxInspectItems_SelectedIndexChanged(null, null);
                ShowCount();
            }
        }
Example #32
0
        private double GetPixelDifPercentage(BinaryOcrBitmap expanded, BinaryOcrBitmap bobNext, NikseBitmap nbmpNext)
        {
            var difColoredPercentage = (Math.Abs(expanded.NumberOfColoredPixels - bobNext.NumberOfColoredPixels)) * 100.0 / (bobNext.Width * bobNext.Height);
            if (difColoredPercentage > 1 && expanded.Width < 3 || bobNext.Width < 3)
                return 100;

            int dif = int.MaxValue;
            if (expanded.Height == bobNext.Height && expanded.Width == bobNext.Width)
            {
                dif = NikseBitmapImageSplitter.IsBitmapsAlike(nbmpNext, expanded);
            }
            else if (expanded.Height == bobNext.Height && expanded.Width == bobNext.Width + 1)
            {
                dif = NikseBitmapImageSplitter.IsBitmapsAlike(nbmpNext, expanded);
            }
            else if (expanded.Height == bobNext.Height && expanded.Width == bobNext.Width - 1)
            {
                dif = NikseBitmapImageSplitter.IsBitmapsAlike(expanded, nbmpNext);
            }
            else if (expanded.Width == bobNext.Width && expanded.Height == bobNext.Height + 1)
            {
                dif = NikseBitmapImageSplitter.IsBitmapsAlike(nbmpNext, expanded);
            }
            else if (expanded.Width == bobNext.Width && expanded.Height == bobNext.Height - 1)
            {
                dif = NikseBitmapImageSplitter.IsBitmapsAlike(expanded, nbmpNext);
            }
            var percentage = dif * 100.0 / (bobNext.Width * bobNext.Height);
            return percentage;
        }
Example #33
0
        public int Add(BinaryOcrBitmap bob)
        {
            int index;
            if (bob.ExpandCount > 0)
            {
                if (bob.ExpandedList == null || bob.ExpandCount -1 != bob.ExpandedList.Count )
                    throw new Exception("BinaryOcrDb.Add: There should be " + (bob.ExpandCount -1) + " sub image(s)");

                if (bob.ExpandedList[0].Text != null)
                    throw new Exception("BinaryOcrDb.Add: sub image should have null text");
                index = FindExactMatchExpanded(bob);
                if (index == -1 || CompareImagesExpanded[index].ExpandCount != bob.ExpandCount)
                {
                    CompareImagesExpanded.Add(bob);
                }
                else
                {
                    bool allAlike = true;
                    for (int i = 0; i < bob.ExpandCount - 1; i++)
                    {
                        if (bob.ExpandedList[i].Hash != CompareImagesExpanded[index].ExpandedList[i].Hash)
                            allAlike = false;
                        if (bob.ExpandedList[i].Text != null)
                            throw new Exception("BinaryOcrDb.Add: sub image should have null text");
                    }
                    if (!allAlike)
                        CompareImagesExpanded.Add(bob);
                    else
                       throw new Exception("BinaryOcrDb.Add: Expanded image already in db!");
                }
            }
            else
            {
                index = FindExactMatch(bob);
                if (index == -1)
                    CompareImages.Add(bob);
                else
                    throw new Exception("BinaryOcrDb.Add: Image already in db!");
            }
            return index;
        }
Example #34
0
        private string SaveCompareItemNew(ImageSplitterItem newTarget, string text, bool isItalic, List<ImageSplitterItem> expandList)
        {
            int expandCount = 0;
            if (expandList != null)
                expandCount = expandList.Count;

            if (expandCount > 0)
            {
                var bob = new BinaryOcrBitmap(expandList[0].NikseBitmap, isItalic, expandCount, text, expandList[0].X, expandList[0].Top);
                bob.ExpandedList = new List<BinaryOcrBitmap>();
                for (int j = 1; j < expandList.Count; j++)
                {
                    var expandedBob = new BinaryOcrBitmap(expandList[j].NikseBitmap);
                    expandedBob.X = expandList[j].X;
                    expandedBob.Y = expandList[j].Top;
                    bob.ExpandedList.Add(expandedBob);
                }
                _binaryOcrDb.Add(bob);
                _binaryOcrDb.Save();
                return bob.Key;
            }
            else
            {
                var bob = new BinaryOcrBitmap(newTarget.NikseBitmap, isItalic, expandCount, text, newTarget.X, newTarget.Top);
                _binaryOcrDb.Add(bob);
                _binaryOcrDb.Save();
                return bob.Key;
            }
        }
Example #35
0
        private CompareMatch GetCompareMatchNew(ImageSplitterItem targetItem, out CompareMatch secondBestGuess, List<ImageSplitterItem> list, int listIndex)
        {
            double maxDiff = _numericUpDownMaxErrorPct;
            secondBestGuess = null;
            int index = 0;
            int smallestDifference = 10000;
            int smallestIndex = -1;
            NikseBitmap target = targetItem.NikseBitmap;
            if (_binaryOcrDb == null)
            {
                return null;
            }

            var bob = new BinaryOcrBitmap(target);

            for (int k = 0; k < _binaryOcrDb.CompareImagesExpanded.Count; k++)
            {
                var b = _binaryOcrDb.CompareImagesExpanded[k];
                if (bob.Hash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels)
                {
                    bool ok = false;
                    for (int i = 0; i < b.ExpandedList.Count; i++)
                    {
                        if (listIndex + i + 1 < list.Count && list[listIndex + i + 1].NikseBitmap != null && b.ExpandedList[i].Hash == new BinaryOcrBitmap(list[listIndex + i + 1].NikseBitmap).Hash)
                        {
                            ok = true;
                        }
                        else
                        {
                            ok = false;
                            break;
                        }
                    }
                    if (ok)
                    {
                        secondBestGuess = null;
                        return new CompareMatch(b.Text, b.Italic, b.ExpandCount, b.Key);
                    }
                }
            }

            FindBestMatchNew(ref index, ref smallestDifference, ref smallestIndex, target, _binaryOcrDb, bob, maxDiff);

            if (smallestIndex >= 0)
            {
                double differencePercentage = smallestDifference * 100.0 / (target.Width * target.Height);
                if (differencePercentage <= maxDiff)
                {
                    var hit = _binaryOcrDb.CompareImages[smallestIndex];

                    string text = hit.Text;
                    if (smallestDifference > 0)
                    {
                        int h = hit.Height;
                        if (text == "V" || text == "W" || text == "U" || text == "S" || text == "Z" || text == "O" || text == "X" || text == "Ø" || text == "C")
                        {
                            if (_binOcrLastLowercaseHeight > 3 && h - _binOcrLastLowercaseHeight < 2)
                                text = text.ToLower();
                        }
                        else if (text == "v" || text == "w" || text == "u" || text == "s" || text == "z" || text == "o" || text == "x" || text == "ø" || text == "c")
                        {
                            if (_binOcrLastUppercaseHeight > 3 && _binOcrLastUppercaseHeight - h < 2)
                                text = text.ToUpper();
                        }
                    }
                    else
                    {
                        SetBinOcrLowercaseUppercase(hit.Height, text);
                    }

                    return new CompareMatch(text, hit.Italic, hit.ExpandCount, hit.Key);
                }

                var guess = _binaryOcrDb.CompareImages[smallestIndex];
                secondBestGuess = new CompareMatch(guess.Text, guess.Italic, guess.ExpandCount, guess.Key);
            }

            return null;
        }