Image Processing System for Analysis of the Behavior of Water Drops From a Pipe

Water Drop

When water is dropping from the end of a pipe at a low flow rate, it comes back a little and leaves the pipe. The time gap between two drops and the location of the drop leaving the pipe has a chaotic behaviour. An image processing system was made to identification of this time gap and location. Those data can be used to analyze this chaotic behaviour and to make a model for that. 

First necessary equipments were arranged and took a video of  water drops. Then using an image processing system, the water drops were identified and necessary data were collected. The image processing system was written using MATLAB. 

MATLAB represents images by matrices. There are two types of images in MATLAB, RGB and gray. RGB images are coloured and gray images are black and white. RGB images make colours by Red, Green and Blue colours. There are separate 3 matrices for for each colour. A pixel is represented by an element of the matrix. Grey images have only a one matrix which shows the brightness. In this system gray images were used, because  it is easy to process gray images.

First matlab reads a frame from the video and covert that RGB image to gray. In this experiment, dark coloured water was used and the background was made in white. So the water drop can be identified by the brightness in gray image. MATLAB represents bright pixels by high values and dark pixels by low values in 255 resolution in gray images. So the  value of the pixels that shows the water drop should have low values. Then observing the gray  image it could be find that the values of those pixels were less than 100. 


In each frame MATLAB reads the pixels that belong to the red colour line in above image and check whether they are less than 100. If there are pixels with less than 100, that means there is a water drop shown in that image. The algorithm collect the frame number of the frames with water drops and the location (which pixel) of each drop. One drop can be seen in two three adjacent frames. So the frame which has highest no of pixels in those  adjacent frame set is considered as the frame which captures the time which passes the red line shown. Finally those data are exported.

To show that the algorithm identified the drop, the pixels with values less than 100 inside the blue box shown below are marked with red and saved in another video file.



The exported video which shows water drops in red
The exported video which shows water drops in red

The MATLAB code is here.


warning('off','images:initSize:adjustingMag');
vidObj = VideoReader('D:\Matlab codes\image processing\SL mo\tst.mp4');
NoF=vidObj.NumberOfFrames;
vidObj = VideoReader('D:\Matlab codes\image processing\SL mo\tst.mp4');
vidO = VideoWriter('D:\Matlab codes\image processing\SL mo\dotOut4','MPEG-4');
%Set framerat to 15fps. THen it will be in slow motion
vidO.FrameRate=15;
open(vidO);
location=[]; %this variable is used to store the locatoin data of drops
frame=0; %used to count frames
tSt=now; %used to calculate time remaining
while hasFrame(vidObj)
    frame=frame+1;
    %calculate time remaining
    tNew=now;
    el=(tNew-tSt);
    rt=el/frame;
    tot=rt*NoF;
    rem=(tot-el);
    ht=strsplit(num2str(rem*24),'.');
    h=ht{1};
    mint=strsplit(num2str(mod(rem*24*60,24)),'.');
    min=mint{1};
    X = sprintf('%0.2f%% %d of %d time remainng %sh %smin',100*frame/NoF,frame,NoF,h,min);
    disp(X);
    
    
    if frame == -1
         break;
    end
    
    
    vid=readFrame(vidObj);
    img=rgb2gray(vid);
    dim=size(img);
    tmp=vid;
    found=false;
    loc=[];
    r=400;
    %drawing boundry lines in the output video in green
    for c=1:dim(2)
        tmp(r,c,1)=0;
        tmp(r,c,2)=255;tmp(r+1,c,2)=255;tmp(r+2,c,2)=255;tmp(r+3,c,2)=255;
        tmp(r,c,3)=0;
    end
    c=575;
    for r=1:dim(1)
        tmp(r,c,1)=0;
        tmp(r,c,2)=255;tmp(r,c+1,2)=255;tmp(r,c+2,2)=255;tmp(r,c+3,2)=255;
        tmp(r,c,3)=0;
    end
    c=dim(2)-950;
    for r=1:dim(1)
        tmp(r,c,1)=0;
        tmp(r,c,2)=255;tmp(r,c+1,2)=255;tmp(r,c+2,2)=255;tmp(r,c+3,2)=255;
        tmp(r,c,3)=0;
    end
    r=400;
    for c=575:dim(2)-950
       
       
        if img(r,c)<100 %if there is a drop shown, value is less than 100
            if found
                loc(3)=c;
            else
                found=true;
                loc=[frame c 0]; %assign loc(0)=frame no, loc(1)=left pixel of drop
            end
        end
    end
    if found
        location=cat(1,location,loc);
    end
    
    %making identified drops, red
    for r =300: dim(1)
        for c=575:dim(2)-950
            if img(r,c)<100
                
                tmp(r,c,1)=255;
                tmp(r,c,2)=0;
                tmp(r,c,3)=0;
            end
        end
    end
    imshow(tmp)
    writeVideo(vidO,tmp)
   
end

close(vidO);

%Select the frames that shows the drop best
locSZ=size(location);
data=[];
max=0;
maxL=0;
for k=1:locSZ(1)-1
    pre=location(k,:);
    preDis=pre(3)-pre(2);
    if(location(k+1)-pre(1)==1)
        if(preDis>max) %The frame which show the drop best has the highest diameter
            max=preDis;
            maxL=pre;
            
        end
    else
        if(preDis>max)
            max=preDis;
            maxL=pre;
            
        end
        
        data=cat(1,data,maxL);
           

        max=0;
        
    end
end
data=cat(1,data,maxL);


Comments