1

从 Matlab 图形创建灰度图像可能会很痛苦,因为您必须恰到好处地缩放颜色图和颜色限制,以便灰色颜色图获取所有细节。我已经意识到 Photoshop 非常非常适合这样做。您加载图像并使用黑白滤镜,然后更改红色、绿色、蓝色等的级别以适应图像的细节。请参阅下面的示例

示例图像 - 应用在 MATLAB 图上的 Photoshop 黑白滤镜

我认为拥有一个可以调用的函数将非常有用,该函数接受与 Photoshop 所需的相同输入。这个函数可能是以下形式

  function bwfilter(h, C)

其中 C 是一个矩阵,它接受红色、绿色、青色等百分比的输入,h 是一个图形句柄。运行该函数后,该图形被转换为黑白并保留为 Matlab 的 .fig 格式,或者如果不可能,则导出为 png、pdf 等,可能使用Oliver Woodford 的出色 export_fig 函数

我不知道该怎么做。有人可以建议吗?当然,如果有人想迎接挑战……

4

1 回答 1

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
于 2013-04-19T06:16:58.260 回答