Python OpenCV處理圖像之濾鏡和圖像運(yùn)算
本文實(shí)例為大家分享了Python OpenCV處理圖像之濾鏡和圖像運(yùn)算的具體代碼,供大家參考,具體內(nèi)容如下
0x01. 濾鏡
喜歡自拍的人肯定都知道濾鏡了,下面代碼嘗試使用一些簡(jiǎn)單的濾鏡,包括圖片的平滑處理、灰度化、二值化等:
import cv2.cv as cv image=cv.LoadImage('img/lena.jpg', cv.CV_LOAD_IMAGE_COLOR) #Load the image cv.ShowImage("Original", image) grey = cv.CreateImage((image.width ,image.height),8,1) #8depth, 1 channel so grayscale cv.CvtColor(image, grey, cv.CV_RGBA2GRAY) #Convert to gray so act as a filter cv.ShowImage('Greyed', grey) # 平滑變換 smoothed = cv.CloneImage(image) cv.Smooth(image,smoothed,cv.CV_MEDIAN) #Apply a smooth alogrithm with the specified algorithm cv.MEDIAN cv.ShowImage("Smoothed", smoothed) # 均衡處理 cv.EqualizeHist(grey, grey) #Work only on grayscaled pictures cv.ShowImage('Equalized', grey) # 二值化處理 threshold1 = cv.CloneImage(grey) cv.Threshold(threshold1,threshold1, 100, 255, cv.CV_THRESH_BINARY) cv.ShowImage("Threshold", threshold1) threshold2 = cv.CloneImage(grey) cv.Threshold(threshold2,threshold2, 100, 255, cv.CV_THRESH_OTSU) cv.ShowImage("Threshold 2", threshold2) element_shape = cv.CV_SHAPE_RECT pos=3 element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, element_shape) cv.Dilate(grey,grey,element,2) #Replace a pixel value with the maximum value of neighboors #There is others like Erode which replace take the lowest value of the neighborhood #Note: The Structuring element is optionnal cv.ShowImage("Dilated", grey) cv.WaitKey(0)
0x02. HighGUI
OpenCV 內(nèi)建了一套簡(jiǎn)單的 GUI 工具,方便我們?cè)谔幚斫缑嫔暇帉懸恍┛丶?,?dòng)態(tài)的改變輸出:
import cv2.cv as cv im = cv.LoadImage("img/lena.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) thresholded = cv.CreateImage(cv.GetSize(im), 8, 1) def onChange(val): cv.Threshold(im, thresholded, val, 255, cv.CV_THRESH_BINARY) cv.ShowImage("Image", thresholded) # 創(chuàng)建一個(gè)滑動(dòng)條控件 onChange(100) #Call here otherwise at startup. Show nothing until we move the trackbar cv.CreateTrackbar("Thresh", "Image", 100, 255, onChange) #Threshold value arbitrarily set to 100 cv.WaitKey(0)
0x03. 選區(qū)操作
有事希望對(duì)圖像中某一塊區(qū)域進(jìn)行變換等操作,就可以使用如下方式:
import cv2.cv as cv im = cv.LoadImage("img/lena.jpg",3) # 選擇一塊區(qū)域 cv.SetImageROI(im, (50,50,150,150)) #Give the rectangle coordinate of the selected area # 變換操作 cv.Zero(im) #cv.Set(im, cv.RGB(100, 100, 100)) put the image to a given value # 解除選區(qū) cv.ResetImageROI(im) # Reset the ROI cv.ShowImage("Image",im) cv.WaitKey(0)
0x04. 運(yùn)算
對(duì)于多張圖片,我們可以進(jìn)行一些運(yùn)算操作(包括算數(shù)運(yùn)算和邏輯運(yùn)算),下面的代碼將演示一些基本的運(yùn)算操作:
import cv2.cv as cv#or simply import cv im = cv.LoadImage("img/lena.jpg") im2 = cv.LoadImage("img/fruits-larger.jpg") cv.ShowImage("Image1", im) cv.ShowImage("Image2", im2) res = cv.CreateImage(cv.GetSize(im2), 8, 3) # 加 cv.Add(im, im2, res) #Add every pixels together (black is 0 so low change and white overload anyway) cv.ShowImage("Add", res) # 減 cv.AbsDiff(im, im2, res) # Like minus for each pixel im(i) - im2(i) cv.ShowImage("AbsDiff", res) # 乘 cv.Mul(im, im2, res) #Multiplie each pixels (almost white) cv.ShowImage("Mult", res) # 除 cv.Div(im, im2, res) #Values will be low so the image will likely to be almost black cv.ShowImage("Div", res) # 與 cv.And(im, im2, res) #Bit and for every pixels cv.ShowImage("And", res) # 或 cv.Or(im, im2, res) # Bit or for every pixels cv.ShowImage("Or", res) # 非 cv.Not(im, res) # Bit not of an image cv.ShowImage("Not", res) # 異或 cv.Xor(im, im2, res) #Bit Xor cv.ShowImage("Xor", res) # 乘方 cv.Pow(im, res, 2) #Pow the each pixel with the given value cv.ShowImage("Pow", res) # 最大值 cv.Max(im, im2, res) #Maximum between two pixels #Same form Min MinS cv.ShowImage("Max",res) cv.WaitKey(0)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python Web開發(fā)模板引擎優(yōu)缺點(diǎn)總結(jié)
這篇文章主要介紹了Python Web開發(fā)模板引擎優(yōu)缺點(diǎn)總結(jié),需要的朋友可以參考下2014-05-05Python讀csv文件去掉一列后再寫入新的文件實(shí)例
下面小編就為大家分享一篇Python讀csv文件去掉一列后再寫入新的文件實(shí)例,具有很的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12由Python運(yùn)算π的值深入Python中科學(xué)計(jì)算的實(shí)現(xiàn)
這篇文章主要介紹了由Python運(yùn)算π的值深入Python中科學(xué)計(jì)算的實(shí)現(xiàn),由簡(jiǎn)單的計(jì)算發(fā)散出各種算法的講解,需要的朋友可以參考下2015-04-04用Python刪除本地目錄下某一時(shí)間點(diǎn)之前創(chuàng)建的所有文件的實(shí)例
下面小編就為大家分享一篇用Python刪除本地目錄下某一時(shí)間點(diǎn)之前創(chuàng)建的所有文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Python采用Django制作簡(jiǎn)易的知乎日?qǐng)?bào)API
這篇文章主要為大家詳細(xì)介紹了Python采用Django制作簡(jiǎn)易的知乎日?qǐng)?bào)API,感興趣的小伙伴們可以參考一下2016-08-08Python繪制數(shù)據(jù)動(dòng)態(tài)圖的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Python語(yǔ)言繪制好看的數(shù)據(jù)動(dòng)態(tài)圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下2022-07-07python通過(guò)opencv實(shí)現(xiàn)批量剪切圖片
這篇文章主要介紹了python通過(guò)opencv實(shí)現(xiàn)批量剪切圖片,還是挺不錯(cuò)的,這里分享個(gè)大家,供需要的朋友參考。2017-11-11Python基于Flask框架配置依賴包信息的項(xiàng)目遷移部署
這篇文章主要介紹了Python基于Flask框架配置依賴包信息的項(xiàng)目遷移部署小技巧,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03Python全景系列之控制流程盤點(diǎn)及進(jìn)階技巧
這篇文章主要為大家介紹了Python全景系列之控制流程盤點(diǎn)及進(jìn)階技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05