Example 4 - Smooth, shrink, canny a video

From Devipedia

Jump to: navigation, search

I've added the ability to write the video to file using cvVideoWriter in this example and added a few additional transformations. I wrote a few wrapper functions as recommended in Learning OpenCV, one to shrink the video size by half, one to perform the Canny algorithm on the frames, and one to convert the video from color to black and white. This one was not in the book, but was needed as Canny requires black and white input.

After writing the code, I executed, and got an output video that was not exactly what I expected.


<websiteFrame> website=http://www.youtube.com/embed/qVXClkLQXlE height=349 width=425 border=0 scroll=auto </websiteFrame>

Apparently on Linux, there is an issue with writing out grayscale. This post got me back on track:

http://www.bananask.com/questions/178/view.html

After making my tweaks, this is the result. I dropped the powerDown calls from the source to preserve resolution.

<websiteFrame> website=http://www.youtube.com/embed/4iyvBJnvOwQ height=349 width=425 border=0 scroll=auto </websiteFrame>



import sys
from opencv.highgui import *
from opencv.cv import *

def merge(img):
    out = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3)
    cvMerge(img, img, img, 0, out)
    return out

def powerDown(img, filter = CV_GAUSSIAN_5x5 ):
    
    # Best to make sure input image is divisible by two
    assert img.width % 2 == 0 and img.height % 2 == 0, 'Not divisible by two'

    out = cvCreateImage(cvSize(img.width/2, img.height/2), 
            img.depth,
            img.nChannels)
    cvPyrDown(img, out)
    return out

def toGrayScale(img):
    out = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1)
    cvCvtColor(img, out, CV_BGR2GRAY)
    return out
     
def canny(img, lowThresh, highThresh, aperture):
   
    # Canny only handles gray scale images 
    if img.nChannels != 1:
        return img

    out = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1)
    cvCanny(img, out, lowThresh, highThresh, aperture)
    return out 


if __name__ == '__main__':

    cvNamedWindow("Example4", CV_WINDOW_AUTOSIZE)
    capture = cvCreateFileCapture(sys.argv[1])
    writer = None
                 
    while True:
        frame = cvQueryFrame( capture )
        if frame == None:
            break

        out = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3)
        cvSmooth( frame, out, CV_GAUSSIAN, 3, 3)

        #out = powerDown(out)
        #out = powerDown(out)
        out = toGrayScale(out)
        out = canny(out, 10, 100, 3)
        out = merge(out)
        
        if writer == None:
            writer  = cvCreateVideoWriter('output.mp4',
                        CV_FOURCC('D','I','V','X'),
                        24,
                        cv.cvSize(out.width,out.height)
                        )

        cvWriteFrame(writer, out)
        cvShowImage("Example4", out)

        c = cvWaitKey(33)
        if c == chr(27):  
            break       
    cvReleaseVideoWriter(writer)
    cvReleaseCapture(capture)
    cvDestroyWindow("Example4")
Personal tools