プリンキピア

第14章コンピュータビジョン

14.3.1. Bounding Boxes14.3.1 境界ボックス

In object detection, we usually use a bounding box to describe the spatial location of an object. The bounding box is rectangular, which is determined by the x and y coordinates of the upper-left corner of the rectangle and the such coordinates of the lower-right corner. Another commonly used bounding box representation is the (x,y)-axis coordinates of the bounding box center, and the width and height of the box.
英語のヒント
物体検出では、ふつう境界ボックスを使って物体の空間的な位置を表す。境界ボックスは長方形で、長方形の左上の角のx座標とy座標、および右下の角の同じ座標で決まる。もう一つよく使われる境界ボックスの表し方は、境界ボックスの中心の(x,y)軸座標と、ボックスの幅と高さである。
Here we define functions to convert between these two representations: box_corner_to_center converts from the two-corner representation to the center-width-height presentation, and box_center_to_corner vice versa. The input argument boxes should be a two-dimensional tensor of shape (n, 4), where n is the number of bounding boxes.
英語のヒント
ここでは、この二つの表し方のあいだで変換する関数を定義する。box_corner_to_centerは二つの角による表し方から中心・幅・高さによる表し方に変換し、box_center_to_cornerはその逆を行う。入力引数boxesは形状が(n, 4)の2次元テンソルでなければならない。ここでnは境界ボックスの数である。
#@save
def box_corner_to_center(boxes):
    """Convert from (upper-left, lower-right) to (center, width, height)."""
    x1, y1, x2, y2 = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
    cx = (x1 + x2) / 2
    cy = (y1 + y2) / 2
    w = x2 - x1
    h = y2 - y1
    boxes = torch.stack((cx, cy, w, h), axis=-1)
    return boxes

#@save
def box_center_to_corner(boxes):
    """Convert from (center, width, height) to (upper-left, lower-right)."""
    cx, cy, w, h = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
    x1 = cx - 0.5 * w
    y1 = cy - 0.5 * h
    x2 = cx + 0.5 * w
    y2 = cy + 0.5 * h
    boxes = torch.stack((x1, y1, x2, y2), axis=-1)
    return boxes
We will define the bounding boxes of the dog and the cat in the image based on the coordinate information. The origin of the coordinates in the image is the upper-left corner of the image, and to the right and down are the positive directions of the x and y axes, respectively.
英語のヒント
座標の情報にもとづいて、画像の中の犬と猫の境界ボックスを定義する。画像の座標の原点は画像の左上の角であり、右と下がそれぞれx軸とy軸の正の向きである。
# Here `bbox` is the abbreviation for bounding box
dog_bbox, cat_bbox = [60.0, 45.0, 378.0, 516.0], [400.0, 112.0, 655.0, 493.0]
We can verify the correctness of the two bounding box conversion functions by converting twice.
英語のヒント
2回変換することで、二つの境界ボックスの変換関数が正しいことを確かめられる。
boxes = torch.tensor((dog_bbox, cat_bbox))
box_center_to_corner(box_corner_to_center(boxes)) == boxes
tensor([[True, True, True, True],
        [True, True, True, True]])
Let’s draw the bounding boxes in the image to check if they are accurate. Before drawing, we will define a helper function bbox_to_rect. It represents the bounding box in the bounding box format of the matplotlib package.
英語のヒント
画像に境界ボックスを描いて、それが正確かどうかを確かめよう。描く前に、補助関数bbox_to_rectを定義する。それは、境界ボックスをmatplotlibパッケージの境界ボックスの形式で表す。
#@save
def bbox_to_rect(bbox, color):
    """Convert bounding box to matplotlib format."""
    # Convert the bounding box (upper-left x, upper-left y, lower-right x,
    # lower-right y) format to the matplotlib format: ((upper-left x,
    # upper-left y), width, height)
    return d2l.plt.Rectangle(
        xy=(bbox[0], bbox[1]), width=bbox[2]-bbox[0], height=bbox[3]-bbox[1],
        fill=False, edgecolor=color, linewidth=2)
After adding the bounding boxes on the image, we can see that the main outline of the two objects are basically inside the two boxes.
英語のヒント
画像に境界ボックスを加えると、二つの物体の主な輪郭が基本的に二つのボックスの中にあることがわかる。
fig = d2l.plt.imshow(img)
fig.axes.add_patch(bbox_to_rect(dog_bbox, 'blue'))
fig.axes.add_patch(bbox_to_rect(cat_bbox, 'red'));