简体   繁体   English

在二维数组中寻找邻居(不包括对角线)

[英]Finding Neighbors in 2d Array (Not Including diagonal)

In another post I found this way to find the neighbors of the current pixel in a 2d array.在另一篇文章中,我发现这种方法可以在二维数组中找到当前像素的邻居。 The solution considered diagonal pixels to be neighbors.该解决方案将对角线像素视为邻居。 I need a solution that gives only directly N, S, E, W. How would I be able to implement that into this code?我需要一个仅直接给出 N、S、E、W 的解决方案。我如何才能将其实现到此代码中? The post I mentioned is found here: Finding valid neighbors in 2D array Any input/help is appreciated.我提到的帖子在这里找到: 在二维数组中查找有效邻居感谢任何输入/帮助。

for(int x=0; x<matrix.length; x++) {
            for(int y=0; y<matrix[0].length; y++) { //Iterate through length of column (y)
                if(matrix[x][y] != 0) { 

                    ArrayList<Integer> neighbors = new ArrayList<Integer>(); //Connected elements with the current element's value
                    //Checks pixels N,S,E,W of the current pixel
                    for(int nx=-1; nx<=1; nx++) { 
                        for(int ny=-1; ny<=1; ny++) {

                            if(x+nx<0 || y+ny<0 || x+nx>labels.length-1 || y+ny>labels[0].length-1) { 
                                continue;
                            }
                            else {
                                if (x + nx == 0 && x + ny == 0) {
                                  continue;
                                }
                                if (labels[x + nx][y + ny] != 0) {
                                    neighbors.add(labels[x + nx][y + ny]);
                                }
                            }
                        }
                    }

I think the following code will be enough for your use case.我认为以下代码足以满足您的用例。

        int n = matrix.length;
        int m = matrix[0].length;
        ArrayList[][] neighbors = new ArrayList[n][m];

        for(int x=0; x<matrix.length; x++) {
            for(int y=0; y<matrix[0].length; y++) { //Iterate through length of column (y)
                neighbors[x][y]= new ArrayList<Integer>();
                if(matrix[x][y] != 0) {

                    //Checks pixels N,S,E,W of the current pixel
                    if (x-1 > 0 && labels[x-1][y]!=0)
                    {
                        neighbors[x][y].add(labels[x-1][y]);
                    }
                    if (x+1 < labels.length && labels[x+1][y]!=0)
                    {

                        //add neighbours
                        neighbors[x][y].add(labels[x-1][y]);
                    }
                    if (y-1 > 0 && labels[x][y-1]!=0 )
                    {
                        //
                        neighbors[x][y].add(labels[x-1][y]);
                    }
                    if (y+1 < labels[0].length && labels[x][y+1]!=0){
                        neighbors[x][y].add(labels[x-1][y]);
                    }

                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM