private byte FindAverageThreshold(int min, int max) { int threshold = 0, count = 0; unsafe { BitmapData bitmapData = ProcessedBitmap.LockBits(new Rectangle(0, 0, ProcessedBitmap.Width, ProcessedBitmap.Height), ImageLockMode.ReadWrite, ProcessedBitmap.PixelFormat); int bytesPerPixel = Image.GetPixelFormatSize(ProcessedBitmap.PixelFormat) / 8; int heightInPixels = bitmapData.Height; int widthInBytes = bitmapData.Width * bytesPerPixel; byte *ptrFirstPixel = (byte *)bitmapData.Scan0; Parallel.For(0, heightInPixels, y => { byte *currentLine = ptrFirstPixel + (y * bitmapData.Stride); for (int x = 0; x < widthInBytes; x = x + bytesPerPixel) { int blue = currentLine[x]; int green = currentLine[x + 1]; int red = currentLine[x + 2]; var intensity = (byte)(0.3 * red + 0.6 * green + 0.1 * blue); if ((intensity >= min) && (intensity <= max)) { threshold += intensity; count++; } } }); ProcessedBitmap.UnlockBits(bitmapData); } return((byte)(threshold / count).TruncateRgb()); }
public void SetUpColors() { unsafe { BitmapData bitmapData = ProcessedBitmap.LockBits(new Rectangle(0, 0, ProcessedBitmap.Width, ProcessedBitmap.Height), ImageLockMode.ReadWrite, ProcessedBitmap.PixelFormat); int bytesPerPixel = Image.GetPixelFormatSize(ProcessedBitmap.PixelFormat) / 8; int heightInPixels = bitmapData.Height; int widthInBytes = bitmapData.Width * bytesPerPixel; byte *ptrFirstPixel = (byte *)bitmapData.Scan0; for (int y = 0; y < heightInPixels; y++) { byte *currentLine = ptrFirstPixel + (y * bitmapData.Stride); for (int x = 0; x < widthInBytes; x = x + bytesPerPixel) { int b = currentLine[x]; int g = currentLine[x + 1]; int r = currentLine[x + 2]; var color = Color.FromArgb(r, g, b); if (!_colorList.Contains(color)) { } _colorList.Add(color); } } ProcessedBitmap.UnlockBits(bitmapData); } }
public static Data Convert(ProcessedBitmap bitmap, DateRange dates) { ImageConverter converter = new ImageConverter(); byte[] bytes = (byte[])converter.ConvertTo(bitmap.Image, typeof(byte[])); return(new Data() { Image = bytes, FromDate = dates.FromDate, ToDate = dates.ToDate, Count = bitmap.NumberReadFromImage }); }
public ProcessedBitmap GetNumberFromProcessedImage(ParsedBitmap image, Location location, Rectangle cropLocation) { ProcessedBitmap processed = new ProcessedBitmap(imageProcessor); processed = processed.Process(image, location, cropLocation); try { processed.NumberReadFromImage = ReadImageText(processed.Image); } catch (NotSupportedException) { processed = processed.Process(image, location, cropLocation, BinarizationThreshold.Heavy); processed.NumberReadFromImage = ReadImageText(processed.Image); } return(processed);; }
private async Task <List <Data> > ProcessSingleImageFromUrl(string url, DateRange dateRange) { List <Data> linksData = new List <Data>(); if (url is null || dateRange is null) { return(linksData); } ParsedBitmap parsed = await webService.DownloadImage(url); ProcessedBitmap processed = ocrService.GetNumberFromProcessedImage(parsed, Location.Chilliwack, map[Location.Chilliwack]); linksData.Add(Data.Convert(processed, dateRange)); return(linksData); }
private void Dispose(bool Disposing) { if (!Disposed) { Disposed = true; SourceBitmap.Dispose(); SourceHandle.Free(); GrayScaleBitmap.Dispose(); GrayScaleHandle.Free(); ProcessedBitmap.Dispose(); ProcessedHandle.Free(); if (Disposing) { SourceBitmap = null; SourceArr = null; GrayScaleBitmap = null; GrayScaleArr = null; ProcessedBitmap = null; ProcessedArr = null; } } }
public DateRange ParseDateRangeFromUrl(ProcessedBitmap processed) { return(ParseDateRangeFromUrl(processed.Url, processed.Location)); }