-1

I have a question regarding looping through a table of data but not storing the data in a table; instead, I would like to store the location of a certain piece of data in the table.

What I am working with now is a table of pgm pixel values which are either defined as the number 0 for black and 255 for white. What I am hoping to do with this table is then loop through it and store the location of that pixel, the row and column number, in an array, but only if that pixel displays a value of 255.

I don't have the pgm table with me at the moment so I'll provide this one for an example. To make it simple could you please help me to only log the position of the pixels that display a value of 15? All answers are greatly appreciated :)

P2
# feep.pgm
24 7
15
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0
0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0
0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2517142
  • 321
  • 1
  • 2
  • 5

1 Answers1

1

You can do something like this:

var locs = [],
    row,
    col,
    pixelData = ...,
    nRows = ...,
    nCols = ...;
for (row = 0; row < nRows; ++row) {
    for (col = 0; col < nCols; ++col) {
        if (pixelData[row][col] == 15) {
            locs.push([row, col]);
        }
    }
}
// now the locs array has all [row,col] locations of pixels with value 15

In the above, pixelData would be an array of rows, each of which is an array of pixel values:

var pixelData = [
    [0,  0,  0,  0,  0,  0,  0,  0,  0,  . . .],
    [0,  3,  3,  3,  3,  0,  0,  7,  7,  . . .],
    . . .
];

At the end, you should expect locs to be an array of two-element arrays, as if it were initialized with:

var locs = [
    [1, 19], [1,20], [1,21], [1,22], [2, 19], [2,22], ...
];
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thanks. This seems great. But for the variable pixelData, what would you declare its value as? – user2517142 Jul 15 '13 at 16:49
  • @user2517142 - That should just be the pgm pixel data; the code I posted assumes that it is an array of arrays of integer values. I assumed that you already had under control the reading of pgm data and that you were asking just about collecting indexes. If not, what form of data are you starting with? – Ted Hopp Jul 15 '13 at 16:56
  • We are using a pgm file which is a true black and white picture of circle. Once we are able to get the pixel values into a table, we Were going to use this code to then find the "coordinates" of the pixels within the picture. And lastly, run those coordinates through some math equations to determine the center and the radius of the circle in the image. – user2517142 Jul 15 '13 at 17:09
  • @user2517142 - In my code, `pixelData` would be the table of pixel values. – Ted Hopp Jul 15 '13 at 17:20
  • I can't thank you enough. The edit cleared that up for me. Hope you have a good day. – user2517142 Jul 15 '13 at 17:40