0

i am writing a program that will read the element in a convolute and the program will then multiple each element in the convolute with the kernel given. I will need to take the convolute array from other class as well as kernel.

But i am getting this Error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6

The error will be specified in the comment section with capitalised sentence, which is in my second for loop

The classes (kernel and the convolute) will be provided down after my code.

Thank You!

public static void main(String[] args) 
{
    Kernel kernel = new Kernel();
    Convolute convolute = new Convolute();

    int[][] matrixA = convolute.MATRIX_A;
    int[][] kernelVertical = kernel.VERTICAL;
    int[][] resultArray = new int[4][4];

    int tempMatrix = 0, sumMatrix = 0;  // declare temporary matrix and sumMatrix to take all the sum of each product array
    int matrixRowA = 0, matrixColumnA = 0;  // declare variable for matrix row and column
    int convoluteRowControl = 2, convoluteColumnControl = 2;  // this variable is to control moving of the kernel through the convolute

    while(convoluteRowControl < matrixA.length)  // while loop stops when convoluteRowControl points to 6
    {
        for(int kernelRow = 0; kernelRow < kernelVertical.length; kernelRow++)  // this loop will stop when kernelRow is 3
        {
            for(int kernelColumn = 0; kernelColumn < kernelVertical[kernelRow].length; kernelColumn++)  // this loop will stop when kernelColumn is 3
            {
                tempMatrix = matrixA[matrixRowA][matrixColumnA] * kernelVertical[kernelRow][kernelColumn];  // ERROR IS HERE!!
                sumMatrix += tempMatrix;  // sum all of the matrix calculated
                matrixColumnA++;  // re-initialize matrixColumnA to move to the next column element in matrixA
            }

            matrixRowA++;  // re-initialize matrixRowA to move to the next row element in matrixA
            matrixColumnA = convoluteColumnControl - 2; // re-initialize matrixColumnA back to the original specified range
        }

        for(int row = 0; row < resultArray.length; row++)  // this loop is used to store sumMatrix calculated in the above loop to each element of the array
        {                                                  // loop stops at 5
            for(int column = 0; column < resultArray[row].length; column++)  
            {
                resultArray[row][column] = sumMatrix;  // store sumMatrix into each element of the array 
            }
        }

        ++convoluteColumnControl;  // re-initialize convoluteColumnControl so that the kernel can move to the next column

        if(matrixColumnA < 5)  // if kernel get to the last column of its size, reset the row and move to the next column again 
        {                      // this will stop when kernel get to the final column of the convolute
            matrixRowA = convoluteRowControl - 2;  // reset row back to the the top kernel
            matrixColumnA = convoluteColumnControl - 2;  // set new starting limit for the column in the kernel
        }

        if(matrixColumnA == 5)  // when matrixColumnA is 5 (which means the final column of the convolute) this IF is executed
        {                       // to set the new row for the kernel
            ++convoluteRowControl;  // convolteRowControl increase by 1 to set a new row limit for the kernel in convolution 
            matrixRowA = convoluteRowControl - 2;  // reset matrixRowA equivalent to the starting of the new kernel
        }
    }

}


public class Convolute
{
    /*
     * ARRAY_A contains a 6x6 matrix
     */
    public static final int[][] MATRIX_A =
    {
            {10, 10, 10,  0,  0,  0},
            {10, 10, 10,  0,  0,  0},
            {10, 10, 10,  0,  0,  0},
            {10, 10, 10,  0,  0,  0},
            {10, 10, 10,  0,  0,  0},
            {10, 10, 10,  0,  0,  0}
    }; 
}

public class Kernel
{
    /*
     * HORIZONTAL - A kernel that detects horizontal lines.
     */
    public static final int[][] HORIZONTAL =
    {
            { 1,  1,  1},
            { 0,  0,  0},
            {-1, -1, -1}
    }; 
    /*
     * VERTICAL - A kernel that detects vertical lines.
     */
public static final int[][] VERTICAL =
    {
            { 1,  0, -1},
            { 1,  0, -1},
            { 1,  0, -1}
    }; 

}

0 Answers0