I replicated the algorithm perfectly in MATLAB (Based on @Ivan Kuckir answer):
function [ mO ] = ApplyBlackWhiteFilter( mI, vCoeffValues )
FALSE = 0;
TRUE = 1;
OFF = 0;
ON = 1;
numRows = size(mI, 1);
numCols = size(mI, 2);
dataClass = class(mI);
numCoeff = size(vCoeffValues, 1);
hueRadius = 1 / numCoeff;
vHueVal = [0:(numCoeff - 1)] * hueRadius;
mHsl = ConvertRgbToHsl(mI);
mO = zeros(numRows, numCols, dataClass);
vCoeffValues = numCoeff * vCoeffValues;
for jj = 1:numCols
for ii = 1:numRows
hueVal = mHsl(ii, jj, 1);
lumCoeff = 0;
% For kk = 1 we're dealing with circular distance
diffVal = min(abs(vHueVal(1) - hueVal), abs(1 - hueVal));
lumCoeff = lumCoeff + (vCoeffValues(1) * max(0, hueRadius - diffVal));
for kk = 2:numCoeff
lumCoeff = lumCoeff + (vCoeffValues(kk) * max(0, hueRadius - abs(vHueVal(kk) - hueVal)));
end
mO(ii, jj) = mHsl(ii, jj, 3) * (1 + lumCoeff);
end
end
end
Pay attention that the conversion from vPhotoshopValues
to vCoeffValues
should be done as vCoeffValues = (vPhotoshopValues - 50) ./ 50
.
As Photoshop values are in [-200, 300] and should be linearly mapped into [-5, 5] with 50 -> 0
.
Here is a comparison to Photoshop:
The maximum error is less than 1 in [0, 255] range.
The MATLAB code is available at my StackExchange Signal Processing Q29041 GitHub Repository (Look at the SignalProcessing\Q688
folder).