Extraction of images from video: why images are missed?

INTRODUCTION:
I have a avi-video recorded at 2 frames per second and I used the following commands to extract the images of the video:
obj = VideoReader('C:\Desktop\test.avi');
vid = read(obj,[1 5000]);
for k = 1:1:5000
newname = strcat('C:\Desktop\TEST\',num2str(k),'.png');
imwrite(vid(:,:,:,k), newname);
end;
PROBLEM:
I controlled the extracted images and realized that one image is missed at every sixth image or 20 images per minutes. Instead of counting 120 images per minute I counted only 100.
I wonder if someone knows what is going wrong with the commands and what I can do to solve this problem.
Thank you in advance Emerson

 採用された回答

Image Analyst
Image Analyst 2013 年 3 月 13 日

0 投票

Add k to your loop to display it:
for k = 1:5000
k % No semicolon.
newname = strcat('C:\Desktop\TEST\',num2str(k),'.png');
imwrite(vid(:,:,:,k), newname);
end
Now, do you see any missing k in the list printed to the command line?

10 件のコメント

Emerson De Souza
Emerson De Souza 2013 年 3 月 13 日
Image Analyst, thank you for answering.
However, the problem is not any missing k.
The problem is, Matlab skips images and the advanced imaged is saved with the old k. To illustrate this, imagine you record a video of a digital clock with 1 image per second and assume the clock and the video are perfectly synchronized. In this way image 1 corresponds to second 1, image 2 corresponds to second 2, and so on perfectly without problem.
I did the experiment above and controlled the extraction using Virtualdub as alternative method for image extraction. I verified that each image really corresponds to a new second. The result from Matlab extraction is displayed in the table below:
MATLAB REAL
1 1
2 2
3 3
4 4
5 5
6 6
7 8 !!!
8 9
9 10
10 11
11 12
12 13
13 15 !!!
14 16
15 17
16 18
17 19
18 21 !!!
19 22
20 23
21 24
22 25
23 26
24 28 !!!
25 29
26 30
27 31
28 32
29 34 !!!
The column labeled Matlab displays the running index k while the column labeled Real displays what has really been extracted. We see that for k=7 it extracted image 8, it jumped the second 7, then it keeps consistent and for k=13 it skips image 14 and save 15, and so on...
I don't know what is generating this error and the semicolon was not the problem. I hope this explanation made clear what the problem is.
Again, thanks for trying to help
Emerson
Image Analyst
Image Analyst 2013 年 3 月 13 日
What's VirtualDub? Is that freeware we can try so we can compare it ourselves (preferably with your video file)?
Emerson De Souza
Emerson De Souza 2013 年 3 月 15 日
Hi, I continued to search the reason for the missing images during extraction using Matlab and did the following:
1) I used CamStudio (<http://camstudio.org/>) to recorded a digital clock at 1 frame per second for 10 minutes and saved as file1.avi
2) I used VirtualDub (<http://www.virtualdub.org/>) to extract the frames and verified that each frame corresponds to a new second and in CORRECT sequence (NO JUMP)
3) I used Matlab to extract the frames and observed irregular jumps (not always at every 6 or 7 frames).
4) I tried to use ImageJ (<http://rsb.info.nih.gov/ij/download.html>) to extract the frames but could not load the video due to unsupported compression!!!
5) Then I used ImageJ and loaded the folder with the extracted images via VirtualDub and saved as file2.avi
6) I run Matlab to extract the video file2.avi and it worked surprisingly well. Each frame corresponds to a new second and in CORRECT sequence (NO JUMP)!!!
It seems that the file1.avi generated via CamStudio is a file that: 1) VirtualDub has no problem to read and extract the files, 2) Matlab can load but does not extract the files correctly, 3) ImageJ can not even load but any file generated via ImageJ Matlab can process well.
I'm not a video specialist, but unsupported compression is the key for the errors in Matlab.
Image Analyst
Image Analyst 2013 年 3 月 15 日
Seems weird that it would extract any frames at all if the compression were not supported. How could it get some but not others?
Emerson De Souza
Emerson De Souza 2013 年 3 月 15 日
I extracted all images with VirtualDub and saved them into a folder.
With ImageJ I can load the folder with all images and make a video out of it. This video was run in Matlab for extraction of images and the result was correct.
Now, if I don't extract the images from the original video with VirtualDub and try to directly open with ImageJ, then I get the error unsupported compression and it does not open at all. Matlab can read the original video but does not extract the images correctly OR may be it could not read the video correctly.
Image Analyst
Image Analyst 2013 年 3 月 15 日
Like I said, I can take a look at it if you want to upload it somewhere. Otherwise submit it to the Mathworks and someone will check it out.
Emerson De Souza
Emerson De Souza 2013 年 3 月 15 日
Would Dropbox work for you to upload the video? Or where would you suggest to upload?
Image Analyst
Image Analyst 2013 年 3 月 15 日
編集済み: Image Analyst 2013 年 3 月 15 日
I'm at home now so I can get it. I can't access dropbox.com from work since it's banned there.
Emerson De Souza
Emerson De Souza 2013 年 4 月 5 日
I did further similars experiments as commented above and it turned out that the problem occurs always when the amount of images are larger than 10000, INDEPENDENT on the the size of the video. ONLY THE AMOUNT OF IMAGES MATTERS. You can try any small video build with small frames, say 20x30 pixels, but the amount of frames > 10000 frames, then again the problem will occur. Anyways, since I have many videos with more than 60000 frames,I will keep using VirtualDub to extract the images. If you still wish to have a look, I can upload it for you via Dropbox. Thanks again for participating in this discussion.
Emerson
AJ
AJ 2013 年 6 月 13 日
Hi Image Analyst, I have a problem , when I am using your code to extract individual frames from a video, I am only able to extract 80 frames from the total number of frames. Kindly help me.

サインインしてコメントする。

その他の回答 (2 件)

Shaun VanWeelden
Shaun VanWeelden 2013 年 3 月 13 日

0 投票

Also, I would do obj.FrameRate to see if it really is 2, this seems much more likely than a number not showing up in a vector.

1 件のコメント

Emerson De Souza
Emerson De Souza 2013 年 3 月 13 日
Thank you Shaun,
I double checked the frame rate and it is correct 2. I recorded videos with different frame rate and the problem as described above persists. It seems that there is no relationship between frame rate and not writing the image in correct sequence during extraction. Thanks anyways.
Emerson

サインインしてコメントする。

Akhilkumar1307
Akhilkumar1307 2017 年 3 月 8 日

0 投票

Hi
I am facing similar problem too
My video frame rate is 30 fps, so it should show me a frame every 33(1000/30) millisecond but its not Its showing around a frame every 400 millisecond
please help

1 件のコメント

Akhilkumar1307
Akhilkumar1307 2017 年 3 月 20 日
Problem is solved
Previously am using .avi files, and know its fine with .mp4 files

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by