用OpenCV實現(xiàn)計算機視覺識別
OpenCV是用于計算機視覺、機器學習和圖像處理的大型開源庫,現(xiàn)在它在實時操作中發(fā)揮著重要作用,這在當今的系統(tǒng)中非常重要。通過使用它,人們可以處理圖像和視頻來識別物體、人臉,甚至人類的筆跡。本文的重點是檢測對象。
有關(guān)詳細信息,請參閱機器學習、深度學習、計算機圖像處理及知識圖譜應用與核心技術(shù)實戰(zhàn)課程中對OpenCV的講解。

目標檢測
目標檢測是一種與計算機視覺、圖像處理和深度學習相關(guān)的計算機技術(shù),用于檢測圖像和視頻中的對象實例。在本文中,我們將使用稱為haar級聯(lián)的東西來進行對象檢測。
Haar級聯(lián)分類器?? ? ? ? ? ? ? ? ? ?
Haar級聯(lián)分類器是一種有效的目標檢測方法。該方法由Paul Viola和Michael Jones在他們的論文《使用簡單特征的提升級聯(lián)快速目標檢測》中提出。Haar Cascade是一種基于機器學習的方法,其中使用大量正負圖像來訓練分類器。
正圖像–這些圖像包含我們希望分類器識別的圖像。
Negative Images–其他一切的圖像,不包含我們要檢測的對象。
下載以下需求的步驟
在終端中運行以下命令以安裝opencv。
Pip install opencv python
運行以下命令以在終端中安裝Matplotlib。
Pip ?matplotlib
注意:將XML文件和PNG圖像放在與Python腳本相同的文件夾中。
具體實施
使用的圖像:

(1)打開圖像
Python代碼:
import CV2
from matplotlib import pyplot as plt
# Opening image
img = CV2.imread("image.jpg")
# OpenCV opens images as BRG
# but we want it as RGB and
# we also need a grayscale
# version
img_gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY)
img_rgb = CV2.cvtColor(img, CV2.COLOR_BGR2RGB)
# Creates the environment
# of the picture and shows it
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()
輸出:

(2)識別
我們將使用OpenCV的detectMultiScale()函數(shù)來識別大符號和小符號:
# Use minSize because for not
# bothering with extra-small
# dots that would look like STOP signs
stop_data = CV2.CascadeClassifier('stop_data.xml')
found = stop_data.detectMultiScale(img_gray,
minSize =(20, 20))
# Don't do anything if there's
# no sign
amount_found = len(found)
if amount_found != 0:
# There may be more than one
# sign in the image
for (x, y, width, height) in found:
# We draw a green rectangle around
# every recognized sign
CV2.rectangle(img_rgb, (x, y),
(x + height, y + width),
(0, 255, 0), 5)
下面是完整腳本:
import CV2
from matplotlib import pyplot as plt
# Opening image
img = CV2.imread("image.jpg")
# OpenCV opens images as BRG
# but we want it as RGB We'll
# also need a grayscale version
img_gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY)
img_rgb = CV2.cvtColor(img, CV2.COLOR_BGR2RGB)
# Use minSize because for not
# bothering with extra-small
# dots that would look like STOP signs
stop_data = CV2.CascadeClassifier('stop_data.xml')
found = stop_data.detectMultiScale(img_gray,
minSize =(20, 20))
# Don't do anything if there's
# no sign
amount_found = len(found)
if amount_found != 0:
# There may be more than one
# sign in the image
for (x, y, width, height) in found:
# We draw a green rectangle around
# every recognized sign
CV2.rectangle(img_rgb, (x, y),
(x + height, y + width),
(0, 255, 0), 5)
# Creates the environment of
# the picture and shows it
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()
輸出:

無論你是在準備你的第一次求職面試,還是在這個不斷發(fā)展的科技景觀中追求更高的技能,中培IT學院機器學習、深度學習、計算機圖像處理及知識圖譜應用與核心技術(shù)實戰(zhàn)課程將是是你成功的關(guān)鍵。本次培訓從實戰(zhàn)的角度對深度學習技術(shù)進行了全面的剖析,并結(jié)合實際案例分析和探討深度學習的應用場景,給深度學習相關(guān)從業(yè)人員以指導和啟迪。中培IT學院累計培訓10萬+人次,期待您的加入。
