将索引的8bpp图像转换为数组. [英] convert image 8bpp indexed into array..

查看:63
本文介绍了将索引的8bpp图像转换为数组.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都请帮助..如何将图像8bppIndexed转换为数组并将数组转换为图像8bppIndexed .. ??给我一些示例代码,类似于下面的代码..我的下面代码仅适用于24bppRgb ..现在我需要8bppIndexed ..请帮助我.....

我已经使用RGB颜色24bppRgb将图像转换为数组:

anyone please help.. how to convert image 8bppIndexed into array and convert array into image 8bppIndexed..?? give me some example coding which is resemble of my code below.. my code below only for 24bppRgb.. now i need for 8bppIndexed.. please help me.....

i have convert image into array using RGB colour 24bppRgb :

public static void convertRgbImageToArray(Bitmap bm, out double[,] r, out double[,] g, out double[,] b) 
        { 
            // make an array that would be refeneccing into the function
            r = new double[bm.Width, bm.Height];
            g = new double[bm.Width, bm.Height];
            b = new double[bm.Width, bm.Height];

            // threshold for image that has a 24bppRGB
            
            // Copy RGB value from the image
            Rectangle rect = new Rectangle(0,0,bm.Width,bm.Height);
            BitmapData bmd = bm.LockBits(
                                         rect,
                                         ImageLockMode.ReadOnly,
                                         bm.PixelFormat
                                         );
            int bytes = bmd.Stride * bmd.Height; 
            byte[] bgrVal = new byte[bytes];
            IntPtr ptr = bmd.Scan0;
            System.Runtime.InteropServices.Marshal.Copy(ptr, bgrVal, 0, bytes); // Copy data value RGB from pointer ptr into array bgrVal
            bm.UnlockBits(bmd);

            // devide array value into RGB colour (Red, Green, Blue).
            for (int x = 0; x < bm.Width; x++)
            {
                for (int y = 0; y < bm.Height; y++)
                {
                    int m = x * 3 + y * bmd.Stride;
                    b[x,y] = (bgrVal[m]);
                    g[x, y] = (bgrVal[m + 1]);
                    r[x, y] = (bgrVal[m + 2]);
                }
            }

            return;
        }


并将数组转换为RGB:


And to convert array into RGB :

public static Bitmap convertRgbArrayToImage(double[,]r,double[,]g, double[,]b)
        {
            // make image.
            Bitmap bm = new Bitmap(b.GetLength(0), b.GetLength(1), PixelFormat.Format24bppRgb);

            // make byte array that use for the image
            // Copy RGB value from image.
            Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height);
            BitmapData bmd = bm.LockBits(
                                         rect,
                                         ImageLockMode.ReadOnly,
                                         bm.PixelFormat
                                         );

            int bytes = bmd.Stride * bmd.Height;
            byte[] bgrVal = new byte[bytes];

            // Copy pixel value into byte array
            for (int x = 0; x < bm.Width; x++)
            {
                for (int y = 0; y < bm.Height; y++)
                {
                    int m = x * 3 + y * bmd.Stride;
                    double bb = (b[x, y]);
                    if (bb < 0) bb = 0;
                    if (bb > 255) bb = 255;
                    bgrVal[m] = (byte)bb;

                    double gg = (g[x, y]);
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    bgrVal[m + 1] = (byte)gg;

                    double rr = (r[x, y]);
                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    bgrVal[m + 2] = (byte)rr;
                }
            }
            IntPtr ptr=bmd.Scan0;
            System.Runtime.InteropServices.Marshal.Copy(bgrVal, 0, ptr, bytes); // Copy array RGB value into pointer.
            bm.UnlockBits(bmd);
            return bm;
        }

推荐答案

您不需要RGB的双精度",它们的范围是0-> 255,但这只是

您是否要返回索引或RGB值?基本上,24bppRgb之间的区别是每个像素3个字节,每个颜色分量1个字节.在8bppIndexed中,它为1,但不是返回您的RGB值,而是返回一个索引,然后您可以在位图的调色板中查找该索引-这将提供您的RGB值.

因此,在执行"x * 3"的地方,删除"* 3",并删除引用bgrVal [m + 1]和bgrVal [m + 2]的位,然后在位图上使用bgrVal [m]调色板参数
You don''t need "doubles" for your RGB''s, their range is 0 -> 255, but that''s by-the-by

Are you wanting to return the indices, or the RGB values? Basically, the difference between 24bppRgb is that there are 3 bytes per pixel, one for each colour component. In 8bppIndexed, there''s 1, but rather than returning you your RGB value, it returns you an index that you can then look up against the palette of your bitmap - and this will provide your RGB values.

So where you do "x * 3", remove the "* 3", and remove the bits wher you reference bgrVal[m + 1] and bgrVal[m + 2], then use bgrVal[m] on the bitmap''s palette parameter


这篇关于将索引的8bpp图像转换为数组.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆