Ask Your Question
1

Canny edge detection applying to video, but converted video is not saving

asked 2015-01-21 00:40:52 -0600

Thondok gravatar image

Hi,

The purpose of this code is to find a RAW video, run a canny edge detection conversion on it, and then save the converted video as output.avi to the designated target directory. For the most part, it runs fine - it finds the video, runs the canny edge detection just fine, and outputs to the correct directory.

The issue is every time the code outputs just a 5.5kb file (which I think is just the avi header). The strange thing is that if out.write(edges) is replaced with out.write(frame), the video outputs correctly, having converted from the RAW file to an avi. This leads me to believe there is some issue with the edges output, but I have no idea what it is, as both frame and edges have the exact same type.

Note: using Python 2.7.3 and OpenCV 3.0

import cv2, os, glob, re
import numpy as np
target_dir = "/home/pi/raw_dump"

fps = 30
width = 320
height = 240

os.chdir(target_dir)
print "Detecting valid files for conversion..."

for file in glob.glob("*RAW.h264"):
    print("Converting file: "+file)

    #Find the name of the file
    output_filename = re.sub("-RAW\.h264","", file)

    #Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('output.avi',fourcc,fps,(width,height))

    #create capture stream object on which to modify
    cap_stream = cv2.VideoCapture(file)
    while(cap_stream.isOpened()):
        # Take each frame
        ret, frame = cap_stream.read()

        if ret == True:
            #flip the frame
            frame = cv2.flip(frame,0)

            # modify the arguments for cv2.Canny to change thresh values
            edges = cv2.Canny(frame,100,200)

            #print modified frame
            cv2.imshow('edges',edges)

            #print original frame
            #cv2.imshow('frame',frame)

            out.write(edges)

            # if waitkey() returns anything other than -1, and error may have occurred. abort
            if cv2.waitKey(10) >= 0:
                break
        else:
            print "Finishing conversion..."
            break

    # release and clean up streams
    print "Created output_filename.avi"

    cap_stream.release()
    out.release()

If anyone has any idea of how to fix this issue, please let me know.

Thanks for your time.

edit retag flag offensive close merge delete

Comments

3

I dont known much aboout the Python implementation of OpenCV, as I use only C++. In my experience, in most cases where VideoWriter fails to write any frame silently and generates just the file header is when the width and height of the written frame is not the same as the specified in the VideoWriter constructor. Also, you should verify if Mat type of "edges" is CV_8UC3 (8bit, 3 channel). Sending a CV_32F(float) or a CV_64F(double) frame may cause problems.

R.Saracchini gravatar imageR.Saracchini ( 2015-01-21 01:45:51 -0600 )edit

Could you try changing width and height in place in this line out = cv2.VideoWriter('output.avi',fourcc,fps,(width,height))? OpenCV randomly switches between width,height params and rows,cols which is exactly the opposite order. But then again it should not work with the normal frame ...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-21 04:33:45 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-06-24 11:49:09 -0600

Snyder gravatar image

Hello, I am not sure if you have found a solution or gave up, but this is what I have found. When you are using cv2.VideoWriter('output.avi',fourcc,fps,(width,height)) you are missing the last argument of the function, is_color. By default, that is set to true, but you need to set it to false. So just write cv2.VideoWriter('output.avi',fourcc,fps,(width,height),false) Your program should work then.

edit flag offensive delete link more

Comments

Indeed that solved the same problem above that I encounter. Thanks!

enzyme gravatar imageenzyme ( 2016-03-08 03:21:19 -0600 )edit
1

It really helped me a lot. I spent so much time to solve this problem. Thanks a lot! and add one more comment: for me, I am using opencv3 and python3, false didn't work and False worked instead.

onAR gravatar imageonAR ( 2017-03-06 12:03:34 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-01-21 00:40:52 -0600

Seen: 4,609 times

Last updated: Jun 24 '15