Total Pageviews

Translate

October 9, 2016

Edge Detection in Videos using MATLAB...

by 4hathacker  |  in Image Processing at  10:23 PM

Hello everyone...!!!

I hope you liked the time lapsing and gif conversion in the previous article. In this article, I have discussed about, Edge Detection, an interesting analysis in image processing. In images, it is usually defined as a sharp variation of intensity function. 



The goal of edge detection in image processing should be finding connected edges as meaningful lines and boundaries, resulting in a segmented image containing two or more regions. There is a great use of edge detection in gradual stages of machine vision system to use segmented results for tasks such as object counting, feature extraction, and classification.

Edge detection method, in general, involves the calculations of first or second derivative along the intensity profile. First derivative for difference in intensity across the edge while the sign of second derivative can be used to determine whether a pixel lies on dark or on bright side of an edge. It is easy to construct our own filters for gradient computation using masks and apply them but in this article, I have used the built-in edge detectors like sobel and canny for edge detection.The input video is given below followed by the algorithm.



The algorithm works as:
  • Read the video
  • Split it into video frames
  • Choose a predefined number of frames in a pack 
  • Convert to grayscale and detect edges
  • Again sum up the frames  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%    Name: Nitin Sharma
%%%%%    Code: edgesinvideo.m
%%%%%    MATLAB Version: R2014a
%%%%%    Toolbox Required: Image Processing Toolbox, Computer Vision 
%%%%%                                   System Toolbox
%%%%%    Utility: To find edges in video, analyzing motion connected edges, object 
%%%%%                 detection and counting, segmentation and classification.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function edgesinvideo 

%%% vidobj is input VidioReader Object
vidobj = VideoReader('C:\Users\ntnsh\Videos\testvid2.mp4');
numframes = get(vidobj, 'NumberOfFrames');

%%% finalvid is output VideoWriter Object
finalvid = VideoWriter('edgedetvid.avi');
finalvid.FrameRate = vidobj.FrameRate;
open(finalvid);

%%%              To cover up total frames, without exceeding memory limit, 
%%%              process the video in a pack of a pre-defined frames. There 
%%%              is a small code to get the number of frames upto a number 17
%%%              else it will automatically set to 10 and we have to bear frame 
%%%              loss at the end. 

c = 1;
for ii = 1 : 17
    if rem(numframes,ii) == 0
        if c < ii
            c = ii;
        end
        if c == 1
            c = 10;
        end
    end
end

startframe = 1;                       %%%  Declaring pack of frames start with 1 
stopframe = c;                       %%%   And ends with counter c

%%% Reading frames and detecting edges 
while (stopframe <= numframes)
    frames = read(vidobj, [startframe stopframe]);
    for l = 1:size(frames,4)
        temp = frames(:,:,:,l);
        temp = rgb2gray(temp);
        detectedge = edge(temp, 'canny');
        detectedge = single(detectedge);
        writeVideo(finalvid, detectedge);
    end
    startframe = startframe + c;
    stopframe = stopframe + c;
end

close(finalvid);

Note that the pack of frames used at a time may contain the number of frames such that at the end, some frames were left for processing which results in loss of video frames. For this, some mathematical calculation must be done to find the factor of total number of frames. In code, I tried to get the optimum number of frames upto range 17 in a pack, and if total number of frames is like a prime number 1097, then the pack get default frame size of 10. Make your better and improved function to overcome loss of frames. The resulting video for edge detection using canny operator is provided below.


2 comments:

  1. Works so well! Thank you so much you saved my life man!

    ReplyDelete
  2. not working properly.

    can you help me?

    ReplyDelete

Like Our Facebook Page

Nitin Sharma's DEV Profile
Proudly Designed by 4hathacker.