سؤال

This is the code I've implemented for detecting the skin color of the image with hand, but it takes about 5 minutes to check each photo...Does anyone know how can I implement it in a much faster way?Thanks!

using (Bitmap SampleImage = (Bitmap)Image.FromFile (path)) { 
    for (int x = 0; x < SampleImage.Width; x++) {
        for (int y = 0; y < SampleImage.Height; y++) {
            Color pixelColor = SampleImage.GetPixel (x, y);
            int r = pixelColor.R;
            int g = pixelColor.G;
            int b = pixelColor.B;
            int differenceMinMax =
                Math.Max (r, Math.Max (g, b)) - Math.Min (r, Math.Min (g, b));

            if (r > 95 & g > 40 & b > 20 & differenceMinMax > 15 & r > g & r > b) {
                SampleImage.SetPixel (x, y, Color.White);
            } else if (r > 220 & g > 210 & b > 170) {
                SampleImage.SetPixel (x, y, Color.White);
            } else {
                SampleImage.SetPixel (x, y, Color.Black);
            }

            SampleImage.Save (path, System.Drawing.Imaging.ImageFormat.Jpeg); 
        }
    }
}
هل كانت مفيدة؟

المحلول

  1. Move SampleImage.Save outside of the loops.
  2. Inner loop should iterate by X coordinate. It reduces the number of CPU cache misses.
  3. Get off slow GetPixel and SetPixel methods as suggested in comments above.
  4. Parallelize inner loop http://msdn.microsoft.com/en-us/library/dd783584(v=vs.110).aspx#fbid=IJmDXumNJjA
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top