简体   繁体   中英

Getting a sequence of indexes

I'm having a bit of a problem. The assignment is to take a picture, run it through an EdgeDetector, and get a 3 axis matrixprinter to draw the edge.

Most of it I got covered, but one major obstacle remains. When I run my picture through my Sobel and use a binary truncate it returns a 2D int array of 0's and 255's. Just an example of a test array for better understanding.

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

I'm then using a nested for loop to go through the array and give me an i coordinate, j coordinate and magnitude for each position in the array, like this. I'm using the StringBuilder, because I need to send a String to the 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

I have also figured out how to print the elements of connected elements like this. This is by row, but have methods by column, diagonaly left and right. Again putting it in a 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 ++;
        }
    }

  }

Now here's the problem. I would very much like to be able to send a string to the PLC containing only the coordinates where it is suppose to draw. In the example array I would be:

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

and so on and so forth.

In essence getting a sequense of element in the middle of the array.

The idea behind this is that the differens in j coordinates can be translated to a number of steps in a steppermotor.

I just can't figure out how to do it.

Reading line by line, and checking when the value changes, you can easily manage it.

Here's an example:

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);
}

For your input array, the result will be:

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; 

Try out this code:

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

Result:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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