Search This Blog

Invisible Cloth Effect using Image Processing | MATLAB

Algorithm: 1. Capture and store the background frame 2. Detect the red colored cloth using color detection and segmentation algorithm. 3. Using proper masking technique , superimpose the background part in that red color detected part.

clc

clear all

close all

warning off;

back=imread('back.jpg');

c=webcam;

while true

    e=c.snapshot;

    mkdir=createMask(e);

    mkdir=bwareafilt(~mkdir,1);

    mkdir=imfill(mkdir,'holes');

    mkdir=~mkdir;

    [r ca b]=size(e);

    backa=imresize(back,[r ca]).*uint8(~mkdir);

    subplot(1,2,1);

    imshow(e);

    subplot(1,2,2);

    imshow(backa+e.*uint8(mkdir));

    drawnow;

end

 

Function Part:

function [BW,maskedRGBImage] = createMask(RGB)

%createMask  Threshold RGB image using auto-generated code from colorThresholder app.

%  [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using

%  auto-generated code from the colorThresholder app. The colorspace and

%  range for each channel of the colorspace were set within the app. The

%  segmentation mask is returned in BW, and a composite of the mask and

%  original RGB images is returned in maskedRGBImage.

 

% Auto-generated by colorThresholder app on 14-Aug-2020

%------------------------------------------------------

 

 

% Convert RGB image to chosen color space

I = rgb2hsv(RGB);

 

% Define thresholds for channel 1 based on histogram settings

channel1Min = 0.929;

channel1Max = 0.033;

 

% Define thresholds for channel 2 based on histogram settings

channel2Min = 0.129;

channel2Max = 1.000;

 

% Define thresholds for channel 3 based on histogram settings

channel3Min = 0.000;

channel3Max = 0.863;

 

% Create mask based on chosen histogram thresholds

sliderBW = ( (I(:,:,1) >= channel1Min) | (I(:,:,1) <= channel1Max) ) & ...

    (I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...

    (I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);

BW = sliderBW;

 

% Invert mask

BW = ~BW;

 

% Initialize output masked image based on input image.

maskedRGBImage = RGB;

 

% Set background pixels where BW is false to zero.

maskedRGBImage(repmat(~BW,[1 1 3])) = 0;

 

end

 

 


Join us on Telegram: https://t.me/matlabcastor

No comments

MATLAB