/// <summary> Get the image signature for a specific file </summary> public ImageInfo GetSignature(string sFile) { ImageInfo ImageInfo = new ImageInfo { File = sFile, Section = new SectionInfo[_iGridSize] }; Bitmap bmp = new Bitmap(sFile); ImageHelper.BitmapBytes BitmapBytes = new ImageHelper.BitmapBytes(bmp); ImageInfo.AspectRatio = (int)((float)bmp.Height / (float)bmp.Width * 100); DateTime dt = DateTime.Now; for (int iRow = 0; iRow < _iRows; iRow++) { for (int iCol = 0; iCol < _iCols; iCol++) { Coords Coords = GetSectionCoords(iRow, iCol, _iRows, _iCols, bmp.Width, bmp.Height); int iSection = (iRow * _iCols) + iCol; ImageInfo.Section[iSection] = GetHisto(Coords, BitmapBytes); SectionInfo EdgeVars = GetEdge(Coords, BitmapBytes, false); ImageInfo.Section[iSection].ChangeCount = EdgeVars.ChangeCount; ImageInfo.Section[iSection].ChangeDistance = EdgeVars.ChangeDistance; } } TimeSpan duration = DateTime.Now - dt; Console.WriteLine("GetSignature Duration: " + duration.ToString()); return(ImageInfo); }
public Bitmap GetTestImage(string sFile) { Bitmap bmp = new Bitmap(sFile); ImageHelper.BitmapBytes BitmapBytes = new ImageHelper.BitmapBytes(bmp); GetEdge(new Coords { Left = 0, Top = 0, Bottom = bmp.Height - 1, Right = bmp.Width - 1 }, BitmapBytes, true); BitmapBytes.UnlockBitmap(true); return(bmp); }
/// <summary> Calculate a number representing the difference between it and the pixels around it /// basically a comparison of the r, g and b of 3 pixels to the bottom right /// This depends on the calling method cycling through all pixels from left to right and downwards, /// this prevents having to compare all eight surrounding pixels which would duplicate effort </summary> public int GetPixelDiff(ImageHelper.BitmapBytes BitmapBytes, int x, int y) { int iRGB; int iDiff = 0; for (iRGB = 0; iRGB < 3; iRGB++) { // Middle Right iDiff += System.Math.Abs(BitmapBytes.GetPixelPart(x, y, (RGB)iRGB) - BitmapBytes.GetPixelPart(x + 1, y, (RGB)iRGB)); // Bottom Right iDiff += System.Math.Abs(BitmapBytes.GetPixelPart(x, y, (RGB)iRGB) - BitmapBytes.GetPixelPart(x + 1, y + 1, (RGB)iRGB)); // Bottom Middle iDiff += System.Math.Abs(BitmapBytes.GetPixelPart(x, y, (RGB)iRGB) - BitmapBytes.GetPixelPart(x, y + 1, (RGB)iRGB)); } return(iDiff); }
/// <summary> Get the edge detection values for a section of an image </summary> /// <param name="Coords">Coords specify section of the image to analyse</param> /// <param name="bModifyImage">if true change the image to show pattern, for debug purposes</param> private SectionInfo GetEdge(Coords Coords, ImageHelper.BitmapBytes BitmapBytes, bool bModifyImage) { int x, y; int iPixelCount = 0; int iPixelDist = 0; SectionInfo EdgeValues = new SectionInfo(); for (x = Coords.Left; x < Coords.Right - 1; x++) { for (y = Coords.Top; y < Coords.Bottom - 1; y++) { if (GetPixelDiff(BitmapBytes, x, y) > PIXEL_DIFF_TOLERENCE) { iPixelCount++; iPixelDist += GetDistance(x - Coords.Left, y - Coords.Top); if (bModifyImage) { BitmapBytes.SetPixel(x, y, 255, 255, 255); } } else if (bModifyImage) { BitmapBytes.SetPixel(x, y, 0, 0, 0); } } } if (iPixelCount != 0 && iPixelDist != 0) { iPixelDist /= iPixelCount; } EdgeValues.ChangeCount = Convert.ToUInt16((float)iPixelCount / (float)GetPixelCount(Coords) * 100); int iMaxChange = GetDistance(Coords.Right - Coords.Left, Coords.Bottom - Coords.Top); EdgeValues.ChangeDistance = Convert.ToUInt16((float)iPixelDist / iMaxChange * 100); return(EdgeValues); }
/// <summary> Calculate the colour histogram for this section of the image </summary> private SectionInfo GetHisto(Coords Coords, ImageHelper.BitmapBytes BitmapBytes) { int[] Hue = new int[_iNumBars]; int x, y; SectionInfo SectionInfo = new SectionInfo(); for (x = Coords.Left; x < Coords.Right - 1; x++) { for (y = Coords.Top; y < Coords.Bottom - 1; y++) { Color pixel = BitmapBytes.GetHSIFromPoint(x, y); float fHue = pixel.GetHue(); int iIll = (int)(pixel.GetBrightness() * 100); int iSat = (int)(pixel.GetSaturation() * 100); bool bColour = false; // Check for colour or black and white // based on various illumination and saturation tolerances if (iSat > 10) { if (iIll > 15 && iIll < 91) { if (((100 - iIll) + iSat) > 35) { if (iSat + iIll > 25) { int iSegment = Convert.ToInt32(Math.Floor(fHue / HUE_MAX * (_iNumBars - 2))); Hue[iSegment]++; bColour = true; } } } } // If this was b/w store either b or w based on the illumination if (!bColour) { if (iIll > 50) { Hue[_iNumBars - 2] += 1; } else { Hue[_iNumBars - 1] += 1; } } } } int iCount; int iPixelCount = (Coords.Bottom - Coords.Top + 1) * (Coords.Right - Coords.Left + 1); iPixelCount = GetPixelCount(Coords); for (iCount = 0; iCount < _iNumBars; iCount++) { float fPercent = (float)Hue[iCount] / iPixelCount * 100; Hue[iCount] = (int)fPercent; } SectionInfo.Hues = Hue; return(SectionInfo); }