Clear Filters
Clear Filters

How do I determine RGB values for a single column of an image?

2 views (last 30 days)
I have a video with over 2000 frames (1080x1920x3) and I want to determine the RGB values of all the frames at column 600.
I need to have 3 matrix with size [1080(rows), 2000(numberofFrames)] for red , green, and blue.
The following is my code. The matrix dimensions do not match and I don't know how to resolve it.
clc;
close all;
imtool close all;
clear;
workspace;
fontSize = 22;
folder = fileparts(which('pan-head.mp4'));
movieFullFileName = fullfile(folder, 'pan-head.mp4');
if ~exist(movieFullFileName, 'file')
strErrorMessage = sprintf('File not found:\n%s\nYou can choose a new one, or cancel', movieFullFileName);
response = questdlg(strErrorMessage, 'File not found', 'OK - choose a new movie.', 'Cancel', 'OK - choose a new movie.');
if strcmpi(response, 'OK - choose a new movie.')
[baseFileName, folderName, FilterIndex] = uigetfile('*.avi');
if ~isequal(baseFileName, 0)
movieFullFileName = fullfile(folderName, baseFileName);
else
return;
end
else
return;
end
end
videoObject = VideoReader(movieFullFileName)
numberOfFrames = videoObject.NumberOfFrames;
vidHeight = videoObject.Height;
vidWidth = videoObject.Width;
numberOfFramesWritten = 0;
figure;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
promptMessage = sprintf('Do you want to save the individual frames out to individual disk files?');
button = questdlg(promptMessage, 'Save individual frames?', 'Yes', 'No', 'Yes');
if strcmp(button, 'Yes')
writeToDisk = true;
[folder, baseFileName, extentions] = fileparts(movieFullFileName);
folder = pwd; % Make it a subfolder of the folder where this m-file lives.
outputFolder = sprintf('%s/Movie Frames from %s', folder, baseFileName);
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
else
writeToDisk = false;
end
for frame = 1 : numberOfFrames
thisFrame = read(videoObject, frame);
hImage = subplot(2, 2, 1);
image(thisFrame);
caption = sprintf('Frame %4d of %d.', frame, numberOfFrames);
title(caption, 'FontSize', fontSize);
drawnow; %refresh the window.
% Write the image array to the output file
%if writeToDisk
% Construct an output image file name.
outputBaseFileName = sprintf('Frame %4.4d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
frameWithText = getframe(gca);
imwrite(frameWithText.cdata, outputFullFileName, 'png');
end
m = [vidHeight, numberOfFrames, 3]
for i = 1:1:numberOfFrames;
red(i) = reshape(m(:,600,1),1,[]);
green(i) = reshape(m(:,600,1),1,[]);
blue (i) = reshape(m(:,600,1),1,[]);
end
Can anyone explain what the exact problem is?
Is there any other approach that I can adopt?
Thanks a lot :)

Accepted Answer

Image Analyst
Image Analyst on 2 Nov 2015
Before the loop, you preallocate your R, G, and B arrays
redColumn600s = zeros(vidHeight, numberOfFrames, 'uint8');
greenColumn600s = zeros(vidHeight, numberOfFrames, 'uint8');
blueColumn600s = zeros(vidHeight, numberOfFrames, 'uint8');
You get your single frame from this:
thisFrame = read(videoObject, frame);
so now you need to extract column 600. To get the 3 colors separately, you'd do this:
column600R = thisFrame(:, 600, 1); % Extract red channel column 600.
column600G = thisFrame(:, 600, 2); % Extract green channel column 600.
column600B = thisFrame(:, 600, 3); % Extract blue channel column 600.
Then stuff those into your cumulative arrays
redColumn600s(:, frame) = column600R;
greenColumn600s(:, frame) = column600G;
blueColumn600s(:, frame) = column600B;
(You could combine the last two code chunks if you want to eliminate the temporary vector variables.)
  1 Comment
meri mak
meri mak on 2 Nov 2015
thank you so much. I've got the results and I checked some of the random pixel's RGB values and they are totally valid.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!