Setting CV_CAP_PROP_POS_FRAMES to 0 fails when VideoCapture is used to read a single image (Bug #1892)


Added by Julien Hirel over 5 years ago. Updated over 2 years ago.


Status:Cancelled Start date:2012-05-04
Priority:Normal Due date:
Assignee:- % Done:

0%

Category:highgui-video
Target version:Next Hackathon
Affected version:2.4.0 - 2.4.8 Operating System:Any
Difficulty: HW Platform:Any
Pull request:

Description

Hi,

I'm trying to use VideoCapture to indifferently read single images or videos (the user provides the path to a file which can be an image or video) in a loop. I have had trouble with reading an image and have managed to narrow the problem down to a simple program. It seems that the problems comes from setting CV_CAP_PROP_POS_FRAMES to 0 when there is only one frame (because this actually works when I'm working with videos).

Here is the code:

 1int main(int ac, char *av[])
 2{
 3   if (ac <= 1)
 4   {
 5      std::cerr << "The name of the video file must be supplied as a parameter" << std::endl;
 6      exit(1);
 7   }
 8
 9   cv::VideoCapture video;
10
11   if (video.open(av[1]) == false)
12   {
13      std::cerr << "The video file (" << av[1] << ")could not be opened" << std::endl;
14      exit(1);
15   }
16
17   std::cout << "CV_CAP_PROP_FRAME_COUNT = " << video.get(CV_CAP_PROP_FRAME_COUNT)
18             << std::endl;
19
20   cv::namedWindow("Display");
21
22   cv::Mat videoFrame;
23
24   std::cout << "(1)CV_CAP_PROP_POS_FRAMES = " << video.get(CV_CAP_PROP_POS_FRAMES)
25             << std::endl;
26
27   // Rewind VideoCapture
28   std::cout << "Rewinding VideoCapture" << std::endl;
29   if (video.set(CV_CAP_PROP_POS_FRAMES, 0) == false)
30   {
31      std::cerr << "The video could not be rewinded" << std::endl;
32      exit(1);
33   }
34
35   std::cout << "(2)CV_CAP_PROP_POS_FRAMES = " << video.get(CV_CAP_PROP_POS_FRAMES)
36             << std::endl;
37
38   if (video.read(videoFrame) == true)
39   {
40      std::cout << "(3)CV_CAP_PROP_POS_FRAMES = " << video.get(CV_CAP_PROP_POS_FRAMES)
41                << std::endl;
42
43      std::cout << "Displaying image" << std::endl;
44      cv::imshow("Display", videoFrame);
45      cv::waitKey(0);
46
47      return 0;
48   }
49   else
50   {
51      std::cout << "(3)CV_CAP_PROP_POS_FRAMES = " << video.get(CV_CAP_PROP_POS_FRAMES)
52                << std::endl;
53      std::cout << "Read failed" << std::endl;
54      return 1;
55   }
56}

If I run this program on a png image, I get the following output :

CV_CAP_PROP_FRAME_COUNT = 1
(1)CV_CAP_PROP_POS_FRAMES = 0
Rewinding VideoCapture
(2)CV_CAP_PROP_POS_FRAMES = 0
(3)CV_CAP_PROP_POS_FRAMES = 0
Read failed

But if I comment video.set(CV_CAP_PROP_POS_FRAMES, 0) then I get:

CV_CAP_PROP_FRAME_COUNT = 1
(1)CV_CAP_PROP_POS_FRAMES = 0
(2)CV_CAP_PROP_POS_FRAMES = 0
(3)CV_CAP_PROP_POS_FRAMES = 1
Displaying image

I'm running OpenCV 2.4.0 (the release tar) on ubuntu 12.04 amd64, with ffmpeg 0.8.1 installed.


cv_videocapture_image.cpp (1.5 kB) Julien Hirel, 2012-05-04 05:22 pm

opencv-logo2.png (1.4 kB) Julien Hirel, 2012-05-04 06:42 pm

cvvideocapture.cpp (1.5 kB) Julien Hirel, 2012-05-07 06:43 pm


Associated revisions

Revision 3ec43a11
Added by Alexander Reshetnikov over 5 years ago

Fixed read error in VideoCapture for images (bug #1892); minor fix of ffmpeg wrapper warning

History

Updated by Alexander Reshetnikov over 5 years ago

Julien, could you attach a video used in your code (now I cannot reproduce this situation)?

  • Target version set to 2.4.1

Updated by Julien Hirel over 5 years ago

Well, the problem occurs when I'm trying to read an image (any image actually).
I just tested with the opencv logo, which I am attaching, and I have the same problem.

My OpenCV is compiled with TBB and IPP enabled, if that makes a difference.

  • File opencv-logo2.png added

Updated by Alexander Reshetnikov over 5 years ago

Julien, I've fixed it. The reason was in seek method of VideoCapture. It was changed in r8314.

  • Status changed from Open to Done

Updated by Julien Hirel over 5 years ago

Thanks for the quick reply and fix Alexander.

Using r8314 fixed the problem for the code I supplied. However I am now trying to go back to frame 0 repeatedly with a VideoCapture reading a single image. It seems that your fix only works for the first call to set(CV_CAP_PROP_POS_FRAMES, 0).

I have attached a new program were I try to display the image in a loop, rewinding the VideoCapture after each failed read. It correctly reads the image the first time but fails to read it in successive iterations.

Here is the output of the program:

Rewinding video ../media/Neutral.jpg
Frames displayed : 1
Rewinding video ../media/Neutral.jpg
Frames displayed : 0
Rewinding video ../media/Neutral.jpg
Frames displayed : 0
Rewinding video ../media/Neutral.jpg
Frames displayed : 0

  • File cvvideocapture.cpp added

Updated by Andrey Kamaev over 5 years ago

  • Status changed from Done to Open

Updated by Andrey Kamaev over 5 years ago

  • Target version deleted (2.4.1)

Updated by Andrey Kamaev about 5 years ago

  • Category changed from highgui-images to highgui-video

Updated by Vadim Pisarevsky almost 5 years ago

  • Target version deleted ()
  • Assignee deleted (Alexander Reshetnikov)

Updated by Kirill Kornyakov almost 5 years ago

  • Target version set to Next Hackathon

Updated by Vadim Pisarevsky over 2 years ago

let's cancel very old bugs; if they are still valid, please, reopen

  • Status changed from Open to Cancelled
  • HW Platform set to Any
  • Operating System set to Any
  • Affected version set to 2.4.0 - 2.4.8

Also available in: Atom PDF