那这个呢:
function StackExchange_rgb2gray_rygcbm
% http://stackoverflow.com/questions/16083685/creating-a-photoshop-like-black-and-white-filter-for-matlab-figures
function imGray = rgb2gray_rygcbm(imgRGB,Coefficients)
%
% imgRGB source RGB image
%
% Coefficients vector of color coefficients
% [red yellow green cyan blue magenta]
%
% 0.0 means that the corresponding color
% will not contribute to resulting gray image
%
% 1.0 means that the corresponding color intensity
% will be unchanged on resulting gray image
%
% values higher than 1.0 might work but only if product
% (Maximal_Color_Intensity * Color_Coefficient) <= 1
%
% rgb2gray_rygcbm(imgRGB,[1 1 1 1 1 1]) is equivalent to rgb2gray(imgRGB)
%
% Author: Andriy Nych
% Date: 2013/04/19
%
% we check what we are fed with
if length(Coefficients)~=6
error('Second argument MUST be 6 elements long!');
end
% extract color information from image
imgHSV = rgb2hsv(imgRGB);
imgH = imgHSV(:,:,1);
imgS = imgHSV(:,:,2);
imgV = imgHSV(:,:,3);
% prepare some small stuff
Coefficients(Coefficients<0) = 0;
Coefficients = [ Coefficients(:)' Coefficients(1) ];
rygcbm = linspace(0,1,7);
% cook some "magic"
imgHf = imgH;
for kk=1:6
iidx = (rygcbm(kk)<=imgH) & (imgH<rygcbm(kk+1));
tx = imgH(iidx);
ty = Coefficients(kk) + sin( (tx-rygcbm(kk))/(rygcbm(kk+1)-rygcbm(kk)) * pi/2 ).^2 * (Coefficients(kk+1)-Coefficients(kk));
imgHf(iidx) = ty;
end
% apply the "magic"
imgV2 = imgV .* imgHf;
imgN = hsv2rgb( cat(3,imgH,imgS,imgV2) );
% and this is it
imGray = rgb2gray(imgN);
end
% Now we shall test the code
% First we generate an RGB image
figure;
surf(peaks(64));
colormap(hsv(256));
F = getframe(gcf);
close(gcf);
imgRGB = F.cdata;
% Now we create simple GUI and display the results
mm = 0.2;
figure('Color','w', 'Units','normalized', 'Position',[0 0 1 1]+[+1 +1 -2 -2]*mm);
imgGray = rgb2gray(imgRGB);
a1 = subplot(1,2,1); h0 = imshow(imgRGB); axis on; title('Original image');
a2 = subplot(1,2,2); h1 = imshow(imgGray); axis on; title('rgb2gray');
mm = 0.05;
set(a1, 'Units','normalized', 'Position',[0.0 0.0 0.5 1.0]+[+1 +1 -2 -2]*mm );
set(a2, 'Units','normalized', 'Position',[0.5 0.0 0.5 1.0]+[+1 +1 -2 -2]*mm );
pause(1);
% we convert the original image for different combination of the coefficients
Coeffs = [0 0 0 0 0 0];
NSteps = 10;
for ic=1:6
% we'll change one coefficient at a time
for k=0:NSteps
% we modify the coefficient
Coeffs(ic) = k/NSteps;
% and use them for image conversion
imgGray = rgb2gray_rygcbm(imgRGB,Coeffs);
% now we show the result
axis(a2);
imshow(imgGray); axis on;
%set(h1,'CData',imgGray);
title(a2, sprintf('r:%5.2f y:%5.2f g:%5.2f c:%5.2f b:%5.2f m:%5.2f',Coeffs) );
drawnow;
pause(.1);
end
end
end