Barrie Reader
Barrie Reader

Reputation: 10713

Selecting the "lowest" items in a 2d array

I have an array - it's populated with 1s and 0s.

When "rendered" out - it looks like this:
enter image description here

Basically, I would like to select the lower sections (highlighted in red). Is there anyway to select just the lowest 1s?

Thanks again!

[edit]
I should mention that the lowest points are random each time!

[edit2]
Currently I'm just selecting everything below a certain area and seeing if it's a 1 and doing what I want... Is there no better way?

Upvotes: 1

Views: 78

Answers (2)

GameAlchemist
GameAlchemist

Reputation: 19294

Another way :

1) compute per row density = number of black pixel per row
put this data inside a new 1D array.

2) decide where you consider it is leg or not (with a treshold possibly, or a relative threshold (ex: less than 30% mean value of non-null rows ...) ).

3) push all (x,y) values in the 'leg' rows.

This will avoid lots of small points 'eating' the pixel threshold before you come to the body of the monster.

Upvotes: 0

Louis Ricci
Louis Ricci

Reputation: 21086

You loop through the 2d array in reverse...

var lowest = [];
var threshold = 6; // find the 6 "lowest" 1's
for(var row=arr.length-1; row>=0; row--)
   for(var col=arr[row].length-1; col>=0; col--)
      if(arr[row][col] == 1 && threshold > 0) {
          threshold--;
          lowest.push({x: col, y: row});
      } 

Upvotes: 1

Related Questions