Python OpenCV处理图像之图像直方图和反向投影


Posted in Python onJuly 10, 2018

本文实例为大家分享了Python OpenCV图像直方图和反向投影的具体代码,供大家参考,具体内容如下

当我们想比较两张图片相似度的时候,可以使用这一节提到的技术

直方图对比

反向投影

关于这两种技术的原理可以参考我上面贴的链接,下面是示例的代码:

0x01. 绘制直方图

import cv2.cv as cv
 
def drawGraph(ar,im, size): #Draw the histogram on the image
  minV, maxV, minloc, maxloc = cv.MinMaxLoc(ar) #Get the min and max value
  hpt = 0.9 * histsize
  for i in range(size):
    intensity = ar[i] * hpt / maxV #Calculate the intensity to make enter in the image
    cv.Line(im, (i,size), (i,int(size-intensity)),cv.Scalar(255,255,255)) #Draw the line
    i += 1
 
#---- Gray image
orig = cv.LoadImage("img/lena.jpg", cv.CV_8U)
 
histsize = 256 #Because we are working on grayscale pictures which values within 0-255
 
hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)
 
cv.CalcHist([orig], hist) #Calculate histogram for the given grayscale picture
 
histImg = cv.CreateMat(histsize, histsize, cv.CV_8U) #Image that will contain the graph of the repartition of values
drawGraph(hist.bins, histImg, histsize)
 
cv.ShowImage("Original Image", orig)
cv.ShowImage("Original Histogram", histImg)
#---------------------
 
#---- Equalized image
imEq = cv.CloneImage(orig)
cv.EqualizeHist(imEq, imEq) #Equlize the original image
 
histEq = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)
cv.CalcHist([imEq], histEq) #Calculate histogram for the given grayscale picture
eqImg = cv.CreateMat(histsize, histsize, cv.CV_8U) #Image that will contain the graph of the repartition of values
drawGraph(histEq.bins, eqImg, histsize)
 
cv.ShowImage("Image Equalized", imEq)
cv.ShowImage("Equalized HIstogram", eqImg)
#--------------------------------
 
cv.WaitKey(0)

0x02. 反向投影

import cv2.cv as cv
 
im = cv.LoadImage("img/lena.jpg", cv.CV_8U)
 
cv.SetImageROI(im, (1, 1,30,30))
 
histsize = 256 #Because we are working on grayscale pictures
hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)
cv.CalcHist([im], hist)
 
 
cv.NormalizeHist(hist,1) # The factor rescale values by multiplying values by the factor
_,max_value,_,_ = cv.GetMinMaxHistValue(hist)
 
if max_value == 0:
  max_value = 1.0
cv.NormalizeHist(hist,256/max_value)
 
cv.ResetImageROI(im)
 
res = cv.CreateMat(im.height, im.width, cv.CV_8U)
cv.CalcBackProject([im], res, hist)
 
cv.Rectangle(im, (1,1), (30,30), (0,0,255), 2, cv.CV_FILLED)
cv.ShowImage("Original Image", im)
cv.ShowImage("BackProjected", res)
cv.WaitKey(0)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用item()方法遍历字典的例子
Aug 26 Python
Python数据分析中Groupby用法之通过字典或Series进行分组的实例
Dec 08 Python
Python+OpenCV实现车牌字符分割和识别
Mar 31 Python
python入门:这篇文章带你直接学会python
Sep 14 Python
浅谈numpy生成数组的零值问题
Nov 12 Python
Python3安装Pillow与PIL的方法
Apr 03 Python
python实现输入任意一个大写字母生成金字塔的示例
Oct 27 Python
在 Linux/Mac 下为Python函数添加超时时间的方法
Feb 20 Python
学会python自动收发邮件 代替你问候女友
May 20 Python
python Zmail模块简介与使用示例
Dec 19 Python
Python Selenium异常处理的实例分析
Feb 28 Python
教你利用python实现企业微信发送消息
May 23 Python
Python中 map()函数的用法详解
Jul 10 #Python
python 读取视频,处理后,实时计算帧数fps的方法
Jul 10 #Python
Python OpenCV处理图像之图像像素点操作
Jul 10 #Python
查找python项目依赖并生成requirements.txt的方法
Jul 10 #Python
Python OpenCV处理图像之滤镜和图像运算
Jul 10 #Python
Python使用cx_Freeze库生成msi格式安装文件的方法
Jul 10 #Python
python操作excel文件并输出txt文件的实例
Jul 10 #Python
You might like
十天学会php之第九天
2006/10/09 PHP
one.php 多项目、函数库、类库 统一为一个版本的方法
2020/08/24 PHP
在网页里看flash的trace数据的js类
2009/01/10 Javascript
ko knockoutjs动态属性绑定技巧应用
2012/11/14 Javascript
JS 实现Json查询的方法实例
2013/04/12 Javascript
JQuery伸缩导航练习示例
2013/11/13 Javascript
js 异步操作回调函数如何控制执行顺序
2013/12/24 Javascript
Javascript 绘制 sin 曲线过程附图
2014/08/21 Javascript
JavaScript极简入门教程(二):对象和函数
2014/10/25 Javascript
基于jQuery实现下拉框
2014/11/24 Javascript
JavaScript实现找质数代码分享
2015/03/24 Javascript
js获取图片宽高的方法
2015/11/25 Javascript
JS获取及验证开始结束日期的方法
2016/08/20 Javascript
Angular ng-repeat 对象和数组遍历实例
2016/09/14 Javascript
深入理解Javascript中的观察者模式
2017/02/20 Javascript
vue之数据交互实例代码
2017/06/20 Javascript
微信小程序methods中定义的方法互相调用的实例代码
2018/08/07 Javascript
JS中数组与对象的遍历方法实例小结
2018/08/14 Javascript
vue路由对不同界面进行传参及跳转的总结
2019/04/20 Javascript
一文快速了解JQuery中的AJAX
2019/05/31 jQuery
Js通过AES加密后PHP用Openssl解密的方法
2019/07/12 Javascript
python使用selenium登录QQ邮箱(附带滑动解锁)
2019/01/23 Python
python正则表达式匹配IP代码实例
2019/12/28 Python
python随机模块random使用方法详解
2020/02/14 Python
python为什么要安装到c盘
2020/07/20 Python
社区版pycharm创建django项目的方法(pycharm的newproject左侧没有项目选项)
2020/09/23 Python
利用Python实现学生信息管理系统的完整实例
2020/12/30 Python
python中HTMLParser模块知识点总结
2021/01/25 Python
利用HTML5中Geolocation获取地理位置调用Google Map API在Google Map上定位
2013/01/23 HTML / CSS
阳光体育:Sunny Sports(购买露营和远足设备)
2018/08/07 全球购物
毕业生捐书活动倡议书
2015/04/27 职场文书
高中生军训感言
2015/08/01 职场文书
2015年十月一日放假通知
2015/08/18 职场文书
学生会自荐信
2019/05/16 职场文书
创业计划书之婴幼儿游泳馆
2019/09/11 职场文书
在Java中Collection的一些常用方法总结
2021/06/13 Java/Android