图像比例比较双精度
我的数学真的很弱,而且图像调整大小算法有问题。 我正在尝试将图像大小调整为特定比例。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要迭代即可获得您的价值。计算目标 x 值,该值将为您提供所需的比率,并删除多余的像素。
假设
x
太大并且您想要裁剪它:[Edit]
重点是,此代码获取您需要的目标
x
达到(假设您需要该比率)。如果您仍然认为需要循环,那么没有什么可以阻止您这样做: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:[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: