Python OpenCV处理图像之图像直方图和反向投影
作者:Leo 时间:2023-09-26 20:18:13
本文实例为大家分享了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)
来源:https://blog.csdn.net/qq_26898461/article/details/50454528
标签:python,OpenCV,图像直方图,反向投影
0
投稿
猜你喜欢
Python经典案例之图像漫水填充分割详解
2021-08-25 11:41:14
利用Python找出删除自己微信的好友并将他们自动化删除
2022-05-23 00:10:13
友情连接地址代码-线线表格
2010-07-01 16:26:00
Python冲顶大会 快来答题!
2022-05-27 08:10:56
PHP基础用法讲解及phpinfo();演示
2023-05-29 08:34:29
pandas数据类型之Series的具体使用
2022-03-30 18:54:46
python实现从ftp服务器下载文件
2022-04-30 18:54:53
js插入flash可防止虚线框激活
2009-03-13 13:31:00
如何使用Git优雅的回滚实现
2022-03-17 15:49:15
Python datetime时间格式化去掉前导0
2022-03-15 11:43:15
ASP脚本循环语句
2009-02-19 13:34:00
EXECUTE IMMEDIATE用法小结
2009-09-26 18:32:00
Python3使用requests发闪存的方法
2021-06-09 16:07:20
pandas groupby 分组取每组的前几行记录方法
2021-06-19 05:52:20
MYSQL各字段的长度是多少?
2009-10-28 18:32:00
Python中functools模块的常用函数解析
2022-08-12 08:10:50
js返回顶部代码
2011-04-25 19:21:00
selenium自动化测试简单准备
2023-02-07 13:04:12
python3获取当前文件的上一级目录实例
2021-06-03 02:22:22
SQL Server 空值处理策略[推荐]
2024-01-15 17:07:41