private CGColor[,] GetOilPaintingColours(UIImage image) { var width = image.CGImage.Width; var height = image.CGImage.Height; var oilColours = new CGColor[width, height]; var radius = 2; var intensity = 15; var intensityCount = new long[256]; var sumR = new float[256]; var sumG = new float[256]; var sumB = new float[256]; for( int pixelY = 0; pixelY < height; pixelY++) { for( int pixelX = 0; pixelX < width; pixelX++) { // Find intensities of nearest nRadius pixels in four direction. for( int borderPixelY = pixelY - radius; borderPixelY <= pixelY + radius; borderPixelY++ ) { for( int borderPixelX = pixelX - radius; borderPixelX <= pixelX + radius; borderPixelX++ ) { if(borderPixelX < 0 || borderPixelY < 0 || borderPixelX >= width || borderPixelY >= height) { continue; } var pixelColour = image.GetPixelColour(new CGPoint(borderPixelX, borderPixelY)); int currentPixelRed = (int)Math.Round(pixelColour.Components[0] * 255); int currentPixelGreen = (int)Math.Round(pixelColour.Components[1] * 255); int currentPixelBlue = (int)Math.Round(pixelColour.Components[2] * 255); // Find intensity of RGB value and apply intensity level. int currentIntensity = (int)( ( (float)( currentPixelRed + currentPixelGreen + currentPixelBlue ) / 3.0 ) * intensity ) / 255; if( currentIntensity > 255 ) currentIntensity = 255; int i = currentIntensity; intensityCount[i]++; sumR[i] = sumR[i] + currentPixelRed; sumG[i] = sumG[i] + currentPixelGreen; sumB[i] = sumB[i] + currentPixelBlue; } } nfloat currentMax = 0; int maxIndex = 0; for( var index = 0; index < 256; index++ ) { if( intensityCount[index] > currentMax ) { currentMax = intensityCount[index]; maxIndex = index; } } oilColours[pixelX, pixelY] = new CGColor((sumR[maxIndex] / currentMax) / 255f, (sumG[maxIndex] / currentMax) / 255f, (sumB[maxIndex] / currentMax) / 255f, 1); Array.Clear(intensityCount, 0, intensityCount.Length); Array.Clear(sumR, 0, sumR.Length); Array.Clear(sumG, 0, sumG.Length); Array.Clear(sumB, 0, sumB.Length); } } return oilColours; }