1
votes

EDIT: fixed issues in the code, but new, novel error has occurred. Posting to new thread for insight.

I'm having some issues with a homework assignment that needs me to print out a filtered copy of a 2d array. I need to create a nested for-loop that calls another method(which stores the neighbors of an index in a 1D array), finds the average of the 1D array, and then prints out a 2D array with the averages.

I was able to code the method "getNeighbors" to obtain the neighbors of an index, but when I call the method into the main, I get 6 errors:

Lab5.java:45: error: cannot find symbol getNeighbors(row, col, imageData); ^ symbol: variable imageData location: class Lab5

Lab5.java:47: error: cannot find symbol for(int i=0; i<nebs.length; i++){ ^ symbol: variable nebs location: class Lab5

Lab5.java:48: error: cannot find symbol sum += nebs[i]; ^ symbol: variable nebs location: class Lab5

Lab5.java:50: error: cannot find symbol int average = sum / nebs[i].length; ^ symbol: variable nebs location: class Lab5

Lab5.java:50: error: cannot find symbol int average = sum / nebs[i].length; ^ symbol: variable i location: class Lab5

Lab5.java:65: error: incompatible types: int[][] cannot be converted to int[] } return copyImage;

I don't know if it's mental fatigue getting to me, but I can't figure out how to properly call the method to provide the 1D neighbors array and then get the average without using variables from the getNeighbors method.

Here is my code:


public static int[][] applyFilter1(int[][] imageData){
        int[][] filtered = new int[imageData.length][imageData[0].length];

        for (int row=0; row<filtered.length;row++){
            for (int col=0;col<filtered[row].length;col++){
                getNeighbors(row, col, imageData);
                int sum = 0;
                for(int i=0; i<nebs.length; i++){
                    sum += nebs[i];
                 }
                 int average = sum / nebs[i].length;
                 filtered[row][col] = average;
                }
            }
            return filtered;
        }

        public static int[] getNeighbors(int row, int col, int[][] imageData){
            //find neighbors of current index
            int [][] copyImage = new int[imageData.length][imageData[0].length];
                try{
                    for(int r=0; r<imageData.length; r++){
                        for(int c=0; c<imageData[r].length; c++){
                            imageData[r][c] = copyImage[r][c];
                        }
                    } return copyImage;
                } catch(Exception e){
                    System.out.println("Array copy not successful");
                }

                    //handles the top row of the array
                    if(row==0){//if copyImage[0].length
                        if(col==0){//handles upper left corner of array
                            int[] nebs = {copyImage[row][col], copyImage[row+1][col],
                                                        copyImage[row][col+1]};
                            return nebs;
                        }
                        else if(col==copyImage[row].length-1){//handles upper right corner of array
                            int[] nebs = {copyImage[row][col], copyImage[row][col-1],
                                                        copyImage[row+1][col]};
                            return nebs;
                        }
                        else{//handles top row of array between corners
                            int[] nebs = {copyImage[row][col], copyImage[row][col-1],
                                                        copyImage[row+1][col], copyImage[row][col+1]};
                            return nebs;
                        }
                    }
                    //handles the bottom row of the array
                    else if(row==copyImage.length-1){//if the row is at max value
                        if(col==0){//handles botton left corner of array
                            int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                        copyImage[row][col+1]};
                            return nebs;
                        }
                        else if(col==copyImage[row].length-1){//handles bottom right corner of array
                            int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                        copyImage[row][col-1]};
                            return nebs;
                        }
                        else{//handles bottom row of array
                            int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                        copyImage[row][col-1], copyImage[row][col+1]};
                            return nebs;
                        }
                    }
                    //handles leftmost column of array
                    else if(col==0){//if col=0 and row increases
                        int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                    copyImage[row+1][col], copyImage[row][col+1]};
                        return nebs;
                    }
                    //handles rightmost column of array
                    else if(col==copyImage[row].length-1){//if col=max value and row increases
                        int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                    copyImage[row+1][col], copyImage[row][col-1]};
                        return nebs;
                    }
                    //handles values in the body of the array
                    else{
                        int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                    copyImage[row+1][col], copyImage[row][col-1],
                                                    copyImage[row][col+1]};
                        return nebs;
                    }
                }

What can I do to get this to compile and execute correctly?

1

1 Answers

0
votes

Perhaps you meant to write image (the name of the int[][] parameter to applyFilter1) in your getNeighbors call, rather than imageData? And probably you also meant to save the result from getNeighbors in an int[] called nebs?

    int[][] nebs = getNeighbors(row, col, image);