【问题标题】:how to fill gradient color from light yellow to dark yellow?如何填充从浅黄色到深黄色的渐变色?
【发布时间】:2016-10-19 13:53:28
【问题描述】:

用matlab,我不知道patch如何在这张图片中垂直制作渐变色。在这里,我只想要一个简单的颜色渐变。我已经使用 bwboundaries 来捕捉边缘函数并填充纯色,就像这样:

for i=1:4
  input=imread(['heartspline2_4_',num2str(i)],'bmp');  
  figure,imshow(input);
  BW=im2bw(input,graythresh(input));
  [B,L]=bwboundaries(BW,'noholes');
  for k=1:length(B)
      boundary=B{k};
      plot(boundary(:,2),boundary(:,1),'k','LineWidth',2);
      fvc=[1 1 0;1 0 0;0 0 1];
      hold on;
      axis off;
      if (k==1)
          patch(boundary(:,2),boundary(:,1),'w');
      else
          p=patch(boundary(:,2),boundary(:,1),'y');
      end
  end
      saveas(gca,['y_','heartspline2_4_',num2str(i)],'bmp')
      close(gcf)
end

【问题讨论】:

  • 我想这是一个后续问题,请链接到另一个问题。你希望填充是渐变的吗?朝哪个方向?颜色的渐变,还是强度的渐变?
  • 感谢您的提问。我已经修改了我的表达方式。
  • 另外,考虑接受另一个答案为有效。
  • 好的,没错!
  • 看看doc patch,更高级的例子展示了如何在补丁上设置颜色渐变。

标签: matlab colors


【解决方案1】:

Following Shai's code 在他对您的其他问题的回答中:

  %% Load image %%
close all; clear all; clc;
img = imread('https://i.stack.imgur.com/yO8Nd.jpg');  %// read image
bw = img(:,:,1) > 128;  %// convert to binary mask
lb = bwlabel(bw,4);  %// extract distinct regions

%%

%% Create as many colors as needed
% example: 2

cmap=rand(2,3);  % make this yellow if needed

% lets convert to HSV, we can tune the intesity of the image better here
cmap=rgb2hsv(cmap);

% for each color, lets crate a set of colors.

for ii=1:size(cmap,1);


       colors{ii}= [cmap(ii,1)*ones(1,size(img,2)); cmap(ii,2)*ones(1,size(img,2)); linspace(0.3,1,size(img,2))].';
       %  Modify the limits of linspace
       % to achieve control over the limits
end

% Now we have the colors, lets create an image of vertical colors and mask
% it
cimage=zeros(size(img,1),size(img,2),3); % empthy color image
finalimage=cimage;
for ii=1:size(colors,2)
    colors{ii}=hsv2rgb(colors{ii});
    cimage=permute(reshape(repmat(colors{ii},[size(img,1),1,1]),[size(img,2),size(img,1),3]),[2,1,3]); % there is probably a simpler way 
    finalimage=finalimage+cimage.*repmat((lb==ii),[1 1 3]);
end


figure; imshow(finalimage, [], 'border', 'tight'); 

如果你正确理解了代码,你将能够做到垂直渐变。我不小心做了水平,但应该没问题。代码已注释,但请不要犹豫。步骤如下

  • 根据需要创建随机颜色。在这种情况下 2
  • 转换为HSV
  • 创建一系列V 值,代表“光”
  • 为图像上的每一行重复该颜色列表
  • 用标签掩盖它,只是将它添加到该标签的区域

【讨论】:

  • 谢谢。比 Shai 的代码更难理解,但我终于明白了这些。 HSV 很容易想到。图像处理基于索引。我还有一个问题,如何设置背景透明?
  • @pring 你需要第四个通道,一个 RGBA 图像。最后一个是透明度!
  • 如何根据你的代码或者Shai的代码来扩展?
  • @pring 前 4 行是从他的回答中复制粘贴的
猜你喜欢
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多