皮肤检测,setPixel和getPixel nad Colors问题

时间:2019-06-06 08:14:41

标签: c# emgucv skin

我从网络摄像头检测皮肤时遇到问题。我尝试使用setPixel和getPixel,但是它s too slow and teacher said I must use other method. I found other method and it可以正常工作,但它给了我其他输出图像。为什么?看起来像是其他形式的相同代码。感谢您的帮助

 Image<Bgr, byte> Imagebinary = capture.QueryFrame().ToImage<Bgr, Byte>();
        Bitmap bitmap = new Bitmap(Imagebinary.Bitmap);
        unsafe
        {
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
            int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(bitmap.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 oldBlue = currentLine[x];
                    int oldGreen = currentLine[x + 1];
                    int oldRed = currentLine[x + 2];

                    // calculate new pixel value
                    currentLine[x] = (byte)oldBlue;
                    currentLine[x + 1] = (byte)oldGreen;
                    currentLine[x + 2] = (byte)oldRed;
                    if ((oldBlue < oldRed * 0.7) && oldRed > 60 && (oldGreen < oldRed * 0.85) && (oldBlue > oldRed * 0.2) && (oldGreen > oldRed * 0.4))
                    {
                        currentLine[x] = 100;
                        currentLine[x + 1] = 100;
                        currentLine[x + 2] = 100;
                    }

                    else
                    {
                        currentLine[x] = 0;
                        currentLine[x + 1] = 0;
                        currentLine[x + 2] = 0;
                    }
                }
            }
            bitmap.UnlockBits(bitmapData);
        }
        Image<Hsv, Byte> My_Image = new Image<Hsv, Byte>(bitmap);
        imageBox1.Image = My_Image;
    }

//////////////////////原始代码,但工作良好但缓慢/////////////////

    Bitmap bitmap = new Bitmap(Imagebinary.Bitmap);
    var startx = 0;
    var starty = 0;
    var endx = bitmap.Width;
    var endy = bitmap.Height;

        for (int y = starty; y<endy; y++)
        {
            for (int x = startx; x<endx; x++)
            {
                var p = bitmap.GetPixel(x, y);
                if ((p.B<p.R* 0.7) && p.R> 60 && (p.G<p.R* 0.85) && (p.B > p.R* 0.2) && (p.G > p.R* 0.4))
                {
                    bitmap.SetPixel(x, y, Color.White);
                }
                else
                    bitmap.SetPixel(x, y, Color.Black);

            }
        }
        Image<Bgr, Byte> myImage = new Image<Bgr, Byte>(bitmap);
imageBox1.Image = myImage;

0 个答案:

没有答案
相关问题