GIMP:POC结果与减法混合模式的GIMP不匹配

2024-09-27 00:20:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试实现GEGL混合模式,颜色减淡减法,作为Python中POC的一部分。我遵循的流程是道奇,然后是减法。我在实施过程中参考了以下来源:

  1. SVG合成规范:https://www.w3.org/TR/SVGCompositing/#comp-op-property
  2. 用于Color DodgeSubtract的GEGL实现

颜色减淡混合工作正常,与GIMP结果“几乎”相同(对于某些像素,我得到的最大强度差为1,但这满足了我的需要)。然而,我得到的减法结果与GIMP中观察到的完全不同。我把减法理解为每个通道的简单的每元素差

我使用线性RGB。以下是我的Python实现:

import cv2
import numpy as np

# Colour Dodge mode
# Alpha channel is assumed to be 1 (Opacity 100%) as this serves the purpose and 
# simplifies implementation.
def applyColourDodge(srcImage, destImage, alphaImage, iterations):
    aComp =  alphaCh + alphaCh - alphaCh * alphaCh
    
    sCdC = srcImage + destImage
    numComponents = srcImage.shape;
    numDims = len(numComponents);
    compImage = srcImage.copy()

    isAlphaPresent = False
    for channel in range(0, numComponents[numDims-1]):
        numRows = srcImage.shape[0]
        numCols = srcImage.shape[1]
        for row in range(0, numRows):
            for col in range (0, numCols):
                if (sCdC[row,col, channel] >= 1):
                    compImage[row, col, channel] = 1
                else:
                    if (srcImage[row, col, channel] == 1):
                        compImage[row, col, channel] = 1
                    else:
                        cD = destImage[row, col, channel]
                        cS = srcImage[row, col, channel]

                        if (cD == 1):
                            compImage[row, col, channel] = 1
                        else:
                            compImage[row, col, channel] = cS/(1 - cD)

    if (isAlphaPresent):
        compImage[:,:,numComponents[numDims-1]] = aComp

    return compImage

# Subtract mode
def applySubtract(srcImage, destImage, alphaImage):
    aComp =  alphaCh + alphaCh - alphaCh * alphaCh
    
    numComponents = srcImage.shape;
    numDims = len(numComponents);

    compImage = np.zeros(srcImage.shape, dtype = "float64")
    isAlphaPresent = False
    for ch in range(0, numComponents[numDims-1]):
        compImage[:,:,ch] = srcImage[:,:,ch] - destImage[:,:,ch]

    if (isAlphaPresent):
        compImage[:,:,numComponents[numDims-1]] = aComp

    return compImage

ipFolder = "D:/Work/TIS/IssueWorkedOn/VP-17871 - Dual Colour Capsules/Dual Color Caps Samples/"

ipString = 'Test Image.bmp'
refString = 'Brown Black Dodge Final.bmp'

ipBGR = cv2.imread(ipFolder + ipString, cv2.IMREAD_COLOR)

ipORG = ipBGR.copy() # Retain the original image
ipBGR = ipBGR.astype("float64") 
ipBGR = ipBGR/255 # Normalise in range [0, 1]

alphaCh = np.ones(ipBGR.shape[:2], dtype = "float64") # Alpha channel

srcImage = ipBGR.copy() # Bottom layer
destImage = srcImage.copy() # Top layer

# For debugging purpose
numComponents = ipBGR.shape;
numRows = srcImage.shape[0]
numCols = srcImage.shape[1]
print(ipORG[int(numRows/2),int(numCols/4),:])
print(ipORG[int(numRows/2),int(3*numCols/4),:])

# Actual composite generation. Desired flow: Dodge -> Dodge -> Subtract
compImage = applyColourDodge(srcImage, destImage, alphaCh, 2)
# compImage = applyColourDodge(compImage, srcImage, alphaCh, 2)
compImage = applySubtract(compImage, srcImage, alphaCh)

# Remap in the range [0, 255]
compImage = compImage * 255
compImage = compImage.astype("uint8")

print(compImage[int(numRows/2),int(numCols/4),:])
print(compImage[int(numRows/2),int(3*numCols/4),:])

# For visualisation
orgWin = "Original Image"
filWin = 'Composite Image'
toolWin = 'Tool Composite'

cv2.namedWindow(orgWin, cv2.WINDOW_FREERATIO)
cv2.namedWindow(filWin, cv2.WINDOW_FREERATIO)
cv2.imshow(orgWin, ipBGR)
cv2.imshow(filWin, compImage)
cv2.imshow(toolWin, ipRef)
cv2.waitKey(0)
cv2.destroyAllWindows()

因此,对于减法模式,我将道奇运算的输出作为顶层,原始源图像作为底层。不透明度设置为100%,因为这符合我的目的并简化了实现

代码的结构不清晰。所以,请原谅。首先,我想修正算法

以下是我用于验证的双色测试图像: Source Image。左半部分的RGB三联体:(71,66,50)和右半部分的RGB三联体:(22,255,182)

来自GIMP的Colour DodgePOC的输出。两幅图像的RGB三联体相同-左:(98、89、62);右:(24255255)

下面是应用减法后的输出:GIMPPOC。我在最终的合成图像中看到了明显的不同: GIMP图像中的RGB三联体:(69,60,35)和(425525) 来自POC图像的RGB三联体:(27,23,12)和(2,0,73)

我试图在SO上找到类似的问题,发现了thisthis、和this。但是,这些并不能帮助我理解我所观察到的行为。如果有人能在这方面帮助我,我将不胜感激

编辑(2021年10月13日): 减去(根据文档):最大值(背景-前景,0)。看来 Subtract不使用alpha信息进行混合。因为,我看到了结果的差异,尝试用BG图像独立测试减法。 因此,尝试使用两个普通RGB图像(alpha设置为1)-BG:(205,36,50),FG: (125,38,85). 合成的颜色,混合:(170,234,0),这并不重要 遵循上面的数学


Tags: 图像channelcolrgbcv2introwshape

热门问题