为什么我没有找回正确的颜色

时间:2019-07-07 19:56:42

标签: c# colors bitmap

我有一个连接到网络摄像头的视频播放器。然后,我在视频区域周围绘制一个蓝框,然后将其复制,然后取像素的平均强度。我遇到的问题是,我知道我裁切了正确的零件,但是我没有找回正确的颜色值。

就像我有一件红色运动衫,但我得到的价值观是 R = 21,G = 21,B = 102 所以我在网上使用了一个网站: https://www.rapidtables.com/web/color/RGB_Color.html#rgb-format

确保值正确,但是我找回了错误的值  R = 21 G = 21 B = 102 是某种深蓝色。 所以我不确定我在做什么错。

视频采用Format32bppArgb格式

 public double[,] imageToDoubleArray(Bitmap _image)
    {
        Bitmap b = new Bitmap(_image);

        System.Drawing.Imaging.BitmapData bData = b.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, b.PixelFormat);//b.PixelFormat);

        /* GetBitsPerPixel just does a switch on the PixelFormat and returns the number */
        byte bitsPerPixel = GetBitsPerPixel(bData.PixelFormat);

        /*the size of the image in bytes */
        int size = bData.Stride * bData.Height;

        /*Allocate buffer for image*/
        byte[] data = new byte[size];
        double[,] returndata = new double[_image.Height, _image.Width];
        /*This overload copies data of /size/ into /data/ from location specified (/Scan0/)*/
        System.Runtime.InteropServices.Marshal.Copy(bData.Scan0, data, 0, size);
        int i = 0;
        // looping through the whole array
        for (int y = 0; y < _image.Height; y++)
        {

            for (int x = 0; x < _image.Width; x++)
            {


                double magnitude = 1 / 3d * (data[i] + data[i + 1] + data[i + 2]);

                // make a new color object
                Color t = new Color();
                // now set the color to red, Green and blue
                t = Color.FromArgb(data[i], data[i + 1], data[i + 2]);
                // printing out the colors to the text box
                ColorTxT.Text = Convert.ToString(t);
                returndata[y, x] = magnitude;

                i += bitsPerPixel / 8;
                if (i == data.Length)
                    break;
            }
        }

        return returndata;

    }

我应该变回红色

0 个答案:

没有答案