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; endend frameRate = get(obj,'FrameRate');implay(taggedBall,frameRate); end
Any advice is much appreciated! Thank you
Best Answer