レイヤーブレンディングアルゴリズム (1)

一般的な混合結果の表示

 ブレンド後にレイヤーを暗くする

ノーマルモード

ブレンドカラー*不透明度+(100% - ブレンドカラーの不透明度)

void layerblend_normal(Mat &base,Mat &blend,Mat &dst,float opacity)
{
    if (base.rows != blend.rows ||
        base.cols != blend.cols ||
        base.type() != blend.type())
        return;

    dst = Mat::zeros(base.rows,base.cols,base.type());

    for (int h = 0;h < base.rows;h ++)
    {
        uchar *d1 = base.ptr<uchar>(h);
        uchar *d2 = blend.ptr<uchar>(h);
        uchar *d3 = dst.ptr<uchar>(h);
        for (int w = 0;w < base.cols;w ++)
        {
            int cw = w * base.channels();
            for (int c = 0;c < base.channels();c ++)
                d3[cw + c] = opacity * d1[cw + c] + (1.0f - opacity) * d2[cw + c];
        }
    }
}

溶解する

        ディゾルブモードで混合カラーの不透明度と塗りつぶしが両方とも 100% の場合、ベースカラーレイヤーは表示されません。ブレンドしたカラーレイヤーの不透明度を下げると、得られたカラーに小さな粒子が多数含まれていることがわかります。これらのパーティクルは、ブレンド カラーの不透明度に応じて変化します。不透明度が低いほど、より多くのブレンドされたカラーレイヤーが溶解されます。少ない部分が残ります。不透明度が高いほど、ブレンドされたカラーレイヤーの溶解される部分が減り、より多くの部分が残り、結果として得られるカラーはブレンドされたカラーに近づきます。

暗闇

        暗色混合モードでは、混合色とベースカラーを比較し、R、G、B の 3 つのグループの値のうち最も小さい値、つまり最も暗い色を結果色の値として選択します。そうすると写真全体が暗くなり、カラー画像であれば色も大きく変わります。(位置が入れ替わっても変化はありません)

void layerblend_darkness(Mat &base,Mat &blend,Mat &dst)
{
    if (base.rows != blend.rows ||
        base.cols != blend.cols ||
        base.type() != blend.type())
        return;

    dst = Mat::zeros(base.rows,base.cols,base.type());

    for (int h = 0;h < base.rows;h ++)
    {
        uchar *d1 = base.ptr<uchar>(h);
        uchar *d2 = blend.ptr<uchar>(h);
        uchar *d3 = dst.ptr<uchar>(h);
        for (int w = 0;w < base.cols;w ++)
        {
            int cw = w * base.channels();
            for (int c = 0;c < base.channels();c ++)
                d3[cw + c] = d1[cw + c] < d2[cw + c] ? d1[cw + c] : d2[cw + c];
        }
    }
}

かける

        乗算混合原理: 混合色とベースカラーの R、G、B 値に基づいて計算されます。計算式は次のとおりです: 結果の色 R = 混合色 R * ベースカラー R / 255。とB値も同様に計算されます。最終的に得られる R、G、B 値が結果色の色になります。各チャンネルの最大値は 255 であるため、結果の色の値は混合色とベースの色の値よりも小さくなります。つまり、結果の色は暗くなります。(位置が入れ替わっても変化はありません)

void layerblend_multiply(Mat &base,Mat &blend,Mat &dst)
{
    if (base.rows != blend.rows ||
        base.cols != blend.cols ||
        base.type() != blend.type())
        return;

    dst = Mat::zeros(base.rows,base.cols,base.type());

    for (int h = 0;h < base.rows;h ++)
    {
        uchar *d1 = base.ptr<uchar>(h);
        uchar *d2 = blend.ptr<uchar>(h);
        uchar *d3 = dst.ptr<uchar>(h);
        for (int w = 0;w < base.cols;w ++)
        {
            int cw = w * base.channels();
            for (int c = 0;c < base.channels();c ++)
                d3[cw + c] = 1.0f * d1[cw + c] * d2[cw + c] / 255;
        }
    }
}

色が濃い

        カラー焼き込みを使用すると、画像にすぐに暗さを加えることができます。計算式: 結果の色 = (基本色 + 混合色 - 255) * 255 / 混合色。このうち(基本色+混合色-255)、負の数があった場合はそのまま0に戻ります。したがって、ベースカラーと混合カラーが両方とも濃い場合は、そのまま黒になります。これにより、結果として得られる色の暗さが増加します。全体的な効果はコントラストが比較的強いように見えます。

void layerblend_color_dark(Mat &base,Mat &blend,Mat &dst)
{
    if (base.rows != blend.rows ||
        base.cols != blend.cols ||
        base.type() != blend.type())
        return;

    dst = Mat::zeros(base.rows,base.cols,base.type());

    for (int h = 0;h < base.rows;h ++)
    {
        uchar *d1 = base.ptr<uchar>(h);
        uchar *d2 = blend.ptr<uchar>(h);
        uchar *d3 = dst.ptr<uchar>(h);
        for (int w = 0;w < base.cols;w ++)
        {
            int cw = w * base.channels();
            for (int c = 0;c < base.channels();c ++){
                d3[cw + c] = 1.0f * d1[cw + c] * d2[cw + c] / 255;

                 int temp = d1[cw + c] + d2[cw + c] - 255;

                 temp = temp < 0 ? 0 : temp;

                 int res = 0;
                 if (d2[cw + c] == 0)
                     res = d1[cw + c];
                 else
                     res = temp * 255.0f / d2[cw + c];

                 d3[cw + c] = saturate_cast<uchar>(res);
            }
        }
    }
}

 ダークモード(暗い)

        ダークブレンドモードの方がわかりやすいですね。混合色とベースカラーのすべてのチャンネルの値を計算し、小さい方の値を結果の色として選択します。したがって、結果として得られる色は混合色またはベースカラーと同じだけであり、他の色は生成されません。白をベースカラーに混ぜるとベースカラーが得られ、黒とベースカラーを混ぜると黒が得られます。ダークモードでは、混合色とベースカラーの値が固定され、位置を反転しても混合色の結果は変化しません。このモードは暗化モードに似ています。

おすすめ

転載: blog.csdn.net/hulinhulin/article/details/133252870