get max rectangle area from numpy array

  • Last Update :
  • Techknowledgy :

You can calculate the area of the rectangles in bulk with:

areas = (predictions[: , 4] - predictions[: , 2]) * (predictions[: , 5] - predictions[: , 3])

Next you can obtain the index (row) with the largest area with:

np.argmax(areas)

For your given sample data, the first rectangle is the largest:

>>> predictions = np.array([
      [19., 0.78, 102.79, 98.85, 258.17, 282.53],
      ...[7., 0.66, 245.61, 211.98, 270.66, 234.76],
      ...[6., 0.56, -6.51, 143.64, 39.31, 286.06],
      ...[6., 0.5, 103.77, 94.07, 256.6, 278.14]
   ]) >>>
   areas = (predictions[: , 4] - predictions[: , 2]) * (predictions[: , 5] - predictions[: , 3]) >>>
   areas
array([28540.1984, 570.639, 6525.6844, 28131.4181]) >>>
   np.argmax(areas)
0

Suggestion : 2

Last Updated : 05 Aug, 2022

Examples: 

Input: arr[] = {
   2,
   1,
   2,
   5,
   4,
   4
}
Output: 8
Explanation: Dimension will be 4 * 2

Input: arr[] = {
   2,
   1,
   3,
   5,
   4,
   4
}
Output: 0
Explanation: No rectangle possible

24

24

Suggestion : 3

I want to get max rectangle area from numpy anycodings_python array that looks like that:,You can calculate the area of the anycodings_python rectangles in bulk with:,But I expect many thousands of rows or even anycodings_python more. Is there a more efficient way of anycodings_python calculating this? ,For your given sample data, the first anycodings_python rectangle is the largest:

I want to get max rectangle area from numpy anycodings_python array that looks like that:

..class conf xmin ymin xmax ymax
   [[19. 0.78 102.79 98.85 258.17 282.53]
      [7. 0.66 245.61 211.98 270.66 234.76]
      [6. 0.56 - 6.51 143.64 39.31 286.06]
      [6. 0.5 103.77 94.07 256.6 278.14]
      ...]

For now I have:

def chooseBiggest(predictions):
   max = 0;
index = 0;
for i in range(len(predictions)):
   pred = predictions[i]
area = (pred[4] - pred[2]) * (pred[5] - pred[3])
if area > max:
   max = area
index = i

return index

You can calculate the area of the anycodings_python rectangles in bulk with:

areas = (predictions[: , 4] - predictions[: , 2]) * (predictions[: , 5] - predictions[: , 3])

Next you can obtain the index (row) with anycodings_python the largest area with:

np.argmax(areas)

For your given sample data, the first anycodings_python rectangle is the largest:

>>> predictions = np.array([
      [19., 0.78, 102.79, 98.85, 258.17, 282.53],
      ...[7., 0.66, 245.61, 211.98, 270.66, 234.76],
      ...[6., 0.56, -6.51, 143.64, 39.31, 286.06],
      ...[6., 0.5, 103.77, 94.07, 256.6, 278.14]
   ]) >>>
   areas = (predictions[: , 4] - predictions[: , 2]) * (predictions[: , 5] - predictions[: , 3]) >>>
   areas
array([28540.1984, 570.639, 6525.6844, 28131.4181]) >>>
   np.argmax(areas)
0

Suggestion : 4

I got the rather surprising result with the following test case:,True == 1 in Python i.e., the result is correct. Boolean appears due to how the histogram is initialized: hist = [(el==value) for el in next(it, [])].,Even if the function had used numpy it is unclear why would you call np.sum() on an scalar integer result.,I added this to the bottom of the max_size function:

I got the rather surprising result with the following test case:

print zheights
array([
   [0, 0, 0, 0, 0, 0, 0, 0, 3, 2],
   [0, 4, 0, 2, 4, 0, 0, 1, 0, 0],
   [1, 0, 1, 0, 0, 0, 3, 0, 0, 4],
   [0, 0, 0, 0, 4, 2, 0, 0, 0, 0],
   [0, 0, 0, 2, 0, 0, 0, 0, 1, 0],
   [4, 3, 0, 0, 1, 2, 0, 0, 0, 0],
   [3, 0, 0, 0, 2, 0, 0, 0, 0, 4],
   [0, 0, 0, 1, 0, 3, 2, 4, 3, 2],
   [0, 3, 0, 0, 0, 2, 0, 1, 0, 0],
   [0, 0, 2, 0, 0, 0, 0, 1, 0, 0]
])

max_size(zheights, value = 0)
   (True, 8)

I added this to the bottom of the max_size function:

row_length = np.sum(max_size[0])
column_length = np.sum(max_size[1])
return (row_length, column_length)

Even if the function had used numpy it is unclear why would you call np.sum() on an scalar integer result.

>>> isinstance(True, int)
True
   >>>
   True + 0
1