Python OpenCV处理图像之图像直方图和反向投影
2018-07-10 17:13
1001 查看
本文实例为大家分享了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-OpenCV 处理图像(四)(五):图像直方图和反向投影 图像中边界和轮廓检测
- Python3与OpenCV3.3 图像处理(十二)--图像直方图应用
- Python-OpenCV 处理图像(四):图像直方图和反向投影
- Python3与OpenCV3.3 图像处理(十一)--图像直方图
- Python图像处理(1):替换OpenCV
- 【opencv+python】图像处理之二、几何变换(仿射与投影)的原理
- Python3与OpenCV3.3 图像处理(十三)--反射投影
- 【OpenCV3图像处理】图像直方图均衡化、直方图匹配
- opencv6.4-imgproc图像处理模块之直方图与模板
- Python与图像处理(1):绘制图像,点和线,图像轮廓,直方图
- 【图像处理】基于OpenCV底层实现的直方图匹配
- Python3与OpenCV3.3 图像处理(二十二)--对象测量(纯代码)
- Python-OpenCV 处理图像(一):基本操作
- Opencv Python版学习笔记(一)图像直方图
- 【OpenCV图像处理】十、图像的直方图及相关处理(上)
- python数字图像处理(9):直方图与均衡化
- Python-OpenCV 处理图像(一):基本操作 cv2
- Python-OpenCV 处理图像(三):图像像素点操作
- 纯Python综合图像处理小工具(1)分通道直方图
- 基于opencv的Python图像处理