Oct
6

如何使用 PHP 计算一个边界框相对于另一个边界框的大小

10/06/2023 01:21 AM 经过 Admin Php


在本教程中,我们将找到一个边界框相对于另一个边界框的大小。 首先,我们将边界框定义为“$box”和“$box2”并定义它们的位置。

$box1 = array(
    'x_min' => -0.462,
    'y_min' => -120.329,
    'x_max' => 94.77,
    'y_max' => -3.674,
);

$box2 = array(
    'x_min' => -0.247,
    'y_min' => -5.025,
    'x_max' => 3.939,
    'y_max' => 0.301,
);

接下来计算每个边界框的宽度和高度。

$box1_width = $box1['x_max'] - $box1['x_min'];
$box1_height = $box1['y_max'] - $box1['y_min'];
$box2_width = $box2['x_max'] - $box2['x_min'];
$box2_height = $box2['y_max'] - $box2['y_min'];

接下来我们将计算每个边界框的面积。

$box1_area = $box1_width * $box1_height;
$box2_area = $box2_width * $box2_height;

接下来,我们计算第二个边界框相对于第一个边界框的大小百分比。

$size_percentage = ($box2_area / $box1_area) * 100;
$size_percentage = round($size_percentage, 2);

这是整体代码。

<?php

$box1 = array(
    'x_min' => -0.462,
    'y_min' => -120.329,
    'x_max' => 94.77,
    'y_max' => -3.674,
);

$box2 = array(
    'x_min' => -0.247,
    'y_min' => -5.025,
    'x_max' => 3.939,
    'y_max' => 0.301,
);

// Calculate the width and height of each bounding box
$box1_width = $box1['x_max'] - $box1['x_min'];
$box1_height = $box1['y_max'] - $box1['y_min'];
$box2_width = $box2['x_max'] - $box2['x_min'];
$box2_height = $box2['y_max'] - $box2['y_min'];

// Calculate the area of each bounding box
$box1_area = $box1_width * $box1_height;
$box2_area = $box2_width * $box2_height;

// Calculate the size percentage of the second bounding box with respect to the first one
$size_percentage = ($box2_area / $box1_area) * 100;
$size_percentage = round($size_percentage, 2);

// Print the size percentage
echo "The size percentage of the second bounding box with respect to the first one is: " . $size_percentage . "%";

你的想法

搜索
赞助
密码观察
跟着我们
公告

添加了新工具:SVG 缩放尺寸计算器

赞助