python?opencv背景減去法摳圖實(shí)現(xiàn)示例
導(dǎo)包
import cv2 from matplotlib import pyplot as plt import numpy as np
導(dǎo)圖
imA=cv2.imread("target.png")
plt.imshow(cv2.cvtColor(imA,cv2.COLOR_BGR2RGB))
imA.shape #(2436, 1125, 3)

imBG=cv2.imread("bg_30061.jpg")
plt.imshow(cv2.cvtColor(imBG,cv2.COLOR_BGR2RGB))

預(yù)處理
# Step 1 預(yù)處理
# 日??s放圖像,背景圖要縮放到和圖A意一樣大
imBG=cv2.resize(imBG,(1125,2436),interpolation=cv2.INTER_CUBIC)
# 后續(xù)代碼都是基于灰度圖像操作的
imA_gray=cv2.cvtColor(imA,cv2.COLOR_BGR2GRAY)
imBG_gray=cv2.cvtColor(imBG,cv2.COLOR_BGR2GRAY)
# 背景減去
sub=imBG_gray.astype("int32")-imA_gray.astype("int32")
sub=np.absolute(sub).astype("uint8")
plt.imshow(cv2.cvtColor(sub,cv2.COLOR_BGR2RGB))

二值化圖像
# Step 2 二值化圖像 thresh=cv2.adaptiveThreshold(sub,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,19,5) ## 圖像二值化有很多種方法, adaptiveThreshold 是基于局部的,最后兩個參數(shù),19,5 決定了邊緣檢測的清晰度 thresh=cv2.erode(thresh,None,iterations=2) thresh=cv2.dilate(thresh,None,iterations=2) # 膨脹腐蝕,主要是消去噪聲點(diǎn),同時邊緣封閉(邊緣不封閉,無法完全填充) plt.figure(num="thresh") plt.imshow(cv2.cvtColor(thresh,cv2.COLOR_BGR2RGB)) plt.close

邊緣檢測
# Step 3 邊緣檢測
cnts,hierarchy=cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
len(cnts)
area=[]
for i in cnts:
cnt_area = cv2.contourArea(i)
area.append(cnt_area)
# 通過面積大小,找到需要的邊緣的 index,這里為 866
mask=np.zeros([2436,1125],dtype=np.uint8)
mask[:,:]=255
res=cv2.drawContours(mask,cnts,866,(0,0,225),2)
plt.imshow(cv2.cvtColor(res,cv2.COLOR_BGR2RGB))
# 畫出邊緣

填充輪廓并制作掩模
# 填充輪廓并制作掩模 mask2=np.zeros([2436,1125],dtype=np.uint8) mask2[:,:]=0 res2=cv2.drawContours(mask2,cnts,866,255,cv2.FILLED) plt.imshow(cv2.cvtColor(res2,cv2.COLOR_BGR2RGB))

保存
# 保存為 png
h,w,c=imA.shape
b,g,r=cv2.split(imA)
imA_2=np.zeros((4,h,w),dtype=imA.dtype)
imA_2[0][0:h,0:w]=b
imA_2[1][0:h,0:w]=g
imA_2[2][0:h,0:w]=r
imA_2[3][0:h,0:w]=mask2
imA_new=cv2.merge(imA_2)
cv2.imwrite("imA_new.png",imA_new)

以上就是python opencv背景減去法摳圖實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于python opencv背景減去法摳圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Python中圖像邊緣檢測算法的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了python中圖像邊緣檢測算法的原理及實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
python使用post提交數(shù)據(jù)到遠(yuǎn)程url的方法
這篇文章主要介紹了python使用post提交數(shù)據(jù)到遠(yuǎn)程url的方法,涉及Python使用post傳遞數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04
使用python實(shí)現(xiàn)將視頻中的音頻分離出來
這篇文章主要介紹了使用python實(shí)現(xiàn)將視頻中的音頻分離出來,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
對Tensorflow中tensorboard日志的生成與顯示詳解
今天小編就為大家分享一篇對Tensorflow中tensorboard日志的生成與顯示詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python實(shí)現(xiàn)線性插值和三次樣條插值的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)線性插值和三次樣條插值的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Django命名URL和反向解析URL實(shí)現(xiàn)解析
這篇文章主要介紹了Django命名URL和反向解析URL實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
手機(jī)使用python操作圖片文件(pydroid3)過程詳解
這篇文章主要介紹了手機(jī)使用python操作圖片文件(pydroid3)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09

