通过数组的低谷函数 [英] passing arrays trough functions

查看:93
本文介绍了通过数组的低谷函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在我的项目中使用了几个函数,每个函数都得到一个数组(不是Ref),并像下面的代码一样返回:

Hello,I''m using several functions in my project,each of them gets an array(not as Ref) and returns it back like the folowing code:

arrayG = gray(bm);
           arrayN = noise(arrayG);
           arrayH = histogram(arrayN);
           arrayB = binary(arrayH);
           showPic(arrayG);


第一个函数获取一个位图并返回一个数组,该数组将在下一个函数中使用,最后一个函数使用中间数组生成一个位图.问题是我完全使用arrayG作为showPic的输入,但是showPic使用arrayB作为它的输入的入口,请帮助我.在此先感谢
这是更多说明:


the first function gets a bitmap and returns an array which is used in the next function,and last function makes a bitmap using intering array.The problem is that altough I''m using arrayG as enterance for showPic, but showPic uses arrayB as it''s entrance,please help me .thanks in advance
here is more explanation:

private int[,] gray(Bitmap bm)
       {
           stG.Start();
           int[,] array = new int[bm.Height, bm.Width];
           bm = (Bitmap)bm.Clone();

           BitmapData bmdata = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
           IntPtr scan0 = bmdata.Scan0;
           int stride = bmdata.Stride;
           unsafe
           {
               byte* p = (byte*)(void*)scan0;
               int offset = stride - (bm.Width * 3);
               for (int i = 0; i < bm.Height; i++)
               {
                   for (int j = 0; j < bm.Width; j++)
                   {
                       array[i,j] = Convert.ToInt32((p[0] + p[1] + p[2]) / 3);



                       p += 3;
                   }
                   p += offset;
               }
           }
           bm.UnlockBits(bmdata);
           stG.Stop();
           return array;
       }







private int[,] binary(int[,] arrayEnter)
       {
           stB.Start();
           const int t = 16;
           for (int i = 0; i < picP.Height; i++)
           {
               for (int j = 0; j < picP.Width; j++)
               {
                   if (arrayEnter[i, j] < t)
                       arrayEnter[i, j] = 0;
                   else
                       arrayEnter[i, j] = 255;
               }
           }
           stB.Stop();
           return arrayEnter;

       }







private void showPic(int[,] arrayEnter)
        {
            stS.Start();
            Bitmap bm = new Bitmap(picP.Width, picP.Height);

            int ma = -1000;
            int mi = 1000;
            for (int i = 0; i < bm.Height; i++)
            {
                for (int j = 0; j < bm.Width; j++)
                {
                    ma = Math.Max(ma, arrayEnter[i, j]);
                    mi = Math.Min(mi, arrayEnter[i, j]);
                }
            }
            int minus = ma - mi;
            BitmapData bmdata = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            IntPtr scan0 = bmdata.Scan0;
            int stride = bmdata.Stride;
            unsafe
            {
                byte* p = (byte*)(void*)scan0;
                int offset = stride - (bm.Width * 3);
                for (int i = 0; i < bm.Height; i++)
                {
                    for (int j = 0; j < bm.Width; j++)
                    {
                        arrayEnter[i, j] = ((arrayEnter[i, j] - mi) / minus) * 255;
                        p[0] = p[1] = p[2] = Convert.ToByte(arrayEnter[i, j]);


                        p += 3;
                    }
                    p += offset;
                }
            }
            bm.UnlockBits(bmdata);
            stS.Stop();
            picP.Image = (Bitmap)bm;
           
        }

推荐答案

您是否认识到binary函数会更改 input 数组内容?
我猜想noisehistogram函数的作用相同.这样,arrayB arrayG,最后.
Do you realize that binary function changes the input array content?
I guess that noise and histogram functions do the same. This way arrayB is arrayG, at the end.




好吧,在不深入的情况下,int是一个简单的值.这很容易复制,这就是为什么我们可以说int a = b;但是,int []数组在技术上是一个复杂的结构,不能直接克隆. Microsoft不允许自动执行此操作的原因仅仅是时间和空间.实际上,您必须复制数组中的每个值,如果要说话的图像或"多维数组",即int [640,320]即要复制204''800整数,则需要花费时间并且占用两倍的空间.空间量.

Microsoft通过自动将引用传递给阵列来防止出现这种情况.它旨在防止新程序员通过消耗所有资源来杀死系统.

据我所知,Microsoft不允许自动复制多维数组.因此,您必须自己复制每个元素,这里是代码:

Hi,

Well without going into to much depth, an int is a simple value. It is easy to copy and is why we can say int a = b; However and int[] array is a technically a complex structure which is not cloneable directly. The reason Microsoft don''t allow this automatically is simply down to time and space. You have to in effect copy each value in the array and if where talking images or a "Multidimensional Array" i.e. int[640,320] thats 204''800 integers to copy which takes time and takes up twice the amount of space.

Microsoft prevent this over-site by automatically passing a reference to the array. It is designed to prevent new programmers from killing the system by eating all the resources.

As far as I am aware Microsoft do not allow multidimensional arrays to be copied automatically. So you have to copy each element yourself here is the code:

private int[,] copy_array (int[,] Input_array, int array_width, int array_height)
{
    int[,] array_copy = new int[array_width, array_height];

    for (int i = 0; i < array_width; i++)
    {
        for (int j = 0; j < array_height; j++)
        {
            array_copy[i, j] = Input_array[i, j];
        }
    }
    
    return array_copy;
}


因此您的代码将如下所示:


So your code will look like this:

arrayG = gray(bm);

arrayN = noise(copy_array(arrayG,bm.Width, bm.Height));
arrayH = histogram(copy_array(arrayN,bm.Width, bm.Height));
arrayB = binary(copy_array(arrayH,bm.Width, bm.Height));

showPic(arrayG);




保重
克里斯




Take Care
Chris


您在此处发布的代码不足以回答您的问题.

两件事-
确保没有数组引用彼此链接.
确保showPic()剂量您想要的并且不是gray()的副本.
The code you have posted here is not enough to answer your question.

Two things -
Make sure no array references are linked to each other.
Make sure showPic() doses what you want it to do and is not a copy of gray().


这篇关于通过数组的低谷函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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