如何使用Python OpenCV混合圖像?
在這篇文章中,我們將提供一些使用OpenCV的示例。
在OpenCV中混合圖像我們將提供一個(gè)逐步的示例,說明如何使用Python OpenCV混合圖像。下面我們展示了目標(biāo)圖像和濾鏡圖像。
目標(biāo)圖像

濾鏡圖像

import cv2
# Two images
img1 = cv2.imread('target.jpg')
img2 = cv2.imread('filter.png')
# OpenCV expects to get BGR images, so we will convert from BGR to RGB
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
# Resize the Images. In order to blend them, the two images
# must be of the same shape
img1 =cv2.resize(img1,(620,350))
img2 =cv2.resize(img2,(620,350))
# Now, we can blend them, we need to define the weight (alpha) of the target image
# as well as the weight of the filter image
# in our case we choose 80% target and 20% filter
blended = cv2.a(chǎn)ddWeighted(src1=img1,alpha=0.8,src2=img2,beta=0.2,gamma=0)
# finally we can save the image. Now we need to convert it from RGB to BGR
cv2.imwrite('Blending.png',cv2.cvtColor(blended, cv2.COLOR_RGB2BGR))

在OpenCV中處理圖像我們將展示如何使用Python中的OpenCV應(yīng)用圖像處理。

如何模糊影像import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
img = cv2.imread('panda.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
blurred_img = cv2.blur(img,ksize=(20,20))
cv2.imwrite("blurredpanda.jpg", blurred_img)

如何申請Sobel Operator你可以在Wikipedia上查看Sobel Operator:https://en.wikipedia.org/wiki/Sobel_operator也可以開始嘗試一些過濾器。讓我們應(yīng)用水平和垂直Sobel。img = cv2.imread('panda.jpeg',0)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
cv2.imwrite("sobelx_panda.jpg", sobelx)
cv2.imwrite("sobely_panda.jpg", sobely)


如何對圖像應(yīng)用閾值我們還可以對圖像進(jìn)行二值化。img = cv2.imread('panda.jpeg',0)
ret,th1 = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
fig = plt.figure(figsize=(12,10))
plt.imshow(th1,cmap='gray')

OpenCV中的人臉檢測我們將討論如何使用OpenCV應(yīng)用人臉檢測。我們通過一個(gè)實(shí)際的可重現(xiàn)的示例來直截了當(dāng)。邏輯如下:我們從URL(或從硬盤)獲取圖像。我們將其轉(zhuǎn)換為numpy array,然后轉(zhuǎn)換為灰度。然后通過應(yīng)用適當(dāng)?shù)模狢ascadeClassifier,**我們獲得了人臉的邊界框。最后,使用PIllow(甚至是OpenCV),我們可以在初始圖像上繪制框。import cv2 as cv
import numpy as np
import PIL
from PIL import Image
import requests
from io import BytesIO
from PIL import ImageDraw
# I have commented out the cat and eye cascade. Notice that the xml files are in the opencv folder that you have downloaded and installed
# so it is good a idea to write the whole path
face_cascade = cv.CascadeClassifier('C:\opencv\build\etc\haarcascades\haarcascade_frontalface_default.xml')
#cat_cascade = cv.CascadeClassifier('C:\opencv\build\etc\haarcascades\haarcascade_frontalcatface.xml')
#eye_cascade = cv.CascadeClassifier('C:\opencv\build\etc\haarcascades\haarcascade_eye.xml')
URL = "https://images.unsplash.com/photo-1525267219888-bb077b8792cc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80"
response = requests.get(URL)
img = Image.open(BytesIO(response.content))
img_initial = img.copy()
# convert it to np array
img = np.a(chǎn)sarray(img)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray)
# And lets just print those faces out to the screen
#print(faces)
drawing=ImageDraw.Draw(img_initial)
# For each item in faces, lets surround it with a red box
for x,y,w,h in faces:
# That might be new syntax for you! Recall that faces is a list of rectangles in (x,y,w,h)
# format, that is, a list of lists. Instead of having to do an iteration and then manually
# pull out each item, we can use tuple unpacking to pull out individual items in the sublist
# directly to variables. A really nice python feature
#
# Now we just need to draw our box
drawing.rectangle((x,y,x+w,y+h), outline="red")
display(img_initial)
最初的圖像是這樣的:

然后在繪制邊界框后,我們得到:

如我們所見,我們設(shè)法正確地獲得了四個(gè)面,但是我們還發(fā)現(xiàn)了窗后的“鬼”……裁剪臉部以分離圖像我們還可以裁剪臉部以分離圖像for x,y,w,h in faces:
img_initial.crop((x,y,x+w,y+h))
display(img_initial.crop((x,y,x+w,y+h)))
例如,我們得到的第一張臉是:

注意:如果你想從硬盤讀取圖像,則只需鍵入以下三行:# read image from the PC
initial_img=Image.open('my_image.jpg')
img = cv.imread('my_image.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
OpenCV中的人臉檢測視頻這篇文章是一個(gè)實(shí)際示例,說明了我們?nèi)绾螌penCV]與Python結(jié)合使用來檢測視頻中的人臉。在上一篇文章中,我們解釋了如何在Tensorflow中應(yīng)用對象檢測和OpenCV中的人臉檢測。通常,計(jì)算機(jī)視覺和對象檢測是人工智能中的熱門話題。例如考慮自動駕駛汽車,該自動駕駛汽車必須連續(xù)檢測周圍的許多不同物體(行人,其他車輛,標(biāo)志等)。如何錄制人臉檢測視頻在以下示例中,我們將USB攝像頭應(yīng)用到人臉檢測,然后將視頻寫入.mp4文件。如你所見,OpenCV能夠檢測到面部,并且當(dāng)它隱藏在手后時(shí),OpenCV會丟失它。import cv2
# change your path to the one where the haarcascades/haarcascade_frontalface_default.xml is
face_cascade = cv2.CascadeClassifier('../DATA/haarcascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# MACOS AND LINUX: *'XVID' (MacOS users may want to try VIDX as well just in case)
# WINDOWS *'VIDX'
writer = cv2.VideoWriter('myface.mp4', cv2.VideoWriter_fourcc(*'XVID'),25, (width, height))
while True:
ret, frame = cap.read(0)
frame = detect_face(frame)
writer.write(frame)
cv2.imshow('Video Face Detection', frame)
# escape button to close it
c = cv2.waitKey(1)
if c == 27:
break
cap.release()
writer.release()
cv2.destroyAllWindows()
計(jì)算機(jī)視覺代碼的輸出只需幾行代碼即可通過動態(tài)人臉檢測錄制該視頻。如果你運(yùn)行上面的代碼塊,你將獲得類似的視頻。
發(fā)表評論
請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個(gè)字
圖片新聞
-

落地?zé)o錫!京東首個(gè)物流機(jī)器人超級工廠來了
-

OpenAI發(fā)布的AI瀏覽器,市場為何反應(yīng)強(qiáng)烈?
-

馬云重返一線督戰(zhàn),阿里重啟創(chuàng)始人模式
-

機(jī)器人奧運(yùn)會戰(zhàn)報(bào):宇樹機(jī)器人摘下首金,天工Ultra搶走首位“百米飛人”
-

存儲圈掐架!江波龍起訴佰維,索賠121萬
-

長安汽車母公司突然更名:從“中國長安”到“辰致科技”
-

豆包前負(fù)責(zé)人喬木出軌BP后續(xù):均被辭退
-

字節(jié)AI Lab負(fù)責(zé)人李航卸任后返聘,Seed進(jìn)入調(diào)整期
最新活動更多
-
即日-5.20立即下載>> 【限時(shí)免費(fèi)】物理場仿真助力生物醫(yī)學(xué)領(lǐng)域技術(shù)創(chuàng)新
-
精彩回顧立即查看>> 【直播】 智測未來·2026海克斯康春季產(chǎn)品創(chuàng)新日
-
精彩回顧立即查看>> 【線下論壇】新唐科技×芯唐南京 2026 年度研討會
-
精彩回顧立即查看>> OFweek 2026(第十五屆)中國機(jī)器人產(chǎn)業(yè)大會
-
精彩回顧立即查看>> 維科杯· OFweek 2025中國機(jī)器人行業(yè)年度評選
-
精彩回顧立即查看>> 【在線會議】液冷服務(wù)器信號完整性及冷卻液關(guān)鍵電參數(shù)測試
推薦專題
- 1 AI狂歡遇上油價(jià)破百,全球股市還能漲多久? | 產(chǎn)聯(lián)看全球
- 2 OpenAI深夜王炸!ChatGPT Images 2.0實(shí)測:中文穩(wěn)、細(xì)節(jié)炸,設(shè)計(jì)師慌了
- 3 6000億美元估值錨定:字節(jié)跳動的“去單一化”突圍與估值重構(gòu)
- 4 Tesla AI5芯片最新進(jìn)展總結(jié)
- 5 連夜測了一波DeepSeek-V4,我發(fā)現(xiàn)它可能只剩“審美”這個(gè)短板了
- 6 熱點(diǎn)丨AI“瑜亮之爭”:既生OpenClaw,何生Hermes?
- 7 AI界的殺豬盤:9秒刪庫跑路,全員被封號,還繼續(xù)扣錢!
- 8 2026,人形機(jī)器人只贏了面子
- 9 DeepSeek降價(jià)90%:價(jià)格屠夫不是身份,是戰(zhàn)略
- 10 AI Infra產(chǎn)業(yè)鏈卡在哪里了?
- 高級軟件工程師 廣東省/深圳市
- 自動化高級工程師 廣東省/深圳市
- 光器件研發(fā)工程師 福建省/福州市
- 銷售總監(jiān)(光器件) 北京市/海淀區(qū)
- 激光器高級銷售經(jīng)理 上海市/虹口區(qū)
- 光器件物理工程師 北京市/海淀區(qū)
- 激光研發(fā)工程師 北京市/昌平區(qū)
- 技術(shù)專家 廣東省/江門市
- 封裝工程師 北京市/海淀區(qū)
- 結(jié)構(gòu)工程師 廣東省/深圳市


分享





