2

This is about a project where I'm tracking people walking on a floor with an infrared camera mounted on the ceiling. I get 'blobs' as rectangles with x/y/w/h information.

Now essentially I need to allocate/assign these blobs to a grid respectively to the grid's cells in percent... so depending where the blobs are and which cells they overlap, the cells should get a percentage of 0% to 100%

Are there best practices on how to approach this? How to relate the blob position/size to the cells?

Image: The darker the cell, the more the blob (red) overlaps

The darker the cell, the more the blob (red) overlaps

riccardolardi
  • 1,713
  • 5
  • 20
  • 35
  • What's wrong with computing a cell's percentage by just counting the number of blob pixels that lie in it, and dividing by the total number of blob pixels? EDIT: I see now that your "blobs" are given as rectangles, but if you get them as (x, y, w, h) then they must be axis-aligned rectangles, which the red blob in your photo isn't. Which is it? – j_random_hacker Jan 03 '17 at 16:32
  • Of course, the grid and blob are all axis aligned. How would you count the blob pixels that lie in a cell? I'm not sure in which programming language I might deveop but I've read that C++ for example has functions like intersection() and union() for rectangle shapes? – riccardolardi Jan 03 '17 at 18:26
  • If the grid and blob are axis aligned then please rotate your photo to show this, otherwise it's misleading. – j_random_hacker Jan 03 '17 at 18:39
  • 1
    Anyway, you can determine which 4 grid cells the corners of the blob land in, and then you know the complete set of cells that have some part of the blob in them. For each such cell, there are just a few cases to handle: (1) All 4 corners of the blob occur in the cell; (2) the (top/bottom/left/right) 2 corners of the blob occur in the cell; (3) the (top/bottom/left/right) edge of the blob crosses the cell; (4) the cell is completely contained in the blob. For each such affected cell, calculate the area for its particular case, and divide by the total blob area. – j_random_hacker Jan 03 '17 at 18:44
  • @j_random_hacker Could you help me with some pseudo-code please? – riccardolardi Jan 04 '17 at 16:41
  • That's more work than I'm prepared to do, sorry. You just need two nested loops scanning the affected grid cells, and to check each cell you visit for which of the cases it is. – j_random_hacker Jan 04 '17 at 16:47
  • are the blobs always going to have max dimensions (side lengths) less than the grid unit? – Sneaky Polar Bear Jan 05 '17 at 13:26
  • @SneakyPolarBear not necessarily, they can be larger and contain the cell as a whole – riccardolardi Jan 05 '17 at 14:40
  • Did you ever solve/ finish this issue? – Sneaky Polar Bear Feb 08 '17 at 20:21
  • @SneakyPolarBear I used this technique in the end: http://stackoverflow.com/questions/9324339/how-much-do-two-rectangles-overlap – riccardolardi Feb 10 '17 at 10:31
  • pop a link to that as an answer and mark it as the correct answer please, to help others searching in the future. – Sneaky Polar Bear Feb 10 '17 at 12:43
  • @SneakyPolarBear done – riccardolardi Feb 11 '17 at 12:47

2 Answers2

0

Basically loop through each pixel in your found object and bin it into your grid using the modulus of your grid spacing. There are probably faster/ more efficient ways to do what you need, but this will get the job done.

int[][] grid
int gridSpacing
int[][] objectPix

for(objectPix[][] "x")
    for(objectPix[] "y")
        gridSpacing[Math.Floor(x/gridSpacing)][Math.Floor(y/gridSpacing)]++

Then in a final step (if you want percentage) go through grid and divide its value by the total number op pixels in the object.

With more details/ effort, you could probably make a clever recursive solution which split the rectangle until it only resided in one grid (and tracked area on each split). But I will leave that solution to you if you need the efficiency.

Recursive method only using vertices (in c#):

public void splitRectangle(ref int[][] grid, int gridSpacing, rect curRect)
    {
        //if rectangle has verticies in different grid zones, split it
        if(Math.Floor(curRect.pt.x / gridSpacing) != Math.Floor((curRect.pt.x + curRect.width) / gridSpacing))
        {
            int xDiv = gridSpacing*(Math.Floor(curRect.pt.x / gridSpacing) + 1) - curRect.pt.x;
            rect split1 = new rect(curRect.pt, xDiv, curRect.height);
            rect split2 = new rect(new point(curRect.pt.x + xDiv, curRect.pt.y), curRect.width - xDiv, curRect.height);
            splitRectangle(grid, gridSpacing, split1);
            splitRectangle(grid, gridSpacing, split2);
        }
        else if (Math.Floor(curRect.pt.y / gridSpacing) != Math.Floor((curRect.pt.y + curRect.height) / gridSpacing))
        {
            int yDiv = gridSpacing*(Math.Floor(curRect.pt.y / gridSpacing) + 1) - curRect.pt.y;
            rect split1 = new rect(curRect.pt, curRect.width, yDiv);
            rect split2 = new rect(new point(curRect.pt.x, curRect.pt.y+yDiv), curRect.width, curRect.height-yDiv);
            splitRectangle(grid, gridSpacing, split1);
            splitRectangle(grid, gridSpacing, split2);
        }
        //if rectangle is fully contained within 1 grid zone, then add its area to that gridZone
        else
        {
            grid[Math.Floor(curRect.pt.x / gridSpacing)][Math.Floor(curRect.pt.y / gridSpacing)] += curRect.width * curRect.height;
        }
    }

I wrote this quickly and did not test it, but it conveys the method that I think will allow you to do what you want. Again, the last step will be going through grid and dividing all cells by original rectangle area to turn them into percent...

Sneaky Polar Bear
  • 1,611
  • 2
  • 17
  • 29
  • I found this: http://stackoverflow.com/questions/9324339/how-much-do-two-rectangles-overlap - is that similar to your proposition? – riccardolardi Jan 05 '17 at 14:41
  • Nope, that is a completely different question and answer set... Technically if you wanted, you could apply their solution by treating each cell in the grid as a candidate rectangle... but that sounds unreasonably inefficient... – Sneaky Polar Bear Jan 05 '17 at 14:45
  • ...you would need to loop through each cell and for each cell loop through all blobs and see if one of them overlaps the current iteration cell... – riccardolardi Jan 05 '17 at 14:46
0

The best way would be to calculate the overlapping area of the blob rectangle on the grid rectangle than divide that by the total area of the grid rectangle. A simple google search and reading should get you towards the right direction on implementing this or you could use some type of existing physics library if it has the method your looking for.

Joe Mccane
  • 55
  • 7