【问题标题】:Getting a sequence of indexes获取索引序列
【发布时间】:2018-11-15 07:57:31
【问题描述】:

我有点问题。任务是拍照,通过 EdgeDetector 运行它,然后得到一个 3 轴矩阵打印机来绘制边缘。

我已经解决了大部分问题,但仍然存在一个主要障碍。当我通过我的 Sobel 运行我的图片并使用二进制截断时,它返回一个 0 和 255 的 2D int 数组。只是一个测试数组的示例,以便更好地理解。

255 255 255 255 255 255 255 255
255  0   0   0  255  0   0  255
255 255  0   0   0  255 255 255
255  0   0  255 255  0   0  255
255 255 255  0  255  0   0  255
255  0  255  0   0   0   0  255
255 255 255 255 255 255 255 255

然后我使用嵌套的 for 循环遍历数组,并为数组中的每个位置提供i 坐标、j 坐标和大小,如下所示。我正在使用 StringBuilder,因为我需要向 PLC 发送一个字符串。

 public static void stringBuilder(int m[][])
   { // start method stringbuilder
       StringBuilder sb = new StringBuilder();
                
       for (int i = 0 ; i < m.length ; i++)
       { // start i for loop
           for ( int j = 0 ; j < m[i].length ; j++)
           { // start j for loop
               int magnitude = m[j][i];
              
               /*
               sb.append("(");
               sb.append(i);
               sb.append(",");
               sb.append(j);
               sb.append(")");
               sb.append(" : ");
               */
               
               if (magnitude == 0)
               {
                   sb.append(magnitude);
                   sb.append("   ");
               }
               else
               {
                 sb.append(magnitude); 
                 sb.append(" ");  
               }
                sb.append(" ");
               
               
               
           } // end j for loop
                sb.append("\n");
           
       } // end i for loop
   
       System.out.println(sb);
   } // end method  stringbuilder

我还想出了如何像这样打印连接元素的元素。这是按行,但有按列,左右对角线的方法。再次将其放入 StringBuilder。

 for (int i = 0; i < n.length ; i++) 
{
  for (int j = 0; j < n[0].length ; j++) 
  {
    int d = j + 1;
    int e = j - 1;
      
    if (d < n[0].length)  
    {
        if ((n[i][j] == 0 && n[i][d] == 0) || (n[i][j] == 0 && n[i][e] == 0))
        { 
            
            coordinates.append("(" + i + "," + j + ")\t");
            //count ++;
        }
    }
    
  }

现在问题来了。我非常希望能够向 PLC 发送一个字符串,该字符串仅包含它应该绘制的坐标。在示例数组中,我将是:

0,0 到 0,7 向上

1,0 向上; 1,1 到 1,3 下降; 1,4 向上; 1,5 到 1,6 下降; 1,7 向上

2,0 到 2,1 向上; 2,2 到 2,4 下降; 2,5 到 2,7 向上

3,0 向上 ; 3,1 到 3,2 下降; 3,3 到 3,4 向上; 3,5 到 3,6 下降; 3,7 向上

等等等等。

本质上是在数组中间获取一个元素序列。

这背后的想法是j 坐标的差异可以转化为步进电机中的多个步数。

我只是不知道该怎么做。

【问题讨论】:

    标签: java arrays multidimensional-array


    【解决方案1】:

    逐行读取,查看值何时发生变化,轻松管理。

    这是一个例子:

    public static void main(String[] args) {
        int[][] array = {
                { 255, 255, 255, 255, 255, 255, 255, 255 },
                { 255, 0, 0, 0, 255, 0, 0, 255 },
                { 255, 255, 0, 0, 0, 255, 255, 255 },
                { 255, 0, 0, 255, 255, 0, 0, 255 },
                { 255, 255, 255, 0, 255, 0, 0, 255 },
                { 255, 0, 255, 0, 0, 0, 0, 255 },
                { 255, 255, 255, 255, 255, 255, 255, 255 }
                };
        StringBuilder result = new StringBuilder();
        int currentValue;
        int startIndex;
        for (int i = 0; i < array.length; i++) {
            if (array[i].length == 0) {
                continue;
            }
            currentValue = array[i][0];
            startIndex = 0;
            for (int j = 1; j < array[i].length; j++) {
                if (currentValue != array[i][j]) {
                    updateResult(result, currentValue, i, startIndex, j - 1);
                    currentValue = array[i][j];
                    startIndex = j;
                }
            }
            updateResult(result, currentValue, i, startIndex, array[i].length - 1);
            result.append("\n");
        }
        System.out.println(result.toString());
    }
    
    private static void updateResult(StringBuilder result, int value, int row, int startIndex, int endIndex) {
        if (startIndex == endIndex) {
            addCoordinates(result, row, startIndex);
        } else {
            addCoordinates(result, row, startIndex);
            result.append(" to ");
            addCoordinates(result, row, endIndex);
        }
        result.append(" ");
        switch(value) {
        case 255:
            result.append("up");
            break;
        case 0:
            result.append("down");
            break;
            default:
                System.err.println("Unrecognized value " + value);
        }
        result.append("; ");
    }
    
    private static void addCoordinates(StringBuilder result, int row, int column) {
        result.append(row);
        result.append(",");
        result.append(column);
    }
    

    对于您的输入数组,结果将是:

    0,0 to 0,7 up; 
    1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up; 
    2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up; 
    3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up; 
    4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up; 
    5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up; 
    6,0 to 6,7 up; 
    

    【讨论】:

    • 太棒了。感谢您的回答。会试一试的。
    【解决方案2】:

    试试这个代码:

    // String formatting
    public static final String FORMAT_TO_UP = "%d,%d to %d,%d up";
    public static final String FORMAT_TO_DOWN = "%d,%d to %d,%d down";
    
    public static final String FORMAT_UP = "%d,%d up";
    public static final String FORMAT_DOWN = "%d,%d down";
    
    public static void stringBuilder(int m[][])
    { // start method stringbuilder
        StringBuilder sb = new StringBuilder();
    
        for(int i = 0; i < m.length; i++)
        { // start i for loop
    
            int upStart = 0;
            int downStart = 0;
    
            boolean hasPreviousUp = false;
            boolean hasPreviousDown = false;
    
            final int rowSize = m[i].length;
            for(int j = 0; j < rowSize; j++)
            { // start j for loop
                int magnitude = m[i][j];
    
                // Reached a down
                if(magnitude == 0)
                {
                    // Handle the change from up to down
                    if(hasPreviousUp)
                    {
                        // If range is only 1
                        if(upStart == j - 1)
                        {
                            sb.append(String.format(FORMAT_UP, i, j - 1));
                        }
                        else
                        {
                            sb.append(String.format(FORMAT_TO_UP, i, upStart, i, j - 1));
                        }
                        hasPreviousUp = false;
                        sb.append("; ");
                    }
    
                    // Don't reset the start value if there was no change of up/down
                    if(!hasPreviousDown)
                    {
                        hasPreviousDown = true;
                        downStart = j;
                    }
                }
                // Reached an up
                else
                {
                    // Handle the change from down to up
                    if(hasPreviousDown)
                    {
                        // If range is only 1
                        if(downStart == j - 1)
                        {
                            sb.append(String.format(FORMAT_DOWN, i, j - 1));
                        }
                        else
                        {
                            sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, j - 1));
                        }
                        hasPreviousDown = false;
                        sb.append("; ");
                    }
    
                    // Don't reset the start value if there was no change of up/down
                    if(!hasPreviousUp)
                    {
                        hasPreviousUp = true;
                        upStart = j;
                    }
                }
    
            } // end j for loop
    
            // Handle the last up/down range
            if(hasPreviousUp)
            {
                if(upStart == rowSize - 1)
                {
                    sb.append(String.format(FORMAT_UP, i, rowSize - 1));
                }
                else
                {
                    sb.append(String.format(FORMAT_TO_UP, i, upStart, i, rowSize - 1));
                }
            }
            else if(hasPreviousDown)
            {
                if(downStart == rowSize - 1)
                {
                    sb.append(String.format(FORMAT_DOWN, i, rowSize - 1));
                }
                else
                {
                    sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, rowSize - 1));
                }
            }
            sb.append("\n");
    
        } // end i for loop
    
        System.out.println(sb);
    } // end method stringbuilder
    

    结果:

    0,0 to 0,7 up
    1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up
    2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up
    3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up
    4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up
    5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up
    6,0 to 6,7 up
    

    【讨论】:

      猜你喜欢
      • 2018-06-07
      • 2020-06-19
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多