You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to cut frames from video and then automatically crop the area of interest in all frames.
2 views (last 30 days)
Show older comments
Davit Hakobyan
on 28 May 2013
eventually build 3D plot with this images, with third dimension as a time evolution of images.
Accepted Answer
Image Analyst
on 28 May 2013
What is a 3D plot? Did you try VideoReader to extract frames, and just imcrop or normal indexing to extract the ROI?
35 Comments
Davit Hakobyan
on 28 May 2013
Edited: Davit Hakobyan
on 28 May 2013
I apologize but I am beginner in MATLAB,. More clearly I have video where some spiral like pattern is rotating (light and not light,fringes), so I would like to have frames with my interested area, and put this pictures in 3D plot like sequential images in one plot. The two dimensions are my image x and y the third video evolution (the frames, with respect to video time evolution).And I guess there is a function to nicely color where is light and where is not.
Davit Hakobyan
on 28 May 2013
Dear Image Analyst I am exploring your code http://www.mathworks.fr/matlabcentral/answers/48797-how-to-extract-frames-of-a-video which is very nice, but can you please tell me how to cut my area of interest and build with images 3D plot maybe ginput function will help, I am really new. Thanks
Image Analyst
on 29 May 2013
You're going to first have to segment out the spiral pattern so that you know what the top and bottom lines and left and right columns of the sprial "tip" is as it moves around all the frames. So let's say that over all the frame, the bounding box of the spiral never leaves the box [x, y, width, height] = [100, 200, 400 300]. So then you'd crop out that 300 row by 400 column box from each frame with imcrop:
croppedFrame = imcrop(fullSizedFrame, [100, 200, 400 300]);
imwrite(fullFileName, croppedFrame);
Repeat that for every frame as you go through the frames the second time. The first time you just scan the frames to find the spiral tip, then you find the overall bounding box, then you crop the overall bounding box from each frame and write to disk.
Davit Hakobyan
on 29 May 2013
Can you please provide me some code I am fighting with your code mentioned above to make use in my case, but I get errors. I am physicist and I will appreciate your help very much, here is what I think to do more clearly. I am selecting the area of interest some radius( light intensity as a function of x and y) in each frame, then I want to build the 3D plot same intensity x y and z as an time evolution of frames.
Image Analyst
on 29 May 2013
Upload your video somewhere where I can download it (i.e. NOT youtube).
Image Analyst
on 29 May 2013
Can you try another one:
Malicious Web Site Blocked
You attempted to access:
http://www.fileconvoy.com/dfl.php?id=gbdc976a9e26e9dce9992976133382e4632dec2a11
This is a known malicious web site. It is recommended that you do NOT visit this site. The detailed report explains the security risks on this site.
For your protection, this web site has been blocked. Visit Symantec to learn more about phishing and internet security.
Exit this site
Image Analyst
on 29 May 2013
OK I got it but I'm still not sure what you would like. Do you just want to crop the movie to a smaller rectangle? Do you want to make a volumetric image and do volume visualization on it like you can do with the very powerful avizo software? (If so, you're out of luck and can do cutaway views at best.) Or do you want something else? Like some kind of 2.5D surface like you'd get with surf() or waterfall()?
Davit Hakobyan
on 29 May 2013
Edited: Image Analyst
on 30 May 2013
Well the difficulty is that I am luck of time, deadline! The point is to show the content of this video in 3d (time as an z coordinate). some volume visualization. I thought to crop some cycle (not rectangle, have intensity as a function of x y in frames of video and call them to build 3D plot with time evolution. The main issue is to make the content of video, (pattern, its evolution in time), in one 3D volume plot. Easy understandable with simple glance.
Image Analyst
on 30 May 2013
Do you have some example that shows something similar? Was it like the capability Avizo provides? Because you can't get that with MATLAB. With Avizo you can do movies of true volume visualization. You can see internal things and surface things and have them be different colors and transparencies and have them fade in and out, have slicing planes (even ones that move through the volume as the volume spins) and all kinds of other powerful visualization techniques, that vast majority of which are not possible in MATLAB. With MATLAB you can do isosurfaces and cutaway views (slices) - that's about it.
Davit Hakobyan
on 30 May 2013
No I do not have to show. But it is not possible to do i said. to have matrix of all frames intensity of xy and then plot them in 3d with time or z
Davit Hakobyan
on 30 May 2013
Image Analyst
on 30 May 2013
Those are iso surface renderings. I don't see how your spinning spiral can be turned into an iso surface and look like anything other than a cylinder. You won't be able to see any internal structure of the spiral. Maybe someone else will help you.
Davit Hakobyan
on 30 May 2013
A bit sad. Can not even help me to crop frames get the matrix of frames and call them to build cylinder with color information in external surface.(not the picture)
Image Analyst
on 30 May 2013
All you have to do is to call imcrop() in the loop over frames and save the cropped image instead of the whole image. Do you need help on that? (Search Answers for rhinos.avi - I've posted the demo many times.) You just have to decide what your cropping rectangle is. Either specify it in terms of rows and columns, or from thresholding and getting the bounding box, but it you do it that way you have to process all frames first and get the overall bounding box.
Davit Hakobyan
on 30 May 2013
Actually I am fighting with your demo, it works with only for rhinos.avi. I want to call video , select 1 column in each frame, calculate meangreen and then plot as a function of frames Can you help please.
Davit Hakobyan
on 30 May 2013
In fact I need your code, but not mean gray whole image but a special ONE column in video. And with my video not rhinos
Davit Hakobyan
on 30 May 2013
It does not work when I am changing rhinos.avi to traffic.avi in same directory
Image Analyst
on 30 May 2013
OK it looks like you need the whole thing done for you. If I do that (no guarantees), I'd need your video, so upload it somewhere. But I'm really surprised that imcrop() if not working on your computer. Maybe you can try just a simple test with one still image to see if that works.
Davit Hakobyan
on 30 May 2013
Edited: Davit Hakobyan
on 30 May 2013
Imcrop is working , just a bit tricky to get one interested column . Everything step is working but not the whole thing together. I need your demo, It is just your same program with rhinos.avi. But mean green not whole frame but one specified cropped column in each frame. Please do it Any video with middle column say x at 500., except rhinos.
Image Analyst
on 30 May 2013
Until then, show me the few lines of code that you used to do the imcrop().
Davit Hakobyan
on 30 May 2013
clc;
close all;
clear all;
workspace;
Image = imread('example.jpg');
Column = imcrop(Image,[500 200 1 600]);
imshow(Image), figure, imshow(Column),
This way I defined my desired column from image
Davit Hakobyan
on 30 May 2013
Now with video, and then plot, Mean green of this column in each frame onto plot.
Image Analyst
on 30 May 2013
So you're cropping out just a single column from your frame and saving all those columns out to disk as separate images? Maybe I can try to do this cropping with your video and figure out why it doesn't work, but I doubt that I'll have any luck because I'm pretty certain imcrop will work when I use it.
Davit Hakobyan
on 30 May 2013
Sir,
I am repeating, I need to build up exactly your demo program with rhinos.avi. Difference is that I want to plot not mean of each frame(picture) but cropped column. The point was to get some help cause I am beginner at Matlab and in TIME pressure, so it takes huge time to do what I need. That is why I asked your help, but it seems I have to do by my own... And this was my first question here .....
Davit Hakobyan
on 30 May 2013
I am asking only for code, of your program, less complicated. In fact if i could have change in your code the rhinos.avi to my video i am done, cause I have wrote cropping part.
Image Analyst
on 31 May 2013
Edited: Image Analyst
on 31 May 2013
I was going to try to help you again tonight, but you removed your post with the link to your video, so I can't. Why can't you just change the name of the input video from rhinos.avi to whatever your video is called?
Image Analyst
on 31 May 2013
OK, here - I've done what you described (almost). I take each frame, extract a column from the green channel, then take the mean intensity of that extracted column. I plot both the mean value, and the extracted lines themselves as a function of frame number. All that's left for you to do is to change the filename from rhinos.avi to whatever your video is called.
% Demo macro to extract frames and get frame means from an avi movie
% Also computes the mean gray value of a column of the green color channel.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Open the rhino.avi demo movie that ships with MATLAB.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
movieFullFileName = fullfile(folder, 'rhinos.avi');
% Check to see that it exists.
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
try
videoObject = VideoReader(movieFullFileName)
% Determine how many frames there are.
numberOfFrames = videoObject.NumberOfFrames;
vidHeight = videoObject.Height;
vidWidth = videoObject.Width;
numberOfFramesWritten = 0;
% Prepare a figure to show the images in the upper half of the screen.
figure;
% screenSize = get(0, 'ScreenSize');
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Loop through the movie, writing all frames out.
% Each frame will be in a separate file with unique name.
meanGrayLevels = zeros(numberOfFrames, 1);
newVideoHeight = min([799, vidHeight]) - 199;
extractedImage = zeros(newVideoHeight, numberOfFrames, 'uint8');
for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisFrame = read(videoObject, frame);
% Display it
hImage = subplot(2, 2, 1);
image(thisFrame);
caption = sprintf('Frame %4d of %d.', frame, numberOfFrames);
title(caption, 'FontSize', fontSize);
drawnow; % Force it to refresh the window.
% Extract line 500 from the greenChannel
% In essence crop this rectangle: [500 200 1 600]);
row1 = 200;
row2 = min([799, vidHeight]);
columnToExtract = 500;
if columnToExtract > vidWidth
% Move to middle column if column is outside of image boundaries.
columnToExtract = floor(vidWidth/2);
end
greenLine = thisFrame(row1:row2, columnToExtract, 2); % 2 means green
% Store it in our new image we're building of the green lines.
extractedImage(:, frame) = greenLine;
subplot(2, 2, 3);
cla;
imshow(extractedImage);
title('Column 500 of the Green Channel', 'FontSize', fontSize);
xlabel('Frame Number');
ylabel('Row Number');
axis on;
drawnow;
% Calculate the mean gray level.
meanGrayLevels(frame) = mean(greenLine(:));
% Plot the mean gray levels.
hPlot = subplot(2, 2, 2);
hold off;
plot(meanGrayLevels, 'k-', 'LineWidth', 2);
grid on;
% Put title back because plot() erases the existing title.
caption = sprintf('Mean Gray Levels of column %d of the Green Channel.', columnToExtract);
title(caption, 'FontSize', fontSize);
if frame == 1
xlabel('Frame Number');
ylabel('Gray Level');
% Get size data later for preallocation if we read
% the movie back in from disk.
[rows, columns, numberOfColorChannels] = size(thisFrame);
end
% Update user with the progress. Display in the command window.
progressIndication = sprintf('Processed frame %4d of %d.', frame, numberOfFrames);
disp(progressIndication);
% Increment frame count (should eventually = numberOfFrames
% unless an error happens).
numberOfFramesWritten = numberOfFramesWritten + 1;
end
% Alert user that we're done.
finishedMessage = sprintf('Done! It processed %d frames of\n"%s"', numberOfFramesWritten, movieFullFileName);
disp(finishedMessage); % Write to command window.
uiwait(msgbox(finishedMessage)); % Also pop up a message box.
catch ME
% Some error happened if you get here.
stError = lasterror;
strErrorMessage = sprintf('Error extracting movie frames from:\n\n%s\n\nError: %s\n\n)', movieFullFileName, stError.message);
uiwait(msgbox(strErrorMessage));
end
Davit Hakobyan
on 31 May 2013
Thank you very much bu again Error extracting rhinos.avi or other video.
Image Analyst
on 31 May 2013
rhinos.avi is a standard demo video that has shipped with MATLAB's image processing toolbox for many years. What error do you have with it?
Davit Hakobyan
on 31 May 2013
I have fix it, merci Monsieur. Now I want to take some rectangle not column and also average, through the row, then build Row number as a function of frame number plot
Davit Hakobyan
on 31 May 2013
Could you please tell me how to extract rectangle (let say instead of one column rectangle with width 10) average green in each extracted frame through the row and then plot same row number vs frame number plot.
Image Analyst
on 31 May 2013
Isn't that what I did when I said this in my demo:
greenLine = thisFrame(row1:row2, columnToExtract, 2); % 2 means green
Of course you can make the rows and columns whatever you want. I just did one column at first because that's what you asked for at that time, but you can get any rectangle like this:
greenLine = thisFrame(row1:row2, col1:col2, 2);
You just figure out what rows and columns you want and that will get that rectangle from the green channel of the image.
I don't understand what "in each extracted frame through the row" means.
Nor do I understand "plot same row number vs frame number plot." means. If you're changing the row number as a function of frame number, of course you can keep track of that just like I kept track of the mean gray level as a function of frame number, and you can plot it just like I did.
Davit Hakobyan
on 31 May 2013
You see your plot "Column 500 of green channel" is exactly what I need. But instead, to have column of 490-510 and the values of the green in each row of this frames(matrix\ractangle) averaged, then to plot it versus framerate
Image Analyst
on 31 May 2013
Yep, so you just did
greenLine = thisFrame(row1:row2, 490:510, 2);
and it all worked for you. Glad I could help. We're all done now, right?
Davit Hakobyan
on 1 Jun 2013
Yes I have wrote one bye myself and adjusted your demo, now I am done with this and have to codes. Now I need indexed color surfaces. Urgent I am on it, if you know how to find stuff like that tell me.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)