Image processing 将图像调整为最大高度和最大宽度范围的数学方法
给出一个图像:Image processing 将图像调整为最大高度和最大宽度范围的数学方法,image-processing,resize,scaling,Image Processing,Resize,Scaling,给出一个图像: maxWidth = 400; maxHeight = 200; width = photo.Width; height = photo.Height; 如果任一维度超过最大属性,我将如何缩放图像 以下是一些测试用例: 300x300 : Too tall, but width ok. 500x200 : Too wide, but height ok. 650x300 : Too tall and too wide 300x190 : Fine, do
maxWidth = 400;
maxHeight = 200;
width = photo.Width;
height = photo.Height;
如果任一维度超过最大属性,我将如何缩放图像
以下是一些测试用例:
300x300 : Too tall, but width ok.
500x200 : Too wide, but height ok.
650x300 : Too tall and too wide
300x190 : Fine, don't resize
我很难想象这个问题的数学,如果它太简单的话,很抱歉!给我带来最大麻烦的情况是两个维度都超过了允许的最大值。计算两个比率(使用浮点结果):
- 输入宽度除以允许的最大宽度
- 输入高度除以最大允许高度
- 如果两个比率都小于1.0,则不要调整大小
- 如果其中一个比率>1.0,则按该系数缩小比例
- 如果两个比率均>1.0,则按两个因素中较大者的比例缩小
scale = min(1, min(maxWidth/photo.Width, maxHeight/photo.Height))
确保除法运算使用浮点运算。如何做到这一点因语言而异。在C/Java/C#及其类似语言中,将其中一个操作数强制转换为浮点。我的数学很糟糕,但是,假设你想要一个比例标度,我会这样解决它:
if maxWidth < photo.Width
width = 'too big'
if maxHeight < photo.Height
height = 'too big'
if height == 'to big' & width == 'too big'
x = photo.Width / maxWidth;
y = photo.Height / maxHeight;
if x > y
scale_by_width(photo)
else
scale_by_height(photo)
elseif height == 'too big'
scale_by_height(photo)
elseif width == 'too big'
scale_by_width(photo)
else
do_nothing
如果maxWidthy
按宽度缩放(照片)
其他的
按高度缩放(照片)
其他高度==‘太大’
按高度缩放(照片)
elseif width==“太大”
按宽度缩放(照片)
其他的
什么也不做