python调用摄像头显示图像的实例


Posted in Python onAugust 03, 2018

如下所示:

import cv2
import numpy as np

bins = np.arange(256).reshape(256,1)

def hist_curve(im):
 h = np.zeros((300,256,3))
 if len(im.shape) == 2:
  color = [(255,255,255)]
 elif im.shape[2] == 3:
  color = [ (255,0,0),(0,255,0),(0,0,255) ]
 for ch, col in enumerate(color):
  hist_item = cv2.calcHist([im],[ch],None,[256],[0,256])
  cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
  hist=np.int32(np.around(hist_item))
  pts = np.int32(np.column_stack((bins,hist)))
  cv2.polylines(h,[pts],False,col)
 y=np.flipud(h)
 return y

def hist_lines(im):
 h = np.zeros((300,256,3))
 if len(im.shape)!=2:
  print "hist_lines applicable only for grayscale images"
  #print "so converting image to grayscale for representation"
  im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
 hist_item = cv2.calcHist([im],[0],None,[256],[0,256])
 cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
 hist=np.int32(np.around(hist_item))
 for x,y in enumerate(hist):
  cv2.line(h,(x,0),(x,y),(255,255,255))
 y = np.flipud(h)
 return y


if __name__ == '__main__':

 import sys

 if len(sys.argv)>1:
  im = cv2.imread(sys.argv[1])
 else :
  im = cv2.imread('../cpp/lena.jpg')
  print "usage : python hist.py <image_file>"


 gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)


 print ''' Histogram plotting \n
 Keymap :\n
 a - show histogram for color image in curve mode \n
 b - show histogram in bin mode \n
 c - show equalized histogram (always in bin mode) \n
 d - show histogram for color image in curve mode \n
 e - show histogram for a normalized image in curve mode \n
 Esc - exit \n
 '''

 cv2.imshow('image',im)
 while True:
  k = cv2.waitKey(0)&0xFF
  if k == ord('a'):
   curve = hist_curve(im)
   cv2.imshow('histogram',curve)
   cv2.imshow('image',im)
   print 'a'
  elif k == ord('b'):
   print 'b'
   lines = hist_lines(im)
   cv2.imshow('histogram',lines)
   cv2.imshow('image',gray)
  elif k == ord('c'):
   print 'c'
   equ = cv2.equalizeHist(gray)
   lines = hist_lines(equ)
   cv2.imshow('histogram',lines)
   cv2.imshow('image',equ)
  elif k == ord('d'):
   print 'd'
   curve = hist_curve(gray)
   cv2.imshow('histogram',curve)
   cv2.imshow('image',gray)
  elif k == ord('e'):
   print 'e'
   norm = cv2.normalize(gray,alpha = 0,beta = 255,norm_type = cv2.NORM_MINMAX)
   lines = hist_lines(norm)
   cv2.imshow('histogram',lines)
   cv2.imshow('image',norm)
  elif k == 27:
   print 'ESC'
   cv2.destroyAllWindows()
   break
 cv2.destroyAllWindows()

以上这篇python调用摄像头显示图像的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
如何准确判断请求是搜索引擎爬虫(蜘蛛)发出的请求
Oct 13 Python
黑科技 Python脚本帮你找出微信上删除你好友的人
Jan 07 Python
Python中防止sql注入的方法详解
Feb 25 Python
基于Python Numpy的数组array和矩阵matrix详解
Apr 04 Python
Python实现的凯撒密码算法示例
Apr 12 Python
python之文件读取一行一行的方法
Jul 12 Python
tensorflow实现加载mnist数据集
Sep 08 Python
10分钟教你用Python实现微信自动回复功能
Nov 28 Python
win10系统下python3安装及pip换源和使用教程
Jan 06 Python
Python读取二进制文件代码方法解析
Jun 22 Python
python中selenium库的基本使用详解
Jul 31 Python
浅谈Python列表嵌套字典转化的问题
Apr 07 Python
python开启摄像头以及深度学习实现目标检测方法
Aug 03 #Python
Python函数参数操作详解
Aug 03 #Python
利用python打开摄像头及颜色检测方法
Aug 03 #Python
numpy添加新的维度:newaxis的方法
Aug 02 #Python
numpy.ndarray 交换多维数组(矩阵)的行/列方法
Aug 02 #Python
对numpy中的transpose和swapaxes函数详解
Aug 02 #Python
Numpy 改变数组维度的几种方法小结
Aug 02 #Python
You might like
解析VS2010利用VS.PHP插件调试PHP的方法
2013/07/19 PHP
PHP学习笔记(一) 简单了解PHP
2014/08/04 PHP
SESSION存放在数据库用法实例
2015/08/08 PHP
Zend Framework教程之Loader以及PluginLoader用法详解
2016/03/09 PHP
一端时间轮换的广告
2006/06/26 Javascript
js Flash插入函数免激活代码
2009/03/31 Javascript
IE中radio 或checkbox的checked属性初始状态下不能选中显示问题
2009/07/25 Javascript
克隆javascript对象的三个方法小结
2011/01/12 Javascript
JS 添加网页桌面快捷方式的代码详细整理
2012/12/27 Javascript
原生javascript兼容性测试实例
2013/07/01 Javascript
js实现广告漂浮效果的小例子
2013/07/02 Javascript
jQuery获取节点和子节点文本的方法
2014/07/22 Javascript
JS使用cookie实现DIV提示框只显示一次的方法
2015/11/05 Javascript
window.setInterval()方法的定义和用法及offsetLeft与style.left的区别
2015/11/11 Javascript
javascript使用递归算法求两个数字组合功能示例
2017/01/03 Javascript
解决vue2.x中数据渲染以及vuex缓存的问题
2017/07/13 Javascript
详解使用webpack打包编写一个vue-toast插件
2017/11/08 Javascript
详解vue中router-link标签所必备了解的属性
2019/04/15 Javascript
JavaScript 异步时序问题
2020/11/20 Javascript
[48:35]2018DOTA2亚洲邀请赛 4.1 小组赛 A组加赛 TNC vs Optic
2018/04/03 DOTA
Python HTMLParser模块解析html获取url实例
2015/04/08 Python
python 使用sys.stdin和fileinput读入标准输入的方法
2018/10/17 Python
python游戏开发之视频转彩色字符动画
2019/04/26 Python
解决django后台样式丢失,css资源加载失败的问题
2019/06/11 Python
Python流程控制常用工具详解
2020/02/24 Python
Python logging模块原理解析及应用
2020/08/13 Python
Python‘==‘ 及 ‘is‘相关原理解析
2020/09/05 Python
寻找完美的房车租赁:RVShare
2019/02/23 全球购物
匡威西班牙官网:Converse西班牙
2019/10/01 全球购物
C&A巴西网上商店:时尚、衣服、手机和鞋子
2020/06/07 全球购物
《跨越百年的美丽》教学反思
2014/02/11 职场文书
应聘会计求职信
2014/06/11 职场文书
2015年全国“爱牙日”宣传活动总结
2015/03/23 职场文书
社区低保工作总结2015
2015/07/23 职场文书
2016道德模范先进事迹材料
2016/02/26 职场文书
vue+element ui实现锚点定位
2021/06/29 Vue.js