Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture image for processing

I'm using Python with PIL and SciPy. i want to capture an image from a webcam then process it further using numpy and Scipy. Can somebody please help me out with the code.

Here is the code there is a predefined image "lena" but I wish to use my own captured image instead of the "lena" image. What changes do I make to the code?

from scipy import misc
lena = misc.lena()
lx, ly = lena.shape
import matplotlib.pyplot as plt
crop_lena = lena[lx / 4: - lx / 4, ly / 4: - ly / 4]
plt.imshow(crop_lena)

Another example

import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
import numpy as np
l = scipy.misc.lena()
plt.figure(figsize=(10, 3.6))
plt.subplot(131)
plt.imshow(l, cmap=plt.cm.gray)
plt.show()
like image 637
user2808264 Avatar asked Sep 25 '13 01:09

user2808264


2 Answers

Video Capture by Markus Gritsch

I've used a lot Video Capture by Markus Gritsch and this maybe is the simplest and fastest way to do what you want.

from VideoCapture import Device
from numpy import *
from PIL import Image
cam = Device(devnum=0, showVideoWindow=0) #devnum=0 means you are using the device set in 0 position probably your webcam
blackimg= cam.getImage() #this return a PIL image but I don't know why the first is always black
#blackimag.show()#try to check if you want
image=cam.getImage() #this is a real image PIL image
imgarray = asarray(image) #convert the image into a matrix
#imgarrayfloat = imgarray.astype('float') # in many cases of processing you have to convert to a float matrix because can occur overflow (e.g. for average images summing  pixels values of 255 and 3 of two images and divide by 2 gives you 1/2 for imgarray and 258/2 for imgarrayfloat 
#recovertedimage=processedimage.astype ("uint8")#if you use the previous you have to reconvert to unit8. Note processedimage is the name of the variable of your image.

Python OpenCV: cv2 & cv

You can do it with Python binding for OpenCV too. There are at least two ways for do that. I've found this and this tutorial interesting.

Video Capture

from cv2 import *
cam = VideoCapture(0)  #set the port of the camera as before
retval, image = cam.read() #return a True bolean and and the image if all go right
cam.release() #Closes video file or capturing device.

In this case you have a numpy.ndarray (no more PIL image) so to show the image type in the shell:

import matplotlib.pyplot as plt
plt.imshow(image)

Old way with CaptureFromCAM

import cv2.cv as cv 
import numpy as np  
Capture = cv.CaptureFromCAM(0)
image = cv.QueryFrame(Capture) #here you have an IplImage
imgarray = np.asarray(image[:,:]) #this is the way I use to convert it to numpy array

You can show it as above.

like image 188
G M Avatar answered Sep 30 '22 20:09

G M


You can use VideoCapture lib to get the picture from a USB camera.

from VideoCapture import Device

came = Device()
came.setResolution(320, 240)

img = cam.getImage()
...
like image 23
Kill Console Avatar answered Sep 30 '22 21:09

Kill Console