时间:2021-05-22
本文实例为大家分享了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 imageorig = 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 valuesdrawGraph(hist.bins, histImg, histsize) cv.ShowImage("Original Image", orig)cv.ShowImage("Original Histogram", histImg)#--------------------- #---- Equalized imageimEq = 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 pictureeqImg = cv.CreateMat(histsize, histsize, cv.CV_8U) #Image that will contain the graph of the repartition of valuesdrawGraph(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 pictureshist = 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.0cv.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)以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
最近跟着OpenCV2-Python-Tutorials在学习python_opencv中直方图的反向投影时,第一种方法是使用numpy实现将图中的红色玫瑰分割
本文实例讲述了Python图像处理之简单画板实现方法。分享给大家供大家参考,具体如下:Python图像处理也是依赖opencv的Python接口实现的,Pyth
直方图反向投影:即取直方图中的值,按直方图面积由大到小,对其对应的像素也由大到小赋予新值。即某种灰度值在图像中所占面积越大,其对应的像素的新值就越大;反之就越小
语言:python+opencv为什么使用图像腐蚀和图像膨胀如图,使用图像腐蚀进行去噪,但是为压缩噪声。对腐蚀过的图像,进行膨胀处理,可以去除噪声,并保持原样形
OpenCV编程实例之图像文件批量读取。本博文摘录《OpenCV图像处理编程实例》2.4章节,更详细的内容请参考本书。在进行图片序列处理时,我们常常需要读取文件