Python+OpenCV實(shí)現(xiàn)圖像識(shí)別替換功能詳解
OpenCV-Python是一個(gè)Python庫(kù),旨在解決計(jì)算機(jī)視覺(jué)問(wèn)題。
OpenCV是一個(gè)開(kāi)源的計(jì)算機(jī)視覺(jué)庫(kù),1999年由英特爾的Gary Bradski啟動(dòng)。Bradski在訪(fǎng)學(xué)過(guò)程中注意到,在很多優(yōu)秀大學(xué)的實(shí)驗(yàn)室中,都有非常完備的內(nèi)部公開(kāi)的計(jì)算機(jī)視覺(jué)接口。這些接口從一屆學(xué)生傳到另一屆學(xué)生,對(duì)于剛?cè)腴T(mén)的新人來(lái)說(shuō),使用這些接口比重復(fù)造輪子方便多了。這些接口可以讓他們?cè)谥暗幕A(chǔ)上更有效地開(kāi)展工作。OpenCV正是基于為計(jì)算機(jī)視覺(jué)提供通用接口這一目標(biāo)而被策劃的。
安裝opencv
pip3 install -i https://pypi.doubanio.com/simple/ opencv-python
思路:
1、首先區(qū)分三張圖片:
base圖片代表初始化圖片;
template圖片代表需要在大圖中匹配的圖片;
white圖片為需要替換的圖片。



2、然后template圖片逐像素縮小匹配,設(shè)定閾值,匹配度到達(dá)閾值的圖片,判定為在初始圖片中;否則忽略掉。
3、匹配到最大閾值的地方,返回該區(qū)域的位置(x,y)
4、然后用white圖片resize到相應(yīng)的大小,填補(bǔ)到目標(biāo)區(qū)域。
match函數(shù):
"""檢查模板圖片中是否包含目標(biāo)圖片"""
def make_cv2(photo1, photo2):
global x, y, w, h, num_1,flag
starttime = datetime.datetime.now()
#讀取base圖片
img_rgb = cv2.imread(f'{photo1}')
#讀取template圖片
template = cv2.imread(f'{photo2}')
h, w = template.shape[:-1]
print('初始寬高', h, w)
res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
print('初始最大相似度', res.max())
threshold = res.max()
""",相似度小于0.2的,不予考慮;相似度在[0.2-0.75]之間的,逐漸縮小圖片"""
print(threshold)
while threshold >= 0.1 and threshold <= 0.83:
if w >= 20 and h >= 20:
w = w - 1
h = h - 1
template = cv2.resize(
template, (w, h), interpolation=cv2.INTER_CUBIC)
res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
threshold = res.max()
print('寬度:', w, '高度:', h, '相似度:', threshold)
else:
break
"""達(dá)到0.75覆蓋之前的圖片"""
if threshold > 0.8:
loc = np.where(res >= threshold)
x = int(loc[1])
y = int(loc[0])
print('覆蓋圖片左上角坐標(biāo):', x, y)
for pt in zip(*loc[::-1]):
cv2.rectangle(
img_rgb, pt, (pt[0] + w, pt[1] + h), (255, 144, 51), 1)
num_1 += 1
endtime = datetime.datetime.now()
print("耗時(shí):", endtime - starttime)
overlay_transparent(x, y, photo1, photo3)
else:
flag = Falsereplace函數(shù):
"""將目標(biāo)圖片鑲嵌到指定坐標(biāo)位置"""
def overlay_transparent(x, y, photo1, photo3):
#覆蓋圖片的時(shí)候上下移動(dòng)的像素空間
y += 4
global w, h, num_2
background = cv2.imread(f'{photo1}')
overlay = cv2.imread(f'{photo3}')
"""縮放圖片大小"""
overlay = cv2.resize(overlay, (w, h), interpolation=cv2.INTER_CUBIC)
background_width = background.shape[1]
background_height = background.shape[0]
if x >= background_width or y >= background_height:
return background
h, w = overlay.shape[0], overlay.shape[1]
if x + w > background_width:
w = background_width - x
overlay = overlay[:, :w]
if y + h > background_height:
h = background_height - y
overlay = overlay[:h]
if overlay.shape[2] < 4:
overlay = np.concatenate([overlay, np.ones((overlay.shape[0], overlay.shape[1], 1), dtype=overlay.dtype) * 255],axis=2,)
overlay_image = overlay[..., :3]
mask = overlay[..., 3:] / 255.0
background[y:y + h,x:x + w] = (1.0 - mask) * background[y:y + h,x:x + w] + mask * overlay_image
# path = 'result'
path = ''
cv2.imwrite(os.path.join(path, f'1.png'), background)
num_2 += 1
print('插入成功。')
init()每次執(zhí)行需要初始化x,y(圖片匹配初始位置參數(shù)),w,h(圖片縮放初始寬高)
x = 0
y = 0
w = 0
h = 0
flag = True
threshold = 0
template = ''
num_1 = 0
num_2 = 0
photo3 = ''
"""參數(shù)初始化"""
def init():
global x, y, w, h, threshold, template,flag
x = 0
y = 0
w = 0
h = 0
threshold = 0
template = ''
完整代碼
import cv2
import datetime
import os
import numpy as np
x = 0
y = 0
w = 0
h = 0
flag = True
threshold = 0
template = ''
num_1 = 0
num_2 = 0
photo3 = ''
"""參數(shù)初始化"""
def init():
global x, y, w, h, threshold, template,flag
x = 0
y = 0
w = 0
h = 0
threshold = 0
template = ''
"""檢查模板圖片中是否包含目標(biāo)圖片"""
def make_cv2(photo1, photo2):
global x, y, w, h, num_1,flag
starttime = datetime.datetime.now()
img_rgb = cv2.imread(f'{photo1}')
template = cv2.imread(f'{photo2}')
h, w = template.shape[:-1]
print('初始寬高', h, w)
res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
print('初始最大相似度', res.max())
threshold = res.max()
""",相似度小于0.2的,不予考慮;相似度在[0.2-0.75]之間的,逐漸縮小圖片"""
print(threshold)
while threshold >= 0.1 and threshold <= 0.83:
if w >= 20 and h >= 20:
w = w - 1
h = h - 1
template = cv2.resize(
template, (w, h), interpolation=cv2.INTER_CUBIC)
res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
threshold = res.max()
print('寬度:', w, '高度:', h, '相似度:', threshold)
else:
break
"""達(dá)到0.75覆蓋之前的圖片"""
if threshold > 0.8:
loc = np.where(res >= threshold)
x = int(loc[1])
y = int(loc[0])
print('覆蓋圖片左上角坐標(biāo):', x, y)
for pt in zip(*loc[::-1]):
cv2.rectangle(
img_rgb, pt, (pt[0] + w, pt[1] + h), (255, 144, 51), 1)
num_1 += 1
endtime = datetime.datetime.now()
print("耗時(shí):", endtime - starttime)
overlay_transparent(x, y, photo1, photo3)
else:
flag = False
"""將目標(biāo)圖片鑲嵌到指定坐標(biāo)位置"""
def overlay_transparent(x, y, photo1, photo3):
y += 0
global w, h, num_2
background = cv2.imread(f'{photo1}')
overlay = cv2.imread(f'{photo3}')
"""縮放圖片大小"""
overlay = cv2.resize(overlay, (w, h), interpolation=cv2.INTER_CUBIC)
background_width = background.shape[1]
background_height = background.shape[0]
if x >= background_width or y >= background_height:
return background
h, w = overlay.shape[0], overlay.shape[1]
if x + w > background_width:
w = background_width - x
overlay = overlay[:, :w]
if y + h > background_height:
h = background_height - y
overlay = overlay[:h]
if overlay.shape[2] < 4:
overlay = np.concatenate([overlay, np.ones((overlay.shape[0], overlay.shape[1], 1), dtype=overlay.dtype) * 255],axis=2,)
overlay_image = overlay[..., :3]
mask = overlay[..., 3:] / 255.0
background[y:y + h,x:x + w] = (1.0 - mask) * background[y:y + h,x:x + w] + mask * overlay_image
# path = 'result'
path = ''
cv2.imwrite(os.path.join(path, f'1.png'), background)
num_2 += 1
print('插入成功。')
init()
if __name__ == "__main__":
photo1 = "1.png"
photo2 = "3.png"
photo3 = "white.png"
while flag == True:
make_cv2(photo1, photo2)
overlay_transparent(x, y, photo1, photo3)執(zhí)行結(jié)果:

到此這篇關(guān)于Python+OpenCV實(shí)現(xiàn)圖像識(shí)別替換功能詳解的文章就介紹到這了,更多相關(guān)Python OpenCV圖像識(shí)別替換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Tensorflow 2.4 搭建單層和多層 Bi-LSTM 模型
這篇文章主要為大家介紹了Tensorflow 2.4 搭建單層 Bi-LSTM 模型和多層 Bi-LSTM 模型的實(shí)現(xiàn)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
pandas.DataFrame的for循環(huán)迭代的實(shí)現(xiàn)
本文主要介紹了pandas.DataFrame的for循環(huán)迭代的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python編程之基于概率論的分類(lèi)方法:樸素貝葉斯
這篇文章主要介紹了Python編程之基于概率論的分類(lèi)方法:樸素貝葉斯,簡(jiǎn)單介紹了其概述,貝葉斯理論和條件概率,以及樸素貝葉斯的原理等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
python實(shí)現(xiàn)生成字符串大小寫(xiě)字母和數(shù)字的各種組合
這篇文章主要給大家介紹了關(guān)于python生成各種字符串的方法實(shí)例,給大家提供些思路,拋磚引玉,希望大家能夠喜歡2019-01-01
Python爬蟲(chóng)爬取全球疫情數(shù)據(jù)并存儲(chǔ)到mysql數(shù)據(jù)庫(kù)的步驟
這篇文章主要介紹了Python爬蟲(chóng)爬取全球疫情數(shù)據(jù)并存儲(chǔ)到mysql數(shù)據(jù)庫(kù)的步驟,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03
Python利用pywin32庫(kù)實(shí)現(xiàn)將PPT導(dǎo)出為高清圖片
這篇文章主要為大家詳細(xì)介紹了Python如何利用pywin32庫(kù)實(shí)現(xiàn)將PPT導(dǎo)出為高清圖片的功能,文中的示例代講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01

