How to determine the color of the letters in these images?

I use this article to solve captchas. It works by removing the background from the image using AForge, and then applying Tesseract OCR to the resulting cleaned image.

The problem is that it is currently used for black letters, and since each stroke has a different text color, I need to either transfer the color to the image cleaner or change the color of the letters to black. To do this, I need to know what the existing color of the letters is.

How can I determine the color of letters?

Image with letters in it

Image with letters in it

+3
source share
2 answers

Using the answer from @Robert Harvey ♦ I went and developed the same code using LockBits and unsafe to improve speed. You must compile the "Allow insecure code" checkbox. Note that the order of the pixels returned from the image is in bgr not rgb format, and I am blocking the bitmap using Format24bppRgb format to force it to use 3 bytes per color.

 public unsafe Color GetTextColour(Bitmap bitmap) { BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); try { const int bytesPerPixel = 3; const int red = 2; const int green = 1; int halfHeight = bitmap.Height / 2; byte* row = (byte*)_bitmapData.Scan0 + (halfHeight * _bitmapData.Stride); Color startingColour = Color.FromArgb(row[red], row[green], row[0]); for (int wi = bytesPerPixel, wc = _bitmapData.Width * bytesPerPixel; wi < wc; wi += bytesPerPixel) { Color thisColour = Color.FromArgb(row[wi + red], row[wi + green], row[wi]); if (thisColour != startingColour) { return thisColour; } } return Color.Empty; //Or some other default value } finally { bitmap.UnlockBits(bitmapData); } } 
+3
source

The solution to this particular problem turned out to be relatively simple. All I had to do was get the color of the edge pixel halfway down the left side of the image, scan the pixels to the right until the color changes, and the color of the first letter.

 public Color GetTextColor(Bitmap bitmap) { var y = bitmap.Height/2; var startingColor = bitmap.GetPixel(0, y); for (int x = 1; x < bitmap.Width; x++) { var thisColor = bitmap.GetPixel(x, y); if (thisColor != startingColor) return thisColor; } return null; } 
+2
source

Source: https://habr.com/ru/post/1014392/


All Articles