从Matlab图形创建灰度图像可能会非常麻烦,因为您必须正确缩放色彩图和颜色限制,以便灰色色彩图能够获取所有细节。我已经意识到Photoshop非常非常适合这样做。您加载图像并使用黑白滤镜,然后更改红色,绿色,蓝色等级别以适合图像的细节。请参阅下面的示例
我认为拥有一个可以调用的函数非常有用,它可以获取与Photoshop需要相同的输入。此功能的格式可能为
function bwfilter(h, C)
其中C是输入红色,绿色,青色等百分比的矩阵,h是数字句柄。在运行该函数时,图形将转换为黑白格式,并保持为Matlab的.fig格式,或者如果不可能,则可以使用优秀的export_fig function by Oliver Woodford导出为png,pdf等。
我不知道该如何解决这个问题。有人可以提供建议吗?当然,如果有人想要接受挑战......
答案 0 :(得分:0)
这个怎么样:
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