Skip to main content

Posts

Showing posts with the label opencv

Bring your hat!

So thought I would make a really simple example of how pygame can be used with a webcam. This example uses opencv to detect a face, then pygame to draw a "hat". #!/usr/bin/python from pycam import VideoCapturePlayer from pycam import pygameFaceDetect import pygame from pygame.locals import * def process(surf): faces = pygameFaceDetect.getFaces(surf) if faces: s = pygameFaceDetect.faceDetect.image_scale for face in faces: pointsInHat = [ (face.x*s, face.y*s), (face.x*s + face.width*s, face.y*s), (face.x*s + face.width*s/2, face.y*s - face.height*s/2 ) ] pygame.draw.polygon(surf, Color("red"), pointsInHat) pygame.draw.polygon(surf, Color("black"), pointsInHat, 10) return surf if __name__ == "__main__": vcp = VideoCapturePlayer(processFunction=process) vcp.main() pygame.quit() And the obligatory screen shot: ...

Greenscreen

Something used heaps in the film industry is the " Greenscree n" I thought I would take a quick look at how to make a greenscreen that works fast enough to run on a live webcam stream. And infact one that works with any coloured background. It has many many limitations, but was a fun experiment! To run this example you will need OpenCV with the SWIG Python bindings installed. You can get this code from my SVN repository here . Firstly the background I started with: Adding an object to the scene, and carrying out back ground subtraction: So anyhow the code: #!/usr/bin/env python from VideoCapturePlayer import VideoCapturePlayer as VCP from opencv import cv def threshold_image ( image , n = []): """Record the first 5 images to get a background, then diff current frame with the last saved frame. """ if len ( n ) < 5 : # n[4] will be our background # First capture a few images ...

Gaussian Blur

Trying to work out why the gaussian blur in OpenCV is different from that of SciPy... The differences are too small to see, but are still there. Following is an imshow of each channel of the difference image. And by looking at a singe row, we see that the difference spikes the whole intensity range. Edit: That was pretty much staring me in the face wasn't it... uint8's are prone to integer overflow! Comparing the OpenCV implementation with two versions in SciPy now gives: Doing a pixel for pixel comparison on each channel between the SciPy and OpenCV examples: The second one is comparing an IIR filter implementation to a ndfilt.

Decoration to the rescue!

import numpy as np from scipy import signal from opencv import adaptors class scipyFromOpenCV ( object ): """This decorator can be used to wrap a function that takes and returns a numpy array into one that takes and retuns an opencv CvMat. """ def __init__ ( self , f ): self . f = f def __call__ ( self , image ): # Convert CvMat to ndarray np_image = adaptors . Ipl2NumPy ( image ) # Call the original function np_image_filtered = self . f ( np_image ) # Convert back to CvMat return adaptors . NumPy2Ipl ( np_image_filtered ) @scipyFromOpenCV def slowGaussianBlur ( matrix ): """Manual gaussian blur - Very very very slow!""" filterSize = 3 filt = gauss_kern ( filterSize ) r = signal . convolve ( matrix [:,:, 0 ], filt , 'same' ) g = signal . convolve ( matrix [:,:, 1 ], filt ...

Opencv source on Ubuntu 9.10

The build instructions for opencv really didn't make it clear how to go about building opencv. Well the ./configure command kept failing... So for my own reference, use the cmake method not autotools. cd ~/projects/opencv # the directory containing INSTALL, CMakeLists.txt etc.  mkdir release  cd release  cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ../ make sudo make install

Camera Histogram Demo

There is a really cool demo for opencv in C called camshiftdemo. I decided to write a version in python... The only one I could find online used opencv with ctypes , so this was a simple enough modification to get it running in more pure "swigged" python. It's in my pycam repository if anyone is interested:  http://code.google.com/p/pycam/source/browse/trunk/pycam/cam-histo.py Here are some screenshots. This is the python cam shift demo tracking the orange on the XO's logo. Now tracking the green section, you can see the histogram for the entire image shown. It is a really simple demo, and quite fun! At the Hitlab open night this was what I used to keep the kids occupied! Unlike the previous computer vision post - this doesn't use pygame at all. It would be a really good example for the XO pippy computer vision package idea however.

Project makes it to the intertubes...

Well I have finally put my code into a repository. It's all open source. I listed it on pygame.org and it went to the front page automatically which was cool! You can find my project at http://www.pycam.googlecode.com The pygame project page is: http://pygame.org/project/1111/

Face Detection with python using opencv

So I have been continuing on with the live edge detection I looked out a few weeks ago... I have made that code alot more object oriented and hopefully re-useable. I am now using both pygame and opencv built from svn instead of the ubuntu repositories. I wanted independence in the image rendering, the webcam capturing and the image processing. So I needed to convert between a numpy array (which pygame and any scipy processing uses) and cvMat which is opencv's data type. This was not immediately obvious as the opencv.adaptors module was full of routines for converting via the Python Image Library (PIL). These images were annoyingly being rotated by the functions when going from numpy to cvMat, then rotated back to the correct way going back from cvMat to numpy. First up is the VideoCapturePlayer class, it can be used to simply display a video feed. It uses pygame camera, stores the images as a pygame.surface and shows the video with pygame. The latest pygame has an option to force ...