MATLAB: How to create a video after an edge detection for loop

.avi creationedge detectionvideovideo processing

Hi all!
Hopefully you can help, I'm having trouble getting this code to work. My aim is to apply edge detection to a video of cells (this part works and I am happy with it) and then have the frames of the resulting edge detection become a new .avi video in itself (that I can play just like the previous .avi file). I have tried a number of methods outside the for loop I created for the edge detection but none of it seems to work. The code is below:
function Vid_Edge_Detection_flourescent
v = VideoReader('VID00126.AVI');
n = v.NumFrames;
for i = 1:n
I0 = read(v,i);
I1 = im2bw(I0,graythresh(I0)-0.03);
I2 = bwareaopen(I1,50);
I3 = edge(I2);
II = imfuse(I0,I3);
imshow(II,'initialmagnification','fit')
pause(0.1)
end
open(v);
w = VideoWriter('Flourescent00126.AVI');
for k = 1:20
surf(sin(2*pi*k/20)*Z,Z)
frame = getframe(gcf);
writeVideo(v,frame);
end
close(v);
If anyone can help that would be amazing! I cant share the .avi as it is above 5mb but the general principle goes. Id like to have a new resulting avi file that the edge detection function has changed. Thanks in advance!

Best Answer

You're not writing the edge image out, either to a saved image file or the video. See my attached demos -- they show you how to do it. In particular the movie_Canny makes a movie of the edges.