Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
arrayG = gray(bm);
           arrayN = noise(arrayG);
           arrayH = histogram(arrayN);
           arrayB = binary(arrayH);
           showPic(arrayG);

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:
C#
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;
       }



C#
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;

       }



C#
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;
           
        }
Posted
Updated 10-Aug-11 21:18pm
v3
Comments
Syed Salman Raza Zaidi 11-Aug-11 3:05am    
Have you tried debugging it?
ready to learn 11-Aug-11 3:06am    
yes,sure

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.
 
Share this answer
 
Comments
ready to learn 11-Aug-11 3:30am    
I didn't get you well,But all the functions create the same problem for me,means that if I use arrayG or arrayB or ... as entrance for showPic, the result will be tha same and showPic shows me the binary picture.
CPallini 11-Aug-11 3:38am    
Since every function changes the array content, at the end you will have arrayG content equal to arrayN content, i.e. you will lose the original arrayG content. If this is not the desidered behaviour then each function should: (1) create a new array, clone of the input one, (2) modify the content of the freshly created array (3) return such modified array.
ready to learn 11-Aug-11 3:40am    
you are just right,but I dont know how to create clone of input array
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:

C#
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:

C#
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
 
Share this answer
 
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().
 
Share this answer
 
Comments
ready to learn 11-Aug-11 3:10am    
I'm nearly sure those things you saied doesn't occur. once I had this problem with passing bitmap through functions and I solve it by:
bm = (Bitmap)bm.Clone();
Is there a way like that which I can use now?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900