Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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.
close all;
imtool close all;
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);
videoObject = VideoReader(movieFullFileName)
numberOfFrames = videoObject.NumberOfFrames;
vidHeight = videoObject.Height;
vidWidth = videoObject.Width;
numberOfFramesWritten = 0;
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')
writeToDisk = false;
for frame = 1 : numberOfFrames
thisFrame = read(videoObject, frame);
hImage = subplot(2, 2, 1);
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');
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,[]);
Can anyone explain what the exact problem is?
Is there any other approach that I can adopt?
Thanks a lot :)

Respuesta aceptada

Image Analyst
Image Analyst el 2 de Nov. de 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 comentario
meri mak
meri mak el 2 de Nov. de 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.

Iniciar sesión para comentar.

Más respuestas (0)


Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by