捕获图像进行处理

2024-01-11

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

视频捕捉(Markus Gritsch) http://videocapture.sourceforge.net/

我用过很多视频捕捉(Markus Gritsch) http://videocapture.sourceforge.net/这也许是做你想做的事最简单、最快的方法。

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 http://opencv.org/

您可以使用 Python 绑定来完成OpenCV http://opencv.org/也。至少有两种方法可以做到这一点。我发现了this http://jayrambhia.wordpress.com/2012/05/10/capture-images-and-video-from-camera-in-opencv-2-3-1/ and this http://codeplasma.com/2012/12/03/getting-webcam-images-with-python-and-opencv-2-for-real-this-time/教程很有趣。

视频截取 http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=videocapture#VideoCapture

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

捕获图像进行处理 的相关文章

随机推荐

  • java - HashMap 中的内容适当的数据

    想象一下您有一本学生评价日记 每个学生在日记中都有每个科目的分数 我想将其存储在HashMap lt gt 但我不明白为什么标记会合并 在期刊课上 public class Journal private static HashMap
  • 两个十六进制数的相似度

    我试图使用汉明和编辑距离找到类似的哈希值 十六进制哈希值 假设两个哈希值相似 如果它们的汉明距离小于 10 不同位数 Hash 1 ffffff base 16 Hash 2 fffff0 base 16 两个哈希之间的汉明距离是4 它们是
  • Android 成功分享意图

    如何判断用户是否成功完成了共享意图 例如 如果用户想通过 Facebook 或 Twitter 分享应用程序 Edit 我不是在研究如何创建Intent共享的 我想知道用户是否确实分享了任何内容 或者用户是否按下了取消按钮 我认为没有一种可
  • 在 Recyclerview 或 Listview 中添加多个标题。随机地

    如何在 RecyclerView 或 listview 中添加多个标题或分隔符 随机如下图突出显示 RecyclerView 中基于日期的多个标题 演示图像 您可以使用库来实现它分段回收器视图适配器 https github com lui
  • Project Euler - #1 Python 错误解决方案

    总体而言 我对编码还比较陌生 因此启动了欧拉项目 以使我的编码更进一步 花了一些时间思考如何自己解决第一个问题并尝试使用递归函数 不幸的是总是遇到同样的错误 266333 我错过了什么 有什么重大错误值得学习吗 原问题是 如果我们列出所有
  • 如果我使用 iPad 尺寸创建应用程序,如何找到 iPhone 尺寸的“安全区域”?

    我正在创建一个游戏 想要使用 iPad 尺寸设置 横向 1024x768 据我所知 使用 iPad 设置只会在 iPhone 上裁剪掉部分高度 假设我有一张 iPad 的 2048x1536 背景图像 1024x768 和 iPhone 6
  • 为什么 Node 比 Chrome 慢 10 倍?

    我正在运行我的Z80模拟器 https github com lkesteloot z80 emulator在 Chrome 和 Node 中 我在 Chrome 中获得的性能大约是 Node 中的 10 倍 100k Z80 指令在 Ch
  • 在 python 中创建线程

    我有一个脚本 我希望一个函数与另一个函数同时运行 我看过的示例代码 import threading def MyThread threading thread doing something def MyThread2 threading
  • DB::raw laravel 5.4 中的 WHERE IN 数组绑定

    我正在尝试将数组绑定为原始数组WHERE IN在 Laravel 中查询DB example arr 1 2 3 DB select DB raw select from test1 WHERE id IN arr 由于某种原因 数组没有更
  • node.js eventEmitter + http.request

    我做了这个教程Node js 事件发射器 http www mshiltonj com blog 2011 10 04 nodejs eventemitter example with custom events 效果很好 我添加了一个使用
  • 使用 Play Framework 和具有超过 22 个参数的案例类

    我已经看到了一些涉及臭名昭著的 22 个字段 参数 问题的其他问题 这是 Scala V here and here https stackoverflow com questions 33494526 how to unlimit spr
  • SqlDataSource 中的动态 WHERE 子句

    我在一个非常简单的应用程序中使用 SqlDataSource 我允许用户通过文本框为 SDS 的选择命令设置多个搜索参数 每个参数一个文本框 例如 txtFirstName txtLastName 等 我计划使用按钮单击事件处理程序来设置
  • XML 正则表达式 - 负匹配

    我在 XSD 模式中遇到负前瞻问题 当我指定
  • 将 jQuery 转换为无冲突模式

    这是我正在使用的脚本 window load function edifici artistici industriale fotovoltaico veterinaria architettonici hide if window loc
  • R - 数字的条件格式(小数位)

    我正在 R 中构建一个闪亮的应用程序 其输出之一是一个包含摘要数据的表格 此表中出现的值变化很大 从 0 003 到 3 450 023 我希望有一种方法可以格式化要显示的数字 例如 小于 0 的数字始终显示三位小数 0 到 10 之间的数
  • GROUP BY - 不对 NULL 进行分组

    我正在尝试找出一种使用 group by 函数返回结果的方法 GROUP BY 按预期工作 但我的问题是 是否可以通过忽略 NULL 字段来进行分组 这样它就不会将 NULL 分组在一起 因为我仍然需要指定字段为 NULL 的所有行 SEL
  • 如何消除嵌入 Power Bi 的报表视觉效果的灰色边框?

    When i render the power bi visuals I notice that there is a grey border on the right and left side of the image Is there
  • Sass开发工作流程问题

    我一直在尝试将 Sass 采用到我的开发工作流程中 我主要从事前端开发 因此我经常更新样式表 经过无休止的搜索 我还没有找到我认为应该是一个简单问题的答案 我同时使用 Coda 和 Textmate 因此两者的解决方案就足够了 在本地开发时
  • Python:巨大的指数被卡住,但不会引发错误

    我在 Win10 x64 上使用 Python 3 6 print 10 10 10 10 这不应该引发错误吗 当我运行它时 它只是不打印任何内容 但不会引发任何错误 没有溢出错误 没有内存错误 什么都没有 现在已经运行了大约 10 分钟
  • 捕获图像进行处理

    我将 Python 与 PIL 和 SciPy 一起使用 我想从网络摄像头捕获图像 然后使用 numpy 和 Scipy 进一步处理它 有人可以帮我解决代码吗 这是代码 有一个预定义的图像 lena 但我希望使用我自己捕获的图像而不是 le