How to find border of object represent in matrix ( coordinate system)

algorithm, c++

Solution

Set a pixel to 0 if all of it's neighbor pixels are 1. The remaining 1s are on a boundary:

for (x = 1; x < width-1; ++x) {
  for (y = 1; y < height-1; ++y) {
    if (data(x+1,y) && data(x-1,y) && data(x,y+1) && data(x,y-1)) {
      data(x,y) = 0;
    }
  }
}

Problem

I have in matrix the footprint of an object, which is in short which cells the object does occupy (object are of one part, cells marked as 0 are unoccupied and cells marked as 1 are occupied by the object). Then, I have a list of occupied cells in `list<pair<int, int> >` with matrix coordinates. How to find all border cells of that object which belongs to that object ? (adjacent cells are for example `(x1, y1)` and `(x2, y2) if abs(x1-x2)<=1 && abs(y1-y2)<=1`). ``` 0 0 0 0 0 0 0 1 1 0 0 1 1 1 0 0 1 1 1 0 0 0 1 0 0 ```

Original source