首页 > 解决方案 > 向 GDI+ 位图添加一定百分比的噪点失败

问题描述

我编写了这段代码,它生成一个像素向量以设置“噪声”。问题是无论我做什么,我只得到一条 NW-SE 线。不是随机点。我究竟做错了什么?

void CPixelNoiseGenerator::GenerateRandomNoise(NoiseAmount eNoiseAmount, UINT nNoiseAmount, UINT nWidth, UINT nHeight, std::vector<CPoint>& vecPoints)
{
    unsigned long long nNumPixels = nWidth * nHeight, nNumNoisePixels = 0;
    nNumNoisePixels = (nNumPixels * nNoiseAmount) / 100;
    vecPoints.clear();

    if (nNumNoisePixels == 0)
        return;

    UINT nWidthDistance = (nWidth * nNoiseAmount) / 100, nHeightDistance = (nHeight * nNoiseAmount) / 100;

    std::random_device rd;
    std::mt19937 generator(rd());
    std::uniform_real_distribution<> distribution(0.0, (nWidth < nHeight ? nHeightDistance : nWidthDistance) * 1.0);
    auto dice = std::bind(distribution, generator);

    do
    {
        for(UINT x = 0; x < nWidth; x += nWidthDistance)
            for (UINT y = 0; y < nHeight; y += nHeightDistance)
            {
                CPoint pt; // = GetRandomPointOnDesign(nWidth, nHeight, generator);
                pt.x = x + distribution(generator);
                pt.y = y + distribution(generator);
                vecPoints.push_back(pt);
            }
    } while (vecPoints.size() <= nNumNoisePixels);

}

标签: c++winapimfcstd

解决方案


推荐阅读