0
 public static long[] GetHistogramRGB(Bitmap b)
        {
            long[] myHistogramBlue = new long[256];
            long[] myHistogramGreen = new long[256];
            long[] myHistogramRed = new long[256];
            BitmapData bmData = null;

            try
            {
                //Lock it fixed with 32bpp
                bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                int scanline = bmData.Stride;
                System.IntPtr Scan0 = bmData.Scan0;
                unsafe
                {
                    byte* p = (byte*)(void*)Scan0;
                    int nWidth = b.Width;
                    int nHeight = b.Height;
                    for (int y = 0; y < nHeight; y++)
                    {
                        for (int x = 0; x < nWidth; x++)
                        {
                            long Temp = 0;
                            Temp += p[0];
                            myHistogramBlue[Temp]++;

                            long Temp2 = 0;
                            Temp2 += p[1];
                            myHistogramGreen[Temp2]++;

                            long Temp3 = 0;
                            Temp3 += p[2];
                            myHistogramRed[Temp3]++;

                            //we do not need to use any offset, we always can increment by pixelsize when
                            //locking in 32bppArgb - mode
                            p += 4;
                        }
                    }
                }
                b.UnlockBits(bmData);
            }
            catch
            {
                try
                {
                    b.UnlockBits(bmData);
                }
                catch
                {
                }
            }

            List<long[]> l = new List<long[]>();
            l.Add(myHistogramBlue);
            l.Add(myHistogramGreen);
            l.Add(myHistogramRed);

            return l;

        }

在最后一行返回 l; 我收到错误:

Cannot implicitly convert type 'System.Collections.Generic.List<long[]>' to 'long[]'

我该如何解决?

我试着这样做:

public static long[] GetHistogramRGB(Bitmap b)
        {
            long[] myHistogramBlue = new long[256];
            long[] myHistogramGreen = new long[256];
            long[] myHistogramRed = new long[256];
            BitmapData bmData = null;

            try
            {
                //Lock it fixed with 32bpp
                bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                int scanline = bmData.Stride;
                System.IntPtr Scan0 = bmData.Scan0;
                unsafe
                {
                    byte* p = (byte*)(void*)Scan0;
                    int nWidth = b.Width;
                    int nHeight = b.Height;
                    for (int y = 0; y < nHeight; y++)
                    {
                        for (int x = 0; x < nWidth; x++)
                        {
                            long Temp = 0;
                            Temp += p[0];
                            myHistogramBlue[Temp]++;

                            long Temp2 = 0;
                            Temp2 += p[1];
                            myHistogramGreen[Temp2]++;

                            long Temp3 = 0;
                            Temp3 += p[2];
                            myHistogramRed[Temp3]++;

                            //we do not need to use any offset, we always can increment by pixelsize when
                            //locking in 32bppArgb - mode
                            p += 4;
                        }
                    }
                }
                b.UnlockBits(bmData);
            }
            catch
            {
                try
                {
                    b.UnlockBits(bmData);
                }
                catch
                {
                }
            }

            var l = new Dictionary<string, long[]>();
            l.Add("blue", myHistogramBlue);
            l.Add("green", myHistogramGreen);
            l.Add("red", myHistogramRed);

            return l;

        }

Error 2 Cannot implicitly convert type 'System.Collections.Generic.Dictionary<string,long[]>' to 'long[]'

我现在在另一个类中使用这个函数调用,如下所示:

long[] HistogramsValuesRGB = Form1.GetHistogramRGB(original_bmp);

这不会给出任何错误,错误在于返回 l 时 Form1 中的函数本身;

4

7 回答 7

6

l是一个数组列表,但你的结构的返回类型是一个数组。

如果需要返回单个数组:

List<long> l = new List<long>();
l.AddRange(myHistogramBlue);
l.AddRange(myHistogramGreen);
l.AddRange(myHistogramRed);

return l.ToArray();

如果需要返回数组列表,请将返回类型从 更改long[]List<long[]>。虽然在这种情况下,我可能会使用 aDictionary<string,long[]>来存储每个数组的名称:

var l = new Dictionary<string,long[]>();
l.Add("blue", myHistogramBlue);
l.Add("green", myHistogramGreen);
l.Add("red", myHistogramRed);
于 2012-12-12T11:52:30.593 回答
1

您的方法应该返回一个 longs ( long[]) 数组。

您正在尝试返回 longs ( List<long[]>) 数组的列表。

您可以:

A)更改方法签名以返回List<long[]>

或者

B) 使用 . 将 long 列表展平为 long 数组SelectMany()。IE

return l.SelectMany(v => v).ToArray();

编辑

C)@Oded 的建议:)

于 2012-12-12T11:53:34.640 回答
1

您的返回类型与您的函数类型不同:

functiontype:  long[]
return type: List<long[]>

更改其中一个以匹配另一个。

于 2012-12-12T11:54:59.127 回答
1

根据您的订购需求...

for(int i = 0; i < myHistogramBlue.Length; i++)
{
l.Add(myHistogramBlue[i]);
l.Add(myHistogramGreen[i]);
l.Add(myHistogramRed[i]);
}

return l.toArray();`
于 2012-12-12T11:56:12.683 回答
0

您的返回值是long[]并且您返回的是List<long[]>

将声明更改为:

public static List<long[]> GetHistogramRGB(Bitmap b)
于 2012-12-12T11:53:41.173 回答
0

您应该返回l.ToArray();代码中的返回类型long[] , List<long[]>不兼容。

于 2012-12-12T11:54:36.207 回答
-1

您正在尝试返回

List<long[]> 

作为

long[]

替换您的代码:

List<long> l = new List<long>();
l.Add(myHistogramBlue);
l.Add(myHistogramGreen);
l.Add(myHistogramRed);

return l.ToArray();
于 2012-12-12T11:53:19.167 回答