我有一个blendImage,一个frameImage(来自JavaCameraView)和一个maskImage。我需要使用减光混合模式将blendImage和frameImage混合在一起,并使用根据maskImage计算得出的Alpha。为此,我要乘以我计算出变暗的frameImagePixel的值,并与之进行比较。然而...

for (int i = 0; i < blendImage.rows; i++) {
    uchar *blendImagePixel = blendImage.ptr(i);
    uchar *frameImagePixel = frameImage.ptr(i);
    uchar *maskImagePixel = maskImage.ptr(i);
    int channelsToBlend = min(3, blendImage.channels()); //if it exists, never blend alpha channel
    for (int j = 0; j != blendImage.cols * blendImage.channels(); j += blendImage.channels()) {
        uchar adjustedMaskImagePixel = saturate_cast<uchar>( (maskImagePixel[j] * maskContrast) + maskBrightness);
        for (int c = 0; c < channelsToBlend; c++) {

            float alpha = (float) map(adjustedMaskImagePixel, 0, 255, 0, 1); //if black, alpha = 0
            float beta  = (float) 1.0 - alpha;

            switch (blendMode) {
                case LIGHTEN: {

                    /* THIS WORKS AND I DON'T KNOW WHY */
                    frameImagePixel[j + c]  = (uchar) (frameImagePixel[j + c] * alpha);
                    if (blendImagePixel[j + c] > frameImagePixel[j + c])
                        frameImagePixel[j + c] = blendImagePixel[j + c];

                     /* THIS DOESN'T WORK AND I DON'T KNOW WHY
                    uchar darkenedFrameImagePixel = (uchar) (frameImagePixel[j + c] * alpha);
                    if (blendImagePixel[j + c] > darkenedFrameImagePixel )
                        frameImagePixel[j + c] = blendImagePixel[j + c];
                    */

                    break;
                }
            }
        }
    }
}

当我计算变暗的frameImagePixel的值并将其存储在frameImagePixel本身中时,最终图像是完美的。
但是,当我计算变暗的frameImagePixel的值但将其存储在uchar中并与该uchar变量进行比较时,结果是嘈杂的颜色受损-基本上是小故障。
我不明白这两个措词似乎是等效的。为什么一个可以完美工作而另一个却无法工作?

最佳答案

这些操作似乎并不等效。在第一种情况下

frameImagePixel[j + c] = (uchar) (frameImagePixel[j + c] * alpha);

独立于更改frameImagePixel
if (blendImagePixel[j + c] > frameImagePixel[j + c])

每个像素乘以alpha。在第二种情况下
blendImagePixel[j + c] < darkenedFrameImagePixel

没发生什么事。并非所有像素都乘以alpha。那是很大的不同。

例:

第一种情况
frameImagePixel[j + c] = 10
alpha = 0.5
blendImagePixel[j + c] = 0
=> frameImagePixel[j + c] == 5
=> blendImagePixel[j + c] > frameImagePixel[j + c] == false

第二种情况
frameImagePixel[j + c] = 10
alpha = 0.5
blendImagePixel[j + c] = 0
=> darkenedFrameImagePixel == 5
=> blendImagePixel[j + c] > frameImagePixel[j + c] == false
=> frameImagePixel[j + c] == 10

08-24 17:48