Skip to content

Instantly share code, notes, and snippets.

@DarkSector
Created December 7, 2013 13:29
Show Gist options
  • Select an option

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

Select an option

Save DarkSector/7841289 to your computer and use it in GitHub Desktop.
clc;
clear;
% Start with importing the video
vidObj = VideoReader('/Users/DarkSector/Desktop/vid2.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)
% We will need global variables
% so that the array is initialized.
global redArray;
global greenArray;
global blueArray;
% 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
% Take frame and apply mask
% crop frame
% separate frame into three different channels
% append frame to individual channel array
disp(frame);
% create frame object
frameObj = read(vidObj, frame);
% create another rectangle from roiPosition
% roiRect = imrect(frameObj, roiPosition);
% Create the mask
% roiMask = createMask(frameObj, roiPosition);
roiMask = poly2mask(roiPosition(1), roiPosition(2), roiPosition(3), roiPosition(4));
% 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 green channel
green_channel= green_channel(:,:,2);
%extract the blue channel
blue_channel= blue_channel(:,:,3);
% if size(redArray) == 0
% % meaning the array is empty so it needs
% % to be appended once
% redArray = cat(3, red_channel);
% elseif size(greenArray) == 0
% greenArray = cat(3, green_channel);
% elseif size(blueArray) == 0
% blueArray = cat(3, blue_channel);
% end
redArray = cat(3, redArray, red_channel);
greenArray = cat(3, greenArray, green_channel);
blueArray = cat(3, blueArray, blue_channel);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment