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 相关文章推荐
详解python中executemany和序列的使用方法
Aug 12 Python
Python 2.x如何设置命令执行的超时时间实例
Oct 19 Python
python3第三方爬虫库BeautifulSoup4安装教程
Jun 19 Python
python的继承知识点总结
Dec 10 Python
python实现图像检索的三种(直方图/OpenCV/哈希法)
Aug 08 Python
Python多叉树的构造及取出节点数据(treelib)的方法
Aug 09 Python
python根据多个文件名批量查找文件
Aug 13 Python
python中dict()的高级用法实现
Nov 13 Python
手把手教你进行Python虚拟环境配置教程
Feb 03 Python
Python使用pdb调试代码的技巧
May 03 Python
python os模块在系统管理中的应用
Jun 22 Python
python使用自定义钉钉机器人的示例代码
Jun 24 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
星际争霸中的对战模式介绍
2020/03/04 星际争霸
PHP Memcached应用实现代码
2010/02/08 PHP
php中使用$_REQUEST需要注意的一个问题
2013/05/02 PHP
使用HMAC-SHA1签名方法详解
2013/06/26 PHP
php封装一个异常的处理类
2017/06/08 PHP
验证码在IE中不刷新而谷歌等浏览器正常的解决方案
2014/03/18 Javascript
JQuery下拉框应用示例介绍
2014/04/23 Javascript
简单的JS控制button颜色随点击更改的实现方法
2017/04/17 Javascript
Vue中的数据监听和数据交互案例解析
2017/07/12 Javascript
AngularJS点击添加样式、点击变色设置的实例代码
2017/07/27 Javascript
10行代码实现微信小程序滑动tab切换
2018/12/28 Javascript
微信小程序地图导航功能实现完整源代码附效果图(推荐)
2019/04/28 Javascript
微信小程序 bindtap 传参的实例代码
2020/02/21 Javascript
OpenLayers实现图层切换控件
2020/09/25 Javascript
vue中如何自定义右键菜单详解
2020/12/08 Vue.js
[53:38]OG vs LGD 2018国际邀请赛淘汰赛BO3 第三场 8.26
2018/08/30 DOTA
Python实现定时任务
2017/02/08 Python
Python基于list的append和pop方法实现堆栈与队列功能示例
2017/07/24 Python
python中利用await关键字如何等待Future对象完成详解
2017/09/07 Python
PyQt5的PyQtGraph实践系列3之实时数据更新绘制图形
2019/05/13 Python
pytorch 多分类问题,计算百分比操作
2020/07/09 Python
python实现登录与注册系统
2020/11/30 Python
CSS3 box-shadow属性实例详解
2020/06/19 HTML / CSS
详解px单位html5响应式方案
2018/03/08 HTML / CSS
移动端Html5页面生成图片解决方案
2018/08/07 HTML / CSS
anello泰国官方网站:日本流行包包品牌
2019/08/08 全球购物
英国最大的独立玩具专卖店:The Entertainer
2019/09/06 全球购物
九年级家长会邀请函
2014/01/15 职场文书
中秋节活动总结
2014/08/29 职场文书
2014年党支部书记工作总结
2014/12/04 职场文书
党支部评议意见
2015/06/02 职场文书
六一儿童节园长致辞
2015/07/31 职场文书
python实现腾讯滑块验证码识别
2021/04/27 Python
pytorch交叉熵损失函数的weight参数的使用
2021/05/24 Python
Vue.js中v-for指令的用法介绍
2022/03/13 Vue.js
MySQL的prepare使用以及遇到的bug
2022/05/11 MySQL