【问题标题】:C++ How to best scale image by dropping pixelsC ++如何通过删除像素来最好地缩放图像
【发布时间】:2016-02-15 01:00:24
【问题描述】:

我正在从分辨率为 102 x 77 的成像仪中捕获一帧数据。我想将其缩小到 80 x 60。质量不是主要问题,但易于实施和速度才是。

我相信我可以通过大约每 4 个像素下降一次来实现这一点:

>>> 80.0 / 102.0 
0.7843137254901961
>>> 
>>> 60.0 / 77.0 
0.7792207792207793
>>> 
>>> 102 * ( 0.75 )
76.5
>>> 77 * ( 0.75 )
57.75

既然不完全是 4,我该如何解释呢?减少获得 80 x 60 所需的像素数的最佳方法是什么?谢谢。

我迭代像素的代码:

// Initialize data store for frame pixel data
vector<quint8> data;
data.resize(frame.getHeight() * frame.getWidth());

// Try to get a frame
forever
{

    // Request a frame and check return status
    if ( !getRawFrame(frame) ) {

        qDebug() << "************************";
        qDebug() << "Failed Capture Attempt!";
        qDebug() << "************************";

        // Failed - try again
        continue;

    }

    // Get the height and width
    int h = frame.getHeight();
    int w = frame.getWidth();

    // Get the frame raw data
    vector<quint8> rawdata = frame.getRawData();

    // Iterate the pixels
    for (int y = 0; y < h; y++)
    {
        for (int x = 0; x < w; x++)
        {

            // Extract
            quint8 pixelValue = reinterpret_cast<quint8*>(rawdata.data())[y*w+x];
            int convertToInt = int(pixelValue);

            /// do stuff on pixel data

            // Downconvert
            pixelValue = convertToInt;

            // Assign
            data[y*w+x] = pixelValue;

        }
    }

    // Assign the data to the Frame now
    frame.setData(data);

    // Done with capture loop
    break;

}

【问题讨论】:

    标签: c++ pixel image-scaling downsampling


    【解决方案1】:

    在删除像素后,分辨率比你需要的更小显然不是你想要的。而且由于您不能只是神奇地取回一些像素,所以我会尽量不要一开始就丢掉那么多。

    在这种情况下,我的方法是每 5 个像素删除一次。 这将使您获得 81x61 的分辨率。现在您可以再删除 1 行和 1 列像素,就完成了。这是我能想到的最快、最简单的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 2012-07-03
      • 2010-12-30
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多