Note that there are some explanatory texts on larger screens.

plurals
  1. POPython OpenCV 2.4 writes half-complete PNG video frames
    primarykey
    data
    text
    <p>I just installed OpenCV 2.4 from source on Ubuntu 12.04. I'm trying to use a Python script to write the first frame of a video to a PNG image, but I'm getting some bizarre results. Here's the code:</p> <pre><code>import numpy as np import cv import cv2 import sys video = cv.CaptureFromFile(sys.argv[1]) frame = cv.QueryFrame(video) proxy = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, proxy, cv.CV_BGR2GRAY) a = np.asarray(cv.GetMat(proxy)) cv2.imwrite('image.png', a) </code></pre> <p>Problem is, the image comes out looking like this:</p> <p><img src="https://i.stack.imgur.com/4q4l7.png" alt="half complete something or other"></p> <p>These are AVI files and otherwise seem to be fine. Any ideas?</p> <p><b>Edit #1:</b> Apologies, here is the ffmpeg version information:</p> <pre><code>ffmpeg version 0.10.2-4:0.10.2-0ubuntu0jon1 built on Mar 18 2012 09:59:38 with gcc 4.6.3 configuration: --extra-version='4:0.10.2-0ubuntu0jon1' --arch=amd64 --prefix=/usr --libdir=/usr/lib/x86_64-linux-gnu --disable-stripping --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --enable-libvpx --enable-runtime-cpudetect --enable-libfreetype --enable-vaapi --enable-frei0r --enable-gpl --enable-postproc --enable-x11grab --enable-librtmp --enable-libvo-aacenc --enable-version3 --enable-libvo-amrwbenc --enable-version3 --enable-libdc1394 --shlibdir=/usr/lib/x86_64-linux-gnu --enable-shared --disable-static libavutil 51. 35.100 / 51. 35.100 libavcodec 53. 61.100 / 53. 61.100 libavformat 53. 32.100 / 53. 32.100 libavdevice 53. 4.100 / 53. 4.100 libavfilter 2. 61.100 / 2. 61.100 libswscale 2. 1.100 / 2. 1.100 libswresample 0. 6.100 / 0. 6.100 libpostproc 52. 0.100 / 52. 0.100 </code></pre> <p><b>Edit #2:</b> In my own troubleshooting, I upgraded ffmpeg from the default 12.04 ubuntu version to the one you see in Edit #1 above. This seems to have changed things a little bit: the video which generated the frame in this question now seems to work fine, but larger videos still present with corrupted bottom halves (or bottom thirds, or fourths). Even larger videos actually segfault entirely. I'm not really sure what to make of this, other than--yet again--faulty or missing codecs. It segfaults right on the <code>QueryFrame</code> step.</p> <p><b>Edit #3:</b> I changed the code to make exclusive use of the cv2 interface (as per a link in one of the comments below). Now, <code>video.retrieve()</code> always returns <code>False</code> and no image is written.</p> <p><b>Edit #4:</b> I ran the following command on the video before using the new cv2 interface to read the video frames:</p> <pre><code>ffmpeg -sameq -i normal.avi p_normal.avi </code></pre> <p>The output of the command looked ok except for this one line following the initialization of ffmpeg and its description of the input:</p> <blockquote> <p>Incompatible pixel format 'pal8' for codec 'mpeg4', auto-selecting format 'yuv420p'</p> </blockquote> <p>Here's the full output of the command:</p> <pre><code>Input #0, avi, from 'normal.avi': Duration: 00:01:37.60, start: 0.000000, bitrate: 1312 kb/s Stream #0:0: Video: rawvideo, pal8, 128x256, 5 tbr, 5 tbn, 5 tbc Incompatible pixel format 'pal8' for codec 'mpeg4', auto-selecting format 'yuv420p' [buffer @ 0x11a0f80] w:128 h:256 pixfmt:pal8 tb:1/1000000 sar:0/1 sws_param: [buffersink @ 0x11a1380] auto-inserting filter 'auto-inserted scale 0' between the filter 'src' and the filter 'out' [scale @ 0x1197da0] w:128 h:256 fmt:pal8 -&gt; w:128 h:256 fmt:yuv420p flags:0x4 Output #0, avi, to 'p_normal.avi': Metadata: ISFT : Lavf53.32.100 Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 128x256, q=2-31, 200 kb/s, 5 tbn, 5 tbc Stream mapping: Stream #0:0 -&gt; #0:0 (rawvideo -&gt; mpeg4) Press [q] to stop, [?] for help frame= 488 fps= 0 q=0.0 Lsize= 1497kB time=00:01:37.60 bitrate= 125.6kbits/s video:1480kB audio:0kB global headers:0kB muxing overhead 1.165352% </code></pre> <p>Most importantly, the Python OpenCV code to read the frame (using the cv2 interface) still returns False (same behavior as before).</p> <p><b>Edit #5:</b> I have so far followed the instructions <a href="https://ffmpeg.org/trac/ffmpeg/wiki/UbuntuCompilationGuide" rel="nofollow noreferrer">found here</a> for installing ffmpeg and its dependencies from source, and that went smoothly. Without reinstalling OpenCV from source, I am still encountering the same problem as before where <code>video.retrieve()</code> returns False. In attempting to recompile OpenCV 2.4 from source, I am getting the following error during compilation:</p> <pre><code>Linking CXX shared library ../../lib/libopencv_highgui.so /usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32S against `av_destruct_packet' can not be used when making a shared object; recompile with -fPIC /usr/local/lib/libavcodec.a: could not read symbols: Bad value </code></pre> <p>If I recompile x264, libvpx, and ffmpeg with the <code>--enable-pic</code> flag, OpenCV compilation still fails, this time with (<code>kdbwin.o</code>, <code>.rodata</code>) instead of (<code>avpacket.o</code>, <code>av_destruct_packet</code>) respectively in the snippet above.</p> <p><b>Edit #6:</b> Fixed the above error by adding <code>--enable-shared</code> to the configuration options of libvpx and ffmpeg. OpenCV recompiled and built successfully, and ffmpeg worked just fine. Sadly, after running the previous command (<code>ffmpeg -sameq -i normal.avi p_normal.avi</code>), my script <i>still</i> couldn't retrieve any frames; the flag returned was still False. Any further ideas?</p> <p><b>Edit #7:</b> Here's the latest script I'm using.</p> <pre><code>import numpy as np import cv2 import sys video = cv2.VideoCapture(sys.argv[1]) flag, frame = video.retrieve() if not flag: print 'Error' quit() proxy = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imwrite('image.png', proxy) </code></pre> <p><b>Edit #8:</b> Got it! Here's what the code should be:</p> <pre><code>import numpy as np import cv2 import sys video = cv2.VideoCapture(sys.argv[1]) if video.grab(): flag, frame = video.retrieve() if not flag: print 'Error' quit() proxy = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imwrite('image.png', proxy) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload