2016-10-19 15 views
1

enter image description here 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 
+0

の領域に私は、これはフォローアップの質問だと思い、他のものにリンクしてください。塗りつぶしをグラデーションにしたいですか?どちらの方向に?色の勾配、または強度の勾配? –

+0

ご質問ありがとうございます。私は表現を変更しました。 – pring

+0

また、他の回答を有効として受け入れることを検討してください。 –

答えて

4

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
  • はちょうど追加するラベルでそれをマスク画像上のすべての行の色のリストを「光」を表す
  • 繰り返しV値の範囲を、作成HSV
  • に変換しますそれそのラベル

enter image description here

+0

ありがとうございます。それはShaiのコードよりも理解するのが難しいですが、最後に私はこれらを理解しています。 HSVは簡単に考えることができます。イメージプロセスはインデックスに基づいています。私はもう一つ質問があります、背景を透明にする方法は? – pring

+0

@pring 4チャンネル、RGBA画像が必要です。最後は透明です! –

+0

あなたのコードやShaiのコードに基づいてどの程度拡張するのですか? – pring

関連する問題