Created
March 2, 2023 21:08
-
-
Save arenasys/eeae2a9c90acdcbf0d8b6914e0dc44c9 to your computer and use it in GitHub Desktop.
np/cv2 blending modes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
def NormalComposition(srcRGBA, dstRGBA): | |
srcA = np.dstack((srcRGBA[:,:,3]/255,)*3) | |
srcRGB = srcRGBA[:,:,:3] | |
dstA = np.dstack((dstRGBA[:,:,3]/255,)*3) | |
dstRGB = dstRGBA[:,:,:3] | |
outA = srcA + dstA*(1-srcA) | |
outRGB = (srcRGB*srcA + dstRGB*dstA*(1-srcA)) / outA | |
outRGBA = np.dstack((outRGB,outA[:,:,0]*255)).astype(np.uint8) | |
return outRGBA | |
def EraseComposition(srcRGBA, dstRGBA): | |
srcA = srcRGBA[:,:,3]/255 | |
dstA = dstRGBA[:,:,3]/255 | |
dstRGB = dstRGBA[:,:,:3] | |
outA = dstA*(1-srcA)*255 | |
outRGBA = np.dstack((dstRGB,outA)).astype(np.uint8) | |
return outRGBA | |
def HSVComposition(srcRGBA, dstRGBA, permutation): | |
dstRGB = dstRGBA[:,:,:3] | |
srcA = (srcRGBA[:,:,3]/255)[...,np.newaxis] | |
srcHSV = cv2.cvtColor(np.ascontiguousarray(srcRGBA[:,:,:3]), cv2.COLOR_RGB2HSV_FULL) | |
dstHSV = cv2.cvtColor(np.ascontiguousarray(dstRGBA[:,:,:3]), cv2.COLOR_RGB2HSV_FULL) | |
outHSV = np.dstack(permutation(srcHSV[:,:,0], srcHSV[:,:,1], srcHSV[:,:,2], dstHSV[:,:,0], dstHSV[:,:,1], dstHSV[:,:,2])) | |
outRGB = cv2.cvtColor(outHSV, cv2.COLOR_HSV2RGB_FULL) | |
outRGB = (dstRGB*(1-srcA) + outRGB*srcA) | |
outRGBA = np.dstack((outRGB, dstRGBA[:,:,3])).astype(np.uint8) | |
return outRGBA | |
def HLSComposition(srcRGBA, dstRGBA, permutation): | |
dstRGB = dstRGBA[:,:,:3] | |
srcA = (srcRGBA[:,:,3]/255)[...,np.newaxis] | |
srcHLS = cv2.cvtColor(np.ascontiguousarray(srcRGBA[:,:,:3]), cv2.COLOR_RGB2HLS_FULL) | |
dstHLS = cv2.cvtColor(np.ascontiguousarray(dstRGBA[:,:,:3]), cv2.COLOR_RGB2HLS_FULL) | |
outHLS = np.dstack(permutation(srcHLS[:,:,0], srcHLS[:,:,1], srcHLS[:,:,2], dstHLS[:,:,0], dstHLS[:,:,1], dstHLS[:,:,2])) | |
outRGB = cv2.cvtColor(outHLS, cv2.COLOR_HLS2RGB_FULL) | |
outRGB = (dstRGB*(1-srcA) + outRGB*srcA) | |
outRGBA = np.dstack((outRGB, dstRGBA[:,:,3])).astype(np.uint8) | |
return outRGBA | |
def HueComposition(srcRGBA, dstRGBA): | |
return HSVComposition(srcRGBA, dstRGBA, lambda sH,sS,sV,dH,dS,dV: (sH,dS,dV)) | |
def SaturationComposition(srcRGBA, dstRGBA): | |
return HSVComposition(srcRGBA, dstRGBA, lambda sH,sS,sV,dH,dS,dV: (dH,sS,dV)) | |
def ValueComposition(srcRGBA, dstRGBA): | |
return HSVComposition(srcRGBA, dstRGBA, lambda sH,sS,sV,dH,dS,dV: (dH,dS,sV)) | |
def ColorComposition(srcRGBA, dstRGBA): | |
return HLSComposition(srcRGBA, dstRGBA, lambda sH,sL,sS,dH,dL,dS: (sH,dL,sS)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment