-2

I am writing a function which takes the following actions: extracting from video, convert the frames to grayscale and calculate the difference between first two images that i have extract.

I have done the code up to this point. But I have no idea how to extract the images that were extracted previously. Can anyone provide some guidance on this?

function [ vid ] = motion( input_args )

vid = (input_args); 
readerobj = VideoReader(vid); 
vidFrames = read(readerobj);

numFrames = get(readerobj, 'Number of Frames');

for k = 1 : numFrames    
    mov(k).cdata = vidFrames(:,:,:,k);    
    mov(k).colormap = [];    
    imagename=strcat(int2str(k), '.jpg');    
    %save inside output folder    
    imwrite(mov(k).cdata, strcat('output\frame-',imagename));    
end

end
rayryeng
  • 102,964
  • 22
  • 184
  • 193
newtoCS
  • 113
  • 9

1 Answers1

0

Reading your problem description, you want to do a difference of frames (in grayscale) between the previous frame and the current frame. What you can do is store the first frame as a temporary variable, then in your for loop, you would take the difference between the current frame and the previous frame. Before you iterate to the next frame, be sure to set this current frame as the previous frame, and then proceed. Obviously, you need to create the grayscale equivalent of each frame you have read in. As such, you should change your for loop to this:

prevFrame = rgb2gray(vidFrames(:,:,:,1)); %// Initialize and get grayscale

for k = 2 : numFrames %// Note we start at index 2
    currFrame = rgb2gray(vidFrames(:,:,:,k)); %// Get current frame
                                              %// and get grayscale
    %// Find difference frame
    diffFrame = uint8(abs(double(currFrame) - double(prevFrame))); 
    mov(k).cdata = cat(3,diffFrame,diffFrame,diffFrame); %// Now save to file
    mov(k).colormap = [];    
    imagename=strcat(int2str(k), '.jpg'); 

    %//save inside output folder    
    imwrite(mov(k).cdata, strcat('output\frame-',imagename));    
    prevFrame = currFrame; %// Save for next iteration
end

Pay special attention to how I calculated the difference frame. I casted each of the frames to double, then took the absolute difference, then recast it as uint8. The reason why is because if you don't do this, if there are any pixels that have a negative difference, MATLAB will saturate this difference to 0. For example, if one pixel was intensity 128 in one frame, then 255 in the next, the difference should be -127. We put an abs here because this is really a difference of 127. It doesn't matter which direction we are going in. However, MATLAB will consider this difference as 0 as anything that is less than 0 gets saturated to 0. As such, I need to convert both frames to double as the frames you read in from file will most likely be uint8. Once you find the absolute difference, we then recast as uint8 so we can save to file and also be able to display these images if desired.

Now the code will save the difference images to file. Note that you will be one frame short because we started at frame 2. This is necessary if you want to compare differences between consecutive frames. Note that when you are creating your frame, I had to replicate the difference frame and make it three channels to mimic a RGB frame. For a grayscale image, the RGB will have each channel to be all the same. This was done using the cat command, and I stacked the difference frame in the third dimension three times.

What's good about your mov structure now is that you can use this structure and create a video out of it using MATLAB's VideoWriter class for example. The structure is exactly formatted to be written to file. Simply loop through your stucture and write each structure element to file with the VideoWriter class. You'll then be able to produce a movie that shows the difference between consecutive frames.

rayryeng
  • 102,964
  • 22
  • 184
  • 193