如何在边界框中扩展点以匹配另一个边界框?

发布于 2025-01-24 13:28:50 字数 1436 浏览 2 评论 0原文

这是用于我正在处理的手写识别算法。我试图缩放一组点,以便无论您写有多大或大的数字,最终都将其缩放到相同的大小。

我很难弄清楚如何做到这一点。我的方法就是这样。我希望将边界框缩放到内部以匹配x =(-256,256)和y =(-256,256)的边界BO中。它应该伸展并挤压所有点,以使四个角相匹配那个大小。因此,我所做的是计算两个点之间的最大x距离,然后将其分为512,这应该给我多少,我需要多少缩放x轴上的每个点才能匹配-256,256边界。重复Y轴。直觉上,我认为它应该起作用,但行不通。

我该怎么做?在数学中,这是什么所谓的,我在这里要完成的工作?

这是我的Python代码和我的尝试。 PointMod应该具有缩放点。

import matplotlib.pyplot as plt
import math

fig = plt.figure()
ax = fig.add_subplot()

points = [(73, 172), (3, 171), (-82, 170), (-130, 164), (-130, 103), (-130, 51), (-89, 80), (-35, 91), (16, 81), (57, 55), (71, 9), (65, -39), (36, -77), (-18, -85), (-65, -68), (-104, -32)]

def boundingbox(points):
    x_min = min(point[0] for point in points)
    x_max = max(point[0] for point in points)
    y_min = min(point[1] for point in points)
    y_max = max(point[1] for point in points)
    return [(x_min, y_min), (x_min, y_max), (x_max, y_max), (x_max, y_min)]

bb = boundingbox(points)

def distance(point1, point2):
    return math.sqrt((point1[0] - point2[0])**2+(point1[1] - point2[1])**2)

xlen = distance(bb[0], bb[3])
ylen = distance(bb[0],bb[1])

pointsmod = []
xfac = 512/xlen
yfac = 512/ylen

for point in points: # My  attempt at scaling the points
    x = point[0]*xfac
    y = point[1]*yfac
    pointsmod.append((x,y))

plt.xlim(-256, 256)
plt.ylim(-256, 256)

plt.scatter(*zip(*points))
plt.scatter(*zip(*pointsmod))

ax.set_aspect('equal')
plt.show()

It's for a handwriting recognition algorithm I'm working on. I'm trying to scale the set of points so that no matter how small or big you write the digit, it all gets scaled to the same size in the end.

I'm having trouble figuring out how to do this. My approach was this. I want the bounding box to be scaled within to match within the bounding bo defined by x = (-256,256) and y = (-256,256) perfectly. It should stretch and squish all points so that the 4 corners match that size. So what I did was calculate the max x distance between the points, then divide that into 512 which should give me how much I need to scale each point on the x-axis to match the -256, 256 borders. Repeat that for the y-axis. Intuitively I thought it should work but it doesn't.

How do I do this? And what is this called in Mathematics what I'm trying to accomplish here?

Here's my Python code and my attempt at it. pointsmod should have the scaled points.

import matplotlib.pyplot as plt
import math

fig = plt.figure()
ax = fig.add_subplot()

points = [(73, 172), (3, 171), (-82, 170), (-130, 164), (-130, 103), (-130, 51), (-89, 80), (-35, 91), (16, 81), (57, 55), (71, 9), (65, -39), (36, -77), (-18, -85), (-65, -68), (-104, -32)]

def boundingbox(points):
    x_min = min(point[0] for point in points)
    x_max = max(point[0] for point in points)
    y_min = min(point[1] for point in points)
    y_max = max(point[1] for point in points)
    return [(x_min, y_min), (x_min, y_max), (x_max, y_max), (x_max, y_min)]

bb = boundingbox(points)

def distance(point1, point2):
    return math.sqrt((point1[0] - point2[0])**2+(point1[1] - point2[1])**2)

xlen = distance(bb[0], bb[3])
ylen = distance(bb[0],bb[1])

pointsmod = []
xfac = 512/xlen
yfac = 512/ylen

for point in points: # My  attempt at scaling the points
    x = point[0]*xfac
    y = point[1]*yfac
    pointsmod.append((x,y))

plt.xlim(-256, 256)
plt.ylim(-256, 256)

plt.scatter(*zip(*points))
plt.scatter(*zip(*pointsmod))

ax.set_aspect('equal')
plt.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱世争霸 2025-01-31 13:28:50

好吧,您没有考虑到不同的起源。您的边界框并不以(0,0)为中心。假设我们

xmin = min(point[0] for point in points)
xmax = max(point[0] for point in points)
ymin = min(point[1] for point in points)
ymax = max(point[1] for point in points)
xlen = xmax - xmin
ylen = ymax - ymin

将翻译从 box到(-256,+256)的翻译是:

def scale( oldx, oldy ):
    newx = (oldx - xmin) * 512 / xlen - 256
    newy = (oldy - ymin) * 512 / ylen - 256
    return newx, newy

将(0,0)移至左上,然后进行缩放,然后将原点移回到中间。

Well, you're not taking the different origins into account. Your bounding box is not centered on (0,0). Say we have

xmin = min(point[0] for point in points)
xmax = max(point[0] for point in points)
ymin = min(point[1] for point in points)
ymax = max(point[1] for point in points)
xlen = xmax - xmin
ylen = ymax - ymin

Then the translation from the points box to (-256,+256) is this:

def scale( oldx, oldy ):
    newx = (oldx - xmin) * 512 / xlen - 256
    newy = (oldy - ymin) * 512 / ylen - 256
    return newx, newy

That moves the (0,0) to upper left, then does the scaling, then moves the origin back to the middle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文