Python OpenCV處理圖像之圖像直方圖和反向投影
本文實(shí)例為大家分享了Python OpenCV圖像直方圖和反向投影的具體代碼,供大家參考,具體內(nèi)容如下
當(dāng)我們想比較兩張圖片相似度的時(shí)候,可以使用這一節(jié)提到的技術(shù)
關(guān)于這兩種技術(shù)的原理可以參考我上面貼的鏈接,下面是示例的代碼:
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)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python腳本打包后無(wú)法運(yùn)行exe文件的解決方案
這篇文章主要介紹了python腳本打包后無(wú)法運(yùn)行exe文件的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03Python實(shí)現(xiàn)基本數(shù)據(jù)結(jié)構(gòu)中隊(duì)列的操作方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)基本數(shù)據(jù)結(jié)構(gòu)中隊(duì)列的操作方法,結(jié)合實(shí)例形式演示了Python針對(duì)數(shù)據(jù)結(jié)構(gòu)中隊(duì)列的初始化、插入、刪除、判斷隊(duì)列滿(mǎn)及隊(duì)列空等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12通過(guò)python實(shí)現(xiàn)彈窗廣告攔截過(guò)程詳解
這篇文章主要介紹了通過(guò)python實(shí)現(xiàn)彈窗廣告攔截過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Python數(shù)據(jù)分析的八種處理缺失值方法詳解
缺失值可能是數(shù)據(jù)科學(xué)中最不受歡迎的值,然而,它們總是在身邊。忽略缺失值也是不合理的,因此我們需要找到有效且適當(dāng)?shù)靥幚硭鼈兊姆椒?/div> 2021-11-11Python基于httpx模塊實(shí)現(xiàn)發(fā)送請(qǐng)求
這篇文章主要介紹了Python基于httpx模塊實(shí)現(xiàn)發(fā)送請(qǐng)求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07python3學(xué)習(xí)之Splash的安裝與實(shí)例教程
splash 是一個(gè)python語(yǔ)言編寫(xiě)的用于配合scrapy解析js的庫(kù),下面這篇文章主要給大家介紹了關(guān)于python3學(xué)習(xí)之Splash的安裝與使用的一些相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07python判斷字符串以什么結(jié)尾的實(shí)例方法
在本篇文章里小編給大家整理了關(guān)于python判斷字符串以什么結(jié)尾的實(shí)例方法 ,需要的朋友們可以學(xué)習(xí)參考下。2020-09-09分享15?個(gè)python中的?Scikit-Learn?技能
這篇文章主要介紹了分享15?個(gè)python中的?Scikit-Learn?技能,Scikit-Learn?是一個(gè)非常棒的?python?庫(kù),用于實(shí)現(xiàn)機(jī)器學(xué)習(xí)模型和統(tǒng)計(jì)建模,有降維、特征選擇、特征提取、集成技術(shù)等特征,下文相關(guān)內(nèi)容需要的朋友可以參考一下2022-03-03最新評(píng)論