【问题标题】:Capture image for processing捕获图像进行处理
【发布时间】:2013-09-25 01:02:42
【问题描述】:

我将 Python 与 PIL 和 SciPy 结合使用。 我想从网络摄像头捕获图像,然后使用 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()

【问题讨论】:

    标签: python python-imaging-library webcam


    【解决方案1】:

    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 绑定来完成此操作。至少有两种方法可以做到这一点。我发现 this 和 this 教程很有趣。

    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 图像),以便在 shell 中显示图像类型:

    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
    

    你可以如上所示。

    【讨论】:

    • 不幸的是,VideoCapture 似乎在 4 年内没有更新,只能在 Windows 上运行。
    • 如果您在末尾添加plt.show(),则 cv2 示例有效,但不幸的是,颜色太差了。
    【解决方案2】:

    您可以使用 VideoCapture 库从 USB 摄像头获取图片。

    from VideoCapture import Device
    
    came = Device()
    came.setResolution(320, 240)
    
    img = cam.getImage()
    ...
    

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 2012-11-17
      相关资源
      最近更新 更多