How do you save the output of a processed video?
16 次查看(过去 30 天)
显示 更早的评论
I have been trying to save the output video of a tracked ball but cannot seem to figure how to use the VideoWriter correctly. How should I input the VideoWriter such that most if not all frames have labeled centroids in the final video? (The code plays a processed video, labeled centroid in Red) Again, would like to save the output video to something like '2-1-quicktime-Tracked.mov'. Sometimes I am able to write the file to the current folder by just inputting videoWriter=VideoWriter('2-1-quicktime-Tracked')but the video will not play or convert. Output in current directory appears as 2-1-quicktime.avi. The avi file type would be fine if it was readable.
Here is the code I am working with:
obj = VideoReader('2-1-quicktime.mov');
[hueChannelB,~,~] = rgb2hsv(read(obj, 25));
hsvI=imcomplement(hueChannelB);
hsvIG=mat2gray(hsvI);
BinaryHueChannel= im2bw(hsvIG,maps,0.9);
nframes = get(obj, 'NumberOfFrames');
I = read(obj, 1);
taggedBall = zeros([size(I,1) size(I,2) 3 nframes], class(I));
for k = 1 : nframes
singleFrame = read(obj, k);
% Convert to hsv then bw to do morphological processing.
[hueChannelB,~,~] = rgb2hsv(singleFrame);
hsvI=imcomplement(hueChannelB);
hsvIG=mat2gray(hsvI);
BinaryHueChannel= im2bw(hsvIG,maps,0.9);
% Get the area and centroid of each remaining object in the frame. The
% object with the largest area is the light-colored car. Create a copy
% of the original frame and tag the ball by changing the centroid pixel
% value to red.
taggedBall(:,:,:,k) = singleFrame;
stats = regionprops(BinaryHueChannel, {'Centroid','Area'});
if ~isempty([stats.Area])
areaArray = [stats.Area];
[junk,idx] = max(areaArray);
c = stats(idx).Centroid;
c = floor(fliplr(c));
width = 2;
row = c(1)-width:c(1)+width;
col = c(2)-width:c(2)+width;
taggedBall(row,col,1,k) = 255;
taggedBall(row,col,2,k) = 0;
taggedBall(row,col,3,k) = 0;
end
end
frameRate = get(obj,'FrameRate');
implay(taggedBall,frameRate);
end
Any advice is much appreciated! Thank you
0 个评论
采纳的回答
Anish Mitra
2016-2-23
You can create a VideoWriter object, before starting the for loop and use the 'open' command. Inside the loop, you will need to use the method 'writeVideo' and write the current frame with the centroid. Then close the file after the loop.
.
.
objWrite = VideoWriter('test');
open(objWrite);
for k = 1:nframes
.
.
.
writeVideo(objWrite, taggedBall(:,:,:,k));
end
close(objWrite);
By default it creates a .avi file, but you can choose which format to write the file in. You can also modify other properties such as FrameRate of the video that you will be creating.
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!