捕获图像以进行处理 [英] Capture image for processing

查看:0
本文介绍了捕获图像以进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是带有PIL和SciPy的Python。 我想从网络摄像头捕获图像,然后使用Numpy和Scipy进行进一步处理。有人能帮我弄一下代码吗?

以下是代码,其中有一个预定义的图像"lena",但我希望使用我自己捕获的图像而不是"lena"图像。我应该对代码进行哪些更改?

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)

另一个例子

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()

推荐答案

Video Capture by Markus Gritsch

我已经用了很多Video Capture by Markus Gritsch,这可能是做您想做的事情的最简单、最快的方法。

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

您也可以使用OpenCV的Python绑定来实现。至少有两种方法可以做到这一点。我发现thisthis教程很有趣。

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.

在本例中,您有一个numpy.ndarray(不再有PIL图像),以便在外壳中显示图像类型:

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

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

您可以如上显示。

这篇关于捕获图像以进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆