vshesh

vshesh

Intersection over union calculation

Hi,

I am trying to calculate the intersection over union for two oriented rectangles, like this picture;

I know there are lots of algorithms for this so I am curious if anyone has seen an implementation. Rectangles are stored as either four corners in counterclockwise order or {{center x, center y}, area, aspect, orientation} tuples.

Otherwise, any thoughts on how to accomplish this in elixir? I will be computing IoU for all pairwise combinations of two sets of ~100 rectangles for a soft-realtime application.

So something like:


defmodule Boxes do
  defstruct center: {0,0}, area: 0, aspect: 1.0, orientation: 0
  @type t :: %__MODULE__{center: {number(), number()}, area: number(), aspect: number(), orientation: number()}
  
  @doc """
  Computes the intersection over union score for two oriented rectangles.
  """
  def iou(box1, box2) do
    
  end
  

  def iou_batch(boxes1, boxes2) do
    Enum.map(boxes1, fn box1 -> Enum.map(boxes2, fn box2 -> iou(box1, box2) end) end)
  end

  @spec corners(__MODULE__.t()) :: list(number())
  @doc """
  Computes the corners of the box given a state vector
  """
  def corners(box) do
    [
      ... # you can assume this function works
    ]
  end
end

First Post!

mindok

mindok

I haven’t seen an Elixir implementation. I suspect you will just have to code it up.

There is a topology library (search hex for “topo” - GitHub repo is here: GitHub - pkinney/topo: A Geometry library for Elixir that calculates spatial relationships between two geometries · GitHub) that has functions to detect whether polylines intersect or overlap, but it doesn’t calculate the amount of overlap. You could take a look at that for some code examples.

I wouldn’t worry too much about performance - 100 x 99 x some basic arithmetic won’t take long at all on GHz (billions of calcs / second) CPUs.

The general approach would be:

  1. Convert all representations of rectangles into a common format
  2. Compute the polygon resulting from the intersection of the two rectangles - stack overflow has a couple of algorithms https://stackoverflow.com/questions/44797713/calculate-the-area-of-intersection-of-two-rotated-rectangles-in-python/45268241 and https://stackoverflow.com/questions/11670028/area-of-intersection-of-two-rotated-rectangles/11672022#11672022 - another library from pkinney will give you intersections of lines - GitHub - pkinney/segseg_ex: Segment-segment intersection classifier and calculator for Elixir · GitHub
  3. Compute the area of the resulting polygon. That would be the intersection. The union would be the area of the two rectangles minus the intersection.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement