簡體   English   中英

獲取索引序列

[英]Getting a sequence of indexes

我有點問題。 任務是拍攝照片,通過EdgeDetector運行它,並使用3軸matrixprinter繪制邊緣。

我大部分都被覆蓋了,但仍然存在一個主要障礙。 當我通過Sobel運行圖片並使用二進制截斷時,它將返回一個0和255的二維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坐標中的差異可以轉換為步進電機中的多個步驟。

我只是不知道該怎么做。

逐行讀取並檢查值何時更改,您可以輕松地對其進行管理。

這是一個例子:

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; 

試用以下代碼:

// 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM