python – 捕获图像进行处理

python – 捕获图像进行处理,第1张

概述我正在使用 Python与PIL和SciPy. 我想从网络摄像头捕获图像,然后使用numpy和Scipy进一步处理它.有人可以帮我解决问题. 这里的代码有一个预定义的图像“lena”,但我希望使用我自己捕获的图像而不是“lena”图像.我对代码做了哪些更改? from scipy import misclena = misc.lena()lx, ly = lena.shapeimport m 我正在使用 Python与PIL和SciPy.
我想从网络摄像头捕获图像,然后使用numpy和Scipy进一步处理它.有人可以帮我解决问题.

这里的代码有一个预定义的图像“lena”,但我希望使用我自己捕获的图像而不是“lena”图像.我对代码做了哪些更改?

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

另一个例子

import scipyfrom scipy import ndimageimport matplotlib.pyplot as pltimport numpy as npl = 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 Devicefrom numpy import *from PIL import Imagecam = Device(devnum=0,showVIDeoWindow=0) #devnum=0 means you are using the device set in 0 position probably your webcamblackimg= 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 wantimage=cam.getimage() #this is a real image PIL imageimgarray = 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 beforeretval,image = cam.read() #return a True bolean and and the image if all go rightcam.release() #Closes vIDeo file or capturing device.

在这种情况下,你有一个numpy.ndarray(没有更多的PIL图像),所以要在shell中显示图像类型:

import matplotlib.pyplot as pltplt.imshow(image)

使用CaptureFromCAM的旧方法

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

你可以像上面那样显示它.

总结

以上是内存溢出为你收集整理的python – 捕获图像进行处理全部内容,希望文章能够帮你解决python – 捕获图像进行处理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1207394.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存