Skip to content

Instantly share code, notes, and snippets.

@DarkSector
Created December 7, 2013 00:31
Show Gist options
  • Select an option

  • Save DarkSector/7835614 to your computer and use it in GitHub Desktop.

Select an option

Save DarkSector/7835614 to your computer and use it in GitHub Desktop.
Putting the whole process in the loop
clc;
clear;
% Start with importing the video
vidObj = VideoReader('/Users/DarkSector/Desktop/vid.avi');
% Once loaded lets get the first frame and choose an ROI on it
frame1 = read(vidObj, 1);
% now let's show the image and select the ROI on it
imshow(frame1)
roiRect = imrect;
% get the position of the rectangle as well
roiPosition = roiRect.getPosition();
%masked = createMask(roiRect, frame1);
%imshow(frame1)
% figure(1);
% subplot(1,3,1);
% plot(red_channel);
%
% subplot(1,3,2);
% plot(blue_channel);
%
% subplot(1,3,3);
% plot(green_channel);
for frame = 1:vidObj.NumberOfFrames-145
% Take frame and apply mask
% crop frame
% separate frame into three different channels
% append frame to individual channel array
% create frame object
frameObj = read(vidObj, frame);
% Create the mask
roiMask = createMask(roiRect);
% Apply the mask on all 3 dimensions/ all channels
mask_3chan = repmat(roiMask, [1,1,3]);
% Set all other pixels to 0
frameObj(~mask_3chan) = 0;
% if we are not using the rest of the pixels,
% might as well remove
% them and crop the image.
% No reason to store dummy data
frameObj = imcrop(frameObj, roiPosition);
red_channel = frameObj;
blue_channel = frameObj;
green_channel = frameObj;
%extract the red channel
red_channel = red_channel(:,:,1);
%extract the blue channel
blue_channel= blue_channel(:,:,2);
%extract the green channel
green_channel= green_channel(:,:,2);
redArray = cat(3, red_channel);
blueArray = cat(3, blue_channel);
greenArray = cat(3, green_channel);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment