图像比例比较双精度

发布于 2025-01-02 09:18:11 字数 1334 浏览 2 评论 0原文

我的数学真的很弱,而且图像调整大小算法有问题。 我正在尝试将图像大小调整为特定比例。

double neededRatio = 1.6d;
while (!AboutEqual(imageRatio, neededRatio))
{
    var cropPixels = 10;
    //crop code
    ...
    imageRatio = (double)img.Width / img.Height;
}

public static bool AboutEqual(double x, double y)
{
    double epsilon = Math.Max(Math.Abs(x), Math.Abs(y)) * 1E-15;
    return Math.Abs(x - y) <= epsilon;
}

问题是,我似乎无法找到正确的像素数来裁剪以使 AboutEqual 方法真正起作用(我发现它 这里)。有时它会失败并且图像会无限期地被裁剪,我尝试记录 AboutEqual 方法的内部工作原理,它显示了我觉得奇怪的东西,例如:

X: 1.5249500998004  Y: 1.6 result: false
X: 1.55600814663951 Y: 1.6 result: false
X: 1.55600814663951 Y: 1.6 result: false
X: 1.58835758835759 Y: 1.6 result: false
X: 1.62208067940552 Y: 1.6 result: false
X: 1.60084925690021 Y: 1.6 result: false
X: 1.5796178343949  Y: 1.6 result: false
X: 1.61388286334056 Y: 1.6 result: false
X: 1.59219088937093 Y: 1.6 result: false
X: 1.62749445676275 Y: 1.6 result: false
X: 1.60532150776053 Y: 1.6 result: false
X: 1.58314855875831 Y: 1.6 result: false
X: 1.61904761904762 Y: 1.6 result: false
X: 1.59637188208617 Y: 1.6 result: false
X: 1.63341067285383 Y: 1.6 result: false

链接的问题说“如果 x 和 y 都是计算出的值,那么你必须增加 epsilon。” - 我该如何做到这一点并找到要裁剪的最佳像素数?

I'm really weak with math and having a problem with an image resize algorithm.
I'm trying to resize an image to a a specific ratio.

double neededRatio = 1.6d;
while (!AboutEqual(imageRatio, neededRatio))
{
    var cropPixels = 10;
    //crop code
    ...
    imageRatio = (double)img.Width / img.Height;
}

public static bool AboutEqual(double x, double y)
{
    double epsilon = Math.Max(Math.Abs(x), Math.Abs(y)) * 1E-15;
    return Math.Abs(x - y) <= epsilon;
}

The problem is, I can't seem to find the right number of pixels to crop to actually make the AboutEqual method work (I found it here). Sometimes it fails and the image get cropped indefinitely, I tried to log the inner-workings of the AboutEqual method and it's showing things I find weird, like:

X: 1.5249500998004  Y: 1.6 result: false
X: 1.55600814663951 Y: 1.6 result: false
X: 1.55600814663951 Y: 1.6 result: false
X: 1.58835758835759 Y: 1.6 result: false
X: 1.62208067940552 Y: 1.6 result: false
X: 1.60084925690021 Y: 1.6 result: false
X: 1.5796178343949  Y: 1.6 result: false
X: 1.61388286334056 Y: 1.6 result: false
X: 1.59219088937093 Y: 1.6 result: false
X: 1.62749445676275 Y: 1.6 result: false
X: 1.60532150776053 Y: 1.6 result: false
X: 1.58314855875831 Y: 1.6 result: false
X: 1.61904761904762 Y: 1.6 result: false
X: 1.59637188208617 Y: 1.6 result: false
X: 1.63341067285383 Y: 1.6 result: false

The linked question says "If both x and y are computed values then you have to increase the epsilon." - How do I do that and find the best number of pixels to crop?

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

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

发布评论

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

评论(1

夏花。依旧 2025-01-09 09:18:11

您不需要迭代即可获得您的价值。计算目标 x 值,该值将为您提供所需的比率,并删除多余的像素。

假设 x 太大并且您想要裁剪它:

// x/y should be 1.6
double neededRatio = 1.6d;

// so if we know y, then x should be:
int xTarget = (int)Math.Round(y * neededRatio);

// pixels to crop
int pixelsToCrop = x - xTarget;

[Edit]

重点是,此代码获取您需要的目标 x达到(假设您需要该比率)。如果您仍然认为需要循环,那么没有什么可以阻止您这样做:

while (x > xTarget)
{
    // crop 10 pixels
    // and do magic stuff
}

You don't need to iterate to get your value. Calculate the target x value which would give you the desired ratio, and remove extra pixels.

Presuming that x is too large and you want to crop it:

// x/y should be 1.6
double neededRatio = 1.6d;

// so if we know y, then x should be:
int xTarget = (int)Math.Round(y * neededRatio);

// pixels to crop
int pixelsToCrop = x - xTarget;

[Edit]

The point is, this code gets the target x you will need to get to (presuming you need the ratio). If you still thing you need a loop, nothing stops you from doing:

while (x > xTarget)
{
    // crop 10 pixels
    // and do magic stuff
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文