wout huff
wout huff

Reputation: 11

2D Array, Index out of range C#

For school we have to make a box filter in LabVIEW. Instead of using LabVIEW I choose to use .Net in LabVIEW by creating a DLL. I have done that and the result is an IndexOutOfRange exeption.

My LabVIEW:

enter image description here

My C# class:

public class Filtor
{
    public Int16[,] OrImage { private set; get; }
    public Int16[,] NewImage { private set; get; }
    public int[,] Kernel { private set; get; }
    public int Row { private set; get; }
    public int Column { private set; get; }
    public int RowK { private set; get; }
    public int ColumnK { private set; get; }
    public int N { private set; get; }
    private int Offset;

    public Filtor()
    {
        OrImage = new Int16[0, 0];
        NewImage = OrImage;
        Kernel = new int[0, 0];
        Row = 0;
        Column = 0;
        RowK = 0;
        ColumnK = 0;

        switch (RowK * ColumnK)
        {
            case 9:
                Offset = 1;
                break;

            case 25:
                Offset = 2;
                break;

            case 49:
                Offset = 3;
                break;

            case 81:
                Offset = 4;
                break;

            default:
                Offset = 0;
                break;
        }
    }

    public Filtor(Int16[,] A, int[,] B)
    {
        OrImage = A;
        Kernel = B;
        NewImage = OrImage;
        Row = A.GetLength(0);
        Column = A.GetLength(1);
        RowK = B.GetLength(0);
        ColumnK = B.GetLength(1);

        switch (RowK * ColumnK)
        {
            case 9:
                Offset = 1;
                break;

            case 25:
                Offset = 2;
                break;

            case 49:
                Offset = 3;
                break;

            case 81:
                Offset = 4;
                break;

            default:
                Offset = 0;
                break;
        }
    }

    public void kernelFiltor(int n)
    {
        N = (n > 0) ? n : 1;

        int i, j, k, l, M = 0;

        for (i = Offset; i < (Row - Offset); i++)
        {
            for (j = Offset; j < (Column - Offset); j++)
            {
                for (k = 0; k < RowK; k++)
                {
                    for (l = 0; l < ColumnK; l++)
                    {
                        M = M + OrImage[(i + k), (j + l)] * Kernel[k, l];
                    }
                }

                if (((1 / N) * (M / (RowK * ColumnK))) <= 0)
                {
                    NewImage[i, j] = 0;
                }
                else if(((1 / N) * (M / (RowK * ColumnK))) >= 255)
                {
                    NewImage[i, j] = 255;
                }
                else
                {
                    NewImage[i, j] =Convert.ToInt16((1 / N) * (M / (RowK * ColumnK)));
                }
                M = 0;
            }
        }

    }
}

Can someone, anyone point out ware the error is?

It could be a mistake, a problem with LabVIEW to C# or the other way around.

Small update:

I have the code running in LabVIEW but its not very nice code:

enter image description here

out of the help I got we can conclude that the problem happens at:

for (k = 0; k < RowK; k++)
            {
                for (l = 0; l < ColumnK; l++)
                {
                    M = M + OrImage[(i + k), (j + l)] * Kernel[k, l];
                }
            }

when the first 2D array is at the END, the second 2D array is moving out of boundary's, what result in a IndexOutOfRange exception. how can i lock the second array B, from exiting the boundary's of the first array A.

Upvotes: 1

Views: 535

Answers (1)

StfBln
StfBln

Reputation: 1157

Using the class you have defined above and by calling it using the following code:

var A = new Int16[1, 1];
A[0, 0] = Int16.MaxValue;

var B = new Int32[2, 1];
B[0, 0] = Int32.MaxValue;
B[1, 0] = Int32.MaxValue;

var f = new Filtor(A, B);
f.kernelFiltor(123);

You will be able to see that the code breaks on the following lines in your class:

for (k = 0; k < RowK; k++)
{
    for (l = 0; l < ColomK; l++)
    {
        M = M + OrImage[(i + k), (j + l)] * Kernel[k, l];
    }
}

More precisely on the following statement:

OrImage[(i + k), (j + l)]

It appears to be occurring when the Filtor class is being instantiated using a lengthier array for B than for A.

Unfortunately I cannot help you further with this problem as I do not get a clear picture of what you are trying to accomplish. However, now you know where the issue might reside.

Upvotes: 1

Related Questions