3

I have a series of images in *.tif format that I want to use to create a video. I am using OpenCV 3.1.0 in Python 2.7. Below is a snippet of my code:

import cv2
import numpy as np

nIMAGES = 10
files = glob.glob(DIR + '\\' + tpname +'\\*.tif' )
image_stack = np.empty((500, 220, nIMAGES))
mov = DIR + '\\' + tpname + '\\' + tpname + '_mov.avi'
MOV = cv2.VideoWriter(filename=mov, fourcc=cv2.VideoWriter_fourcc('F', 'M', 'P', '4'), fps=2, frameSize=(220, 500)) # frame size is 220 x 500

for i in np.arange(0, nIMAGES):
    print 'Working on: ' + files[i][-14:-4]
    image = cv2.imread(files[i], 0)
    crop_image = image[50:550, 252:472] #crop y:h, x:w

 # now let's create the movie:
    crop_image = cv2.applyColorMap(crop_image, cv2.COLORMAP_JET)
    MOV.write(crop_image)

MOV.release()

When I run this code, I create an AVI file that is 0 Kb (it hasn't saved anything to it).

I believe I am missing something like frame = cv2.VideoCapture.read(crop_image) in which case I would convert the write line to MOV.write(frame). However, I get an AttributeError in that the VideoCapture.read is not an attribute.

I am using this OpenCV webpage as my guide: http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html

Warren Lamont
  • 185
  • 4
  • 12
  • does it print all the "working on ..." files? In C++ API you would have to add the opencv_ffmpeg.dll to successfully write frames. – Micka Feb 22 '16 at 21:36
  • Yes. It does print the `Working on:` message. It does read the images in and it does crop them appropriately. I can display these images with `cv2.imshow` – Warren Lamont Feb 23 '16 at 10:42
  • 1
    please have a look at http://stackoverflow.com/questions/11699298/opencv-2-4-videocapture-not-working-on-windows . Same should be the case for VideoWriter, so add the opencv_ffmpeg dll to a location where your program can find it! – Micka Feb 23 '16 at 10:46
  • wasn't the file already created in your old version? `When I run this code, I create an AVI file that is 0 Kb` ? I'm quite sure that the right dll file isn't found yet... – Micka Feb 23 '16 at 14:38
  • 1
    OR: the codec isn't supported by your ffmpeg dll. See http://answers.opencv.org/question/12959/videowriter-fourcc-1-works-but-cv_fourcccram-does-not/ and try a different codec. For example start with the 'MJPG' codec. If that works you can try different codecs. – Micka Feb 23 '16 at 14:42
  • Ok. So it now creates a video. But it only has the first image of the sequence. To recap: I copied all the contents of `ffmpeg` to `C:\Anaconda\ ` (since I am using Spyder and Anaconda). I changed the names of the `.dll` files to: `opencv_ffmpeg310.dll` and `opencv_ffmpeg310_64.dll`. It now creates a video file `*.avi` that is 100 kb (the size of one image). – Warren Lamont Feb 23 '16 at 14:43
  • is the size of all your images `(220, 500)`? If not you'll have to resize them programmatically (cv2.resize), they must fit the Recorder settings. In addition check that color/grayscale is set correctly and fitting to your images, or convert the images programmatically. – Micka Feb 23 '16 at 14:43
  • I got it to work! @Micka you are the man! I need to change the codec to `fourcc=cv2.VideoWriter_fourcc('M','J','P','G')`. It now works beautifully. – Warren Lamont Feb 23 '16 at 14:47
  • 1
    nice to hear that. But be aware that MJPEG isn't a great video codec, so if you get problems in video quality and video size you might want to find different codecs. – Micka Feb 23 '16 at 14:49

1 Answers1

1

I had to make two changes to get this to work:

  1. Following the advice on this question: OpenCV 2.4 VideoCapture not working on Windows I had to copy the fmpeg.dll files to my Python directory (C:\Anaconda). I also had to relabel the folders to include the version of my opencv (3.1.0) e.g. opencv_ffmpeg310_64.dll
  2. The other change I needed to make was to change my codec to MJPG

Much appreciation goes to @Micka for helping me very quickly.

Community
  • 1
  • 1
Warren Lamont
  • 185
  • 4
  • 12