Python opencv操作深入詳解
直接讀取圖片
def display_img(file="p.jpeg"):
img = cv.imread(file)
print (img.shape)
cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()
讀取灰度圖片
def display_gray_img(file="p.jpeg"):
img = cv.imread(file,cv.IMREAD_GRAYSCALE)
print (img.shape)
cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()
cv.imwrite("gray_img.png",img)
讀取視頻
def display_video(file="sj.mp4"):
v = cv.VideoCapture(file)
if v.isOpened():
open,frame = v.read()
else:
open=False
while open:
ret,frame = v.read()
if frame is None:
break
if ret == True:
gray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
cv.imshow("result",gray)
if cv.waitKey(10) & 0xFF == 27:
break
v.release()
v.waitKey(0)
v.destroyAllWindows()
截取圖片
def get_frame_img(file="p.jpeg"):
img = cv.imread(file)
print (img.shape)
cat = img[0:200,0:200]
cv.imshow('get_frame_img',cat)
cv.waitKey(0)
cv.destroyAllWindows()
提取rgb通道
def extrats_rgb_img(file="p.jpeg"):
img = cv.imread(file)
b,g,r = cv.split(img)
print (b.shape,g.shape,r.shape)
new_img = cv.merge((b,g,r))
print (new_img.shape)
copy_img_r = img.copy()
copy_img_r[:,:,0]=0
copy_img_r[:,:,1]=0
cv.imshow("r_img",copy_img_r)
copy_img_g = img.copy()
copy_img_g[:,:,0]=0
copy_img_g[:,:,2]=0
cv.imshow("g_img",copy_img_g)
copy_img_b = img.copy()
copy_img_b[:,:,1]=0
copy_img_b[:,:,2]=0
cv.imshow("b_img",copy_img_b)
邊界填充
def border_fill_img(file="p.jpeg"):
border_type = [
cv.BORDER_REPLICATE,#復(fù)制法,復(fù)制邊緣
cv.BORDER_REFLECT, #反射法,對(duì)感興趣的圖像中的像素在兩邊進(jìn)行復(fù)制
cv.BORDER_REFLECT_101,#反射法,以邊緣像素為軸,對(duì)稱(chēng)
cv.BORDER_WRAP,#外包裝法
cv.BORDER_CONSTANT#常量法,常量填充
]
border_title = [
"REPLICATE",
"REFLECT",
"REFLECT_101",
"WRAP",
"CONSTANT"
]
img = cv.imread(file)
top_size,bottom_size,left_size,right_size = (50,50,50,50)
plt.subplot(231)
plt.imshow(img,"gray")#原始圖像
plt.title("ORIGNAL")
for i in range(len(border_type)):
result = cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,border_type[i])
plt.subplot(232+i)
plt.imshow(result,"gray")
plt.title(border_title[i])
plt.show()

圖像融合,變換
def img_compose(file1="tu.jpeg",file2="gui.jpeg"): img_1 = cv.imread(file1) img_2 = cv.imread(file2) print (img_1.shape) print (img_2.shape) img_1= cv.resize(img_1,(500,500)) img_2= cv.resize(img_2,(500,500)) print (img_1.shape) print (img_2.shape) res = cv.addWeighted(img_1,0.4,img_2,0.6,0) plt.imshow(res) plt.show() res = cv.resize(img_1,(0,0),fx=3,fy=1) plt.imshow(res) plt.show() res = cv.resize(img_2,(0,0),fx=1,fy=3) plt.imshow(res) plt.show()

二值化處理
def Binarization(filepath):
img = cv2.imread(filepath,0)
limit = 120
ret,thresh=cv2.threshold(img,limit,255,cv2.THRESH_BINARY_INV)
plt.imshow(thresh,'gray')
plt.show()
return thresh
Binarization('t1.jpg')
到此這篇關(guān)于Python opencv操作深入詳解的文章就介紹到這了,更多相關(guān)Python opencv操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 通過(guò)正則表達(dá)式快速獲取電影的下載地址
這篇文章主要介紹了Python 通過(guò)正則表達(dá)式快速獲取電影的下載地址,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python使用pypandoc將markdown文件和LaTex公式轉(zhuǎn)為word
pypandoc 是一個(gè)用于 pandoc 的輕量級(jí) Python 包裝器,支持多種格式的文檔轉(zhuǎn)換,下面我們來(lái)看看如何使用pypandoc將markdown文件和LaTex公式轉(zhuǎn)為word吧2025-04-04
Python大批量寫(xiě)入數(shù)據(jù)(百萬(wàn)級(jí)別)的方法
這篇文章主要給大家介紹了關(guān)于Python大批量寫(xiě)入數(shù)據(jù)(百萬(wàn)級(jí)別)的相關(guān)資料,在日常處理數(shù)據(jù)的過(guò)程中,我們都有批量寫(xiě)入數(shù)據(jù)的需求,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考下2023-07-07
Python接口自動(dòng)化淺析Token應(yīng)用原理
本文主要介紹token基本概念、運(yùn)行原理及在自動(dòng)化中接口如何攜帶token進(jìn)行訪問(wèn),附含源碼,內(nèi)容非常詳細(xì)易理解,有需要的朋友可以參考下2021-08-08
Python 異步協(xié)程函數(shù)原理及實(shí)例詳解
這篇文章主要介紹了Python 異步協(xié)程函數(shù)原理及實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Python連接Oracle之環(huán)境配置、實(shí)例代碼及報(bào)錯(cuò)解決方法詳解
這篇文章主要介紹了Python連接Oracle之環(huán)境配置、實(shí)例代碼及報(bào)錯(cuò)解決方法詳解,需要的朋友可以參考下2020-02-02
單身狗福利?Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)
今天我就當(dāng)回媒婆,給男性程序員來(lái)點(diǎn)福利.今天目標(biāo)爬取征婚網(wǎng)上呈現(xiàn)出來(lái)的女生信息保存成excel表格供大家篩選心儀的女生,需要的朋友可以參考下2021-06-06
Django中reverse反轉(zhuǎn)并且傳遞參數(shù)的方法
今天小編就為大家分享一篇Django中reverse反轉(zhuǎn)并且傳遞參數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08

