Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to fill arraylist in c# iteratively.actually I am doing small program in image processing (to remove noise from image) in which I have mask of size 3*3 that mean for each pixel on image it will collect 9 values. for example consider we have image of size 10*10 so total no. of pixels would be 100, now for each pixel it will collect its 9 neighbor value and fill it in array list. code is

C#
for (int ii = 0; ii < img.Width; ii++)
                {
                    for (int jj = 0; jj < img.Height; jj++)
                    {

                        if (ii - 1 >= 0 && jj - 1 >= 0)
                        {
                            c = img.GetPixel(ii - 1, jj - 1);
                            mask[0] = Convert.ToInt16(c.R);
                        }
                        else
                        {
                            mask[0] = 0;
                        }

                        if (jj - 1 >= 0 && ii + 1 < img.Width)
                        {
                            c = img.GetPixel(ii + 1, jj - 1);
                            mask[1] = Convert.ToInt16(c.R);
                        }
                        else
                            mask[1] = 0;

                        if (jj - 1 >= 0)
                        {
                            c = img.GetPixel(ii, jj - 1);
                            mask[2] = Convert.ToInt16(c.R);
                        }
                        else
                            mask[2] = 0;

                        if (ii + 1 < img.Width)
                        {
                            c = img.GetPixel(ii + 1, jj);
                            mask[3] = Convert.ToInt16(c.R);
                        }
                        else
                            mask[3] = 0;

                        if (ii - 1 >= 0)
                        {
                            c = img.GetPixel(ii - 1, jj);
                            mask[4] = Convert.ToInt16(c.R);
                        }
                        else
                            mask[4] = 0;

                        if (ii - 1 >= 0 && jj + 1 < img.Height)
                        {
                            c = img.GetPixel(ii - 1, jj + 1);
                            mask[5] = Convert.ToInt16(c.R);
                        }
                        else
                            mask[5] = 0;

                        if (jj + 1 < img.Height)
                        {
                            c = img.GetPixel(ii, jj + 1);
                            mask[6] = Convert.ToInt16(c.R);
                        }
                        else
                            mask[6] = 0;


                        if (ii + 1 < img.Width && jj + 1 < img.Height)
                        {
                            c = img.GetPixel(ii + 1, jj + 1);
                            mask[7] = Convert.ToInt16(c.R);
                        }
                        else
                            mask[7] = 0;
                        c = img.GetPixel(ii, jj);
                        mask[8] = Convert.ToInt16(c.R);
                                                                                          
                                               
                       
             }

                    double[][] collection_input ={
                                                  
                                        new double[9]{mask[0],mask[1],mask[2],mask[3],mask[4],mask[5],mask[6],mask[7],mask[8]}     
                                               
                                             };
here I want collection_ input contain 9 value for each pixel in this case it would be 9 value for each pixel total would be 900. plz help me if u know this

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 5-Jan-13 21:43pm
v3
Comments
Zoltán Zörgő 6-Jan-13 3:27am    
Woooow, that will be really slow :(

1 solution

If I understand you correctly,
you can use a Generic List (see this e.g.), where each object in the generic list can be a 9-elements array or a two dimensional array 3X3.

As you go along the iteration fill the Generic List with objects that represent the 9 values you need.

[See this Code Project article for a simple generic example to get the hang of things]

Simple as that...

Lemme know if you need clarification, or if you think I didn't understand your question.
 
Share this answer
 
v2

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