Change color of a specific region in eye image [Matlab]

前端 未结 2 1009
自闭症患者
自闭症患者 2021-01-17 04:55

I am trying to change the pupil of the following image:
\"enter
This is not a featur

2条回答
  •  温柔的废话
    2021-01-17 05:46

    Here is a solution for manual selection of the pupil. Basically you select a color with which to want to fill the pupil, then trace a region of interest by hand and the region gets filled. In the example I selected a ridiculously large region but you see the point :)

    Here is the code:

    clear
    clc
    close all
    
    A = imread('pupil.jpg');
    
    imshow(A)
    
    %// Create dialog box to choose color to fill.
    ListColors = {'red' 'green' 'blue' 'magenta' 'white' 'yellow' 'cyan' 'random'};
    [Selection,ok] = listdlg('ListString',ListColors,'PromptString','Choose color','SelectionMode','single');
    
    %//Freehand tool
    hHand = imfreehand(gca,'Closed',true);
    
    %// createMask is a method from imfreehand. Image processing toolbox required.
    mask = createMask(hHand);
    
    %// Create color mask. You can create your own mask with any 3-element vector.
    
    switch ListColors{Selection}
    
            case 'red'
                colormask = 255*[1, 0, 0];
    
            case 'green'
                colormask = 255*[0, 1, 0];
    
            case 'blue'
                colormask = 255*[0, 0, 1];
    
            case 'white'
                colormask = 255*[1, 1, 1];
    
            case 'yellow'
                colormask = 255*[1, 1, 0];
    
            case 'magenta'
                colormask = 255*[1, 0, 1];
    
            case 'cyan'
                colormask = 255*[0, 1, 1];
    
            case 'random'
                colormask = 255*[rand(1), rand(1), rand(1)];
    end
    
    % Extract the individual channels.
    redChannel = A(:, :, 1);
    greenChannel = A(:, :, 2);
    blueChannel = A(:, :, 3);
    
    %// Apply mask to the ROI
    redChannel(mask) = colormask(1);
    greenChannel(mask) =  colormask(2);
    blueChannel(mask) = colormask(3);
    
    %// Generate 3 channels final image
    A = cat(3, redChannel, greenChannel, blueChannel);
    
    imshow(A)
    

    A few screenshots to show you the output:

    1) Select the color (here random)

    enter image description here

    2) Trace the region

    enter image description here

    3) Output:

    enter image description here

    That was fun haha hope that helps!

提交回复
热议问题