none
10 bit bmp video RRS feed

  • Question

  • so I have values coming from a camera 0-1023 so 10 bits.  and I display this in a video. that part seems to be working.

    but when I try to get the values back from the video, I run in a big problem.

    my highest value is 1020, but my lowest value is 255 and not 0.

    why ?

    here is my code :

     public double[,] imageToByteArray(Bitmap _image)
            {
                Bitmap b = new Bitmap(_image);
    
                BitmapData bData = b.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, 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;
                for (int y = 0; y < _image.Height; y++)
                {
    
                    for (int x = 0; x < _image.Width; x++)
                    {
    
                        double magnitude = data[i] + data[i + 1] + data[i + 2] + data[i +3]; //  1 / 3d * (data[i] + data[i + 1] + data[i + 2]);
    
                        returndata[y, x] = magnitude;
                        i += bitsPerPixel / 8;
                    }
                }
    
                return returndata;
    
            }

    Monday, June 17, 2019 9:14 PM

All replies

  • Are you just throwing random code together without thinking about what the code does?  You are assuming in your loop that all pixels consist of 4 bytes, and you are adding up all 4 bytes.  If your image is Format32bppArgb, then you are adding in the alpha value, which is always 255.  The pixels will run from (255,0,0,0) for black to (255,255,255,255) for white.  That means the minimum sum will be 255, the maximum will be 1020.

    However, that's not 10-bit color.  That's 8-bit color, and you're computing gray x 3 + 255.

    What do you want here?  As another poster pointed out, you cannot display more than 8 bits per component, and you cannot print more than 8 bits per component.  You can certainly do data analysis on the 10-bit bitmap, but you'll do that as an array of shorts, not an image.  If you want to display it, then you need to divide the pixel value by 4 when creating the bitmap.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, June 18, 2019 6:46 AM
  • Hi Btb4198,

    Thank you for posting here.

    Could you provide the related code about GetBitsPerPixel method?

    We will be appreciated if you provide the above information.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 18, 2019 7:15 AM
    Moderator
  •    private byte GetBitsPerPixel(System.Drawing.Imaging.PixelFormat pixelFormat)
            {
                byte BitsPerPixel = 0;
                switch (pixelFormat)
                {
                    case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                        BitsPerPixel = 8;
                        break;
                    case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                        BitsPerPixel = 24;
                        break;
                    case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                    case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
                        BitsPerPixel = 32;
                        break;
                    default:
                        BitsPerPixel = 0;
                        break;
                }
                return BitsPerPixel;
            }

    Tuesday, June 18, 2019 1:43 PM
  • Hi Btb4198,

    Thanks for giving the information.

    I have found the reason that why the lowest value is 255.

    After executing the following code, you will get the data byte array.

     System.Runtime.InteropServices.Marshal.Copy(bData.Scan0, data, 0, size);

    data  byte array:

    As you see in the above picture, it has a rule. There must be a value of 255 among the four consecutive values. Besides, If your i is 0, the following code is:

    double magnitude = data[0] + data[ 1] + data[ 2] + data[ 3];

    It is 255 and it is the lowest value because it only has the first three digits being consecutive zero value, and the latter values are only two consecutive zero value.

    Hope my explanation could be helpful.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 19, 2019 2:11 AM
    Moderator
  • Your explanation confuses me.  Yes, you have described the pattern, but not the reason.  The reason is that this is a PixelFormat32bppArgb image.  The 4th byte of each pixel is the alpha value, which will always be 255.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thursday, June 20, 2019 3:57 AM