如何填充渐变颜色从浅黄色到深黄色

如何填充渐变颜色从浅黄色到深黄色

本文介绍了如何填充渐变颜色从浅黄色到深黄色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


使用matlab,我不知道如何补丁可以使渐变色垂直在这张图片。这里我只想要一个简单的颜色渐变。我使用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


解决方案

With matlab, I don't know how patch can make gradient color vertically in this picture. Here I just want a simple color gradient. I have used bwboundaries to catch the edge function and fill pure color just like this:

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
解决方案

Following Shai's code in his answer in your other question:

  %% 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');

If you understand the code properly, you will be able to do it for vertical gradient. I accidentally did horizontal, but should be alright. The code is commented, but do not hesitate to ask. The steps are the following

  • Create random colors, as many as desired. In this case 2
  • convert to HSV
  • Create a range of V values, that represent "light"
  • Repeat that list of colors for every row on the image
  • mask it with the labels to just add it to the areas of that label

这篇关于如何填充渐变颜色从浅黄色到深黄色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:30