MATLAB Answers

how to create a video after an edge detection for loop

7 views (last 30 days)
Ben Timm
Ben Timm on 4 Jul 2020
Commented: Ben Timm on 9 Jul 2020 at 13:41
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!

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 4 Jul 2020
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.

  4 Comments

Show 1 older comment
Image Analyst
Image Analyst on 5 Jul 2020
You put this code OUTSIDE the loop where the video get created, so of course no video gets made:
% My Flourescent image det
for i = 1:n
I0 = read(v,i);
I1 = im2bw(I0,graythresh(I0)-0.03); % binarize image with treshold
I2 = bwareaopen(I1,50); % remove small regions (50 pixels)
I3 = edge(I2);
II = imfuse(I0,I3);
imshow(II,'initialmagnification','fit')
pause(0.1)
end
That needs to be inside the loop over frames of course. And since the whole thing was inside a try block, with no catch, it didn't show you the error that n was not defined. So put that inside the loop and see if you can get it working.
And you'll need to cast your binary image to an RGB uint8 image in the range 0-255.
See attached test1.m - it's closer to what you need.
Ben Timm
Ben Timm on 7 Jul 2020
Once again thanks for the help! I didn't realise the try block did that so the heads up was appreciated.
I ran your test.m file after reading through it and there were a couple of issues I found which I have seemingly sorted. That is until the most recent one where this error occurs:
"
Error using imread>get_full_filename (line 566)
File "/Desktop/Matlab Files/Videos/Movie Frames from VID00123/Frame0001.png" does not exist.
Error in imread (line 375)
fullname = get_full_filename(filename);
Error in test1 (line 238)
thisFrame = imread(outputFullFileName);
"
I noticed the produced avi was 0mb big despite the processing of edge detection working seemingly perfect. I figure the real problem is before line 238, possibly around 180 but after searching online and .doc'ing it, Ive come up with nothing as of yet..
Ben Timm
Ben Timm on 9 Jul 2020 at 13:41
I figured it out! For anyone that needs help with this in the future, please see the attached doc.
Thank you again for the assistance Image Analyst!
-Ben

Sign in to comment.

More Answers (0)


Translated by