how to fill gradient color from light yellow to dark yellow?

前端 未结 1 1202
旧巷少年郎
旧巷少年郎 2021-01-24 04:44

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 fu

相关标签:
1条回答
  • 2021-01-24 05:40

    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

    0 讨论(0)
提交回复
热议问题