Skip to content
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

Support convert from UpperLeft, Center in BoundingBoxUtils #40953

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ private static RectF convertOneBoundingBox(
case BOUNDARIES:
return convertFromBoundaries(values, coordinateType, height, width);
case UPPER_LEFT:
return convertFromUpperLeft(values, coordinateType, height, width);
case CENTER:
// TODO(b/150824448): convertFrom{UpperLeft, Center}
throw new IllegalArgumentException("BoundingBox.Type " + type + " is not yet supported.");
return convertFromCenter(values, coordinateType, height, width);
}
throw new IllegalArgumentException("Cannot recognize BoundingBox.Type " + type);
}
Expand All @@ -197,6 +197,42 @@ private static RectF convertFromBoundaries(
}
}

private static RectF convertFromUpperLeft(
float[] values, CoordinateType coordinateType, int height, int width) {
if (coordinateType == CoordinateType.PIXEL) {
float left = values[0];
float top = values[1];
float w = values[2];
float h = values[3];

return new RectF(
left, top, left+w, top+h);
} else {
return new RectF(
values[0] * width, values[1] * height, (float) width, (float) height);
}
}

private static RectF convertFromCenter(
float[] values, CoordinateType coordinateType, int height, int width) {
if (coordinateType == CoordinateType.PIXEL) {
float centerX = values[0];
float centerY = values[1];
float w = values[2];
float h = values[3];

float left = centerX - w/2;
float top = centerY - h/2;
float right = centerX + w/2;
float bottom = centerX + h/2;

return new RectF(
left, top, right, bottom);
} else {
throw new IllegalArgumentException("BoundingBox.Type Center is not supported with CoordinateType CoordinateType.RATIO");
}
}

// Private constructor to prevent initialization.
private BoundingBoxUtil() {}
}