【问题标题】:How to calculate resolution based on megapixels and aspect ratio如何根据百万像素和纵横比计算分辨率
【发布时间】:2014-07-10 00:25:32
【问题描述】:

如何在 C# 中计算基于百万像素的精确分辨率?

假设我有一个 5000x4000 像素的图像,我想重新缩放到 0.6mpx,我怎样才能获得精确的水平和垂直分辨率来获得 0.6mpx 的图像并保持原始纵横比?

我有这个:

int nXPixels=5000;  // original image Width
int nYPixels=4000;  // original image Height
float fRatio=5000/4000;    // original image aspect ratio
int nNewPixelResolution=600000; // 0.6mpx new resolution

int nNewXPixels=?
int nNewYPixels=?

【问题讨论】:

  • 这个问题的答案是在 2 秒的 Google 搜索中找到的:math.stackexchange.com/questions/180804/…
  • @durbnpoisn 不是;没有提到任何关于面积(或百万像素)的内容。
  • 实际上@durbnpoisn,您的链接没有提供答案。 OP 希望指定输出 MegaPixels(wxh 的总像素)而不是输出宽度或高度。

标签: c# image resolution


【解决方案1】:

百万像素的数量等于分辨率的长宽乘积除以一百万。因此,对于您的 5000x4000 像素,您将拥有 20 Mpx。要将其重新缩放为 0.6Mpx,但保持比例,您只需将每个维度重新缩放 20Mpx/0.6Mpx 的平方根,这样,当新计算的维度相乘时,结果将等于 0.6Mpx

所以你的计算应该类似于:

ratio=sqrt(20/0.6)
newWidth=oldWidth/ratio
newHeight=oldHeight/ratio

如果然后将newWidthnewHeight 相乘,则结果应约为0.6Mpx,即600000 px

【讨论】:

    【解决方案2】:
    int nXPixels=5000;  // original image Width
    int nYPixels=4000;  // original image Height
    //float fRatio=5000/4000;    // original image aspect ratio
    int nNewPixelResolution=600000; // 0.6mpx new resolution
    int nOldPixelResolution=nXPixels*nYPixels;
    double conv = Math.sqrt((double)nNewPixelResolution/(double)nOldPixelResolution);
    int nNewXPixels= (int)Math.round(nXPixels*conv);
    int nNewYPixels= (int)Math.round(nYPixels*conv);
    

    【讨论】:

    • 如果是这种情况,那么我们只需先将两个整数转换为 (float) 即可。我已经更新了代码。
    • conv 为 0,因为他正在将新分辨率除以旧分辨率,并且他正在执行 int 除法,结果为 0(因为新分辨率小于旧分辨率)。将float 替换为double 并在括号内添加1.0,如下所示:double conv=Math.Sqrt(1.0*nNewPixelResolution/nOldPixelResolution)。这将强制除法的结果类型为 double,因为至少有一个数字是 double)
    猜你喜欢
    • 2013-01-19
    • 2014-04-03
    • 2021-11-24
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 2011-11-16
    相关资源
    最近更新 更多