-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add functions to compute Union & Intersection Area of two rectangles #484
Comments
Thanks a lot for the request. It seems like a good addition to kmath-geometry package. We need to implement shapse first though. If you have any understanding how shape API should look like, it would be welcome. |
And a clarification question. In object detection you probably search for intersection of rectangles alongside axis. Is it always this way or do we need also include rotations? |
@altavir That's correct, you can expect the rectangles to always be in the same orientation. You can think of it like an N x N grid where each cell might have a rectangle that might or might not be larger than the grid cell. So my use case doesn't need rotation. |
Here is the code you can use for now for that purpose: https://pl.kotl.in/5_rip0Gfi import kotlin.math.min
import kotlin.math.max
infix fun ClosedRange<Double>.intersect(other: ClosedRange<Double>): ClosedFloatingPointRange<Double>? {
val left = max(start, other.start)
val right = min(endInclusive, other.endInclusive)
return if(left<=right){
left..right
} else {
null
}
}
data class Rectangle(val x : ClosedFloatingPointRange<Double>, val y: ClosedFloatingPointRange<Double>)
val Rectangle.surface get() = (x.endInclusive - x.start)*(y.endInclusive - y.start)
infix fun Rectangle.intersect(other: Rectangle): Rectangle?{
val xIntersect = x.intersect(other.x) ?: return null
val yIntersect = x.intersect(other.x) ?: return null
return Rectangle(xIntersect, yIntersect)
}
fun main(){
val res = Rectangle(0.0..1.0, 0.0..1.0) intersect Rectangle(0.5..1.5, 0.5..1.5)
println(res?.surface)
}
We will deffinitely look further into geometry package for more complex cases. |
Feature Request
Use Case
Proposal
getIntersectionArea(Rect1, Rect2)
getUnionArea(Rect1, Rect2)
or
getIoU(Rect1, Rect2)
Ideally the solution should be mockable so that library users have more freedom.
The text was updated successfully, but these errors were encountered: