基於Python OpenCV的單目標輪廓匹配

2021-09-29 14:34:11 字數 2615 閱讀 6891

python opencv 實現單目標餐盤輪廓識別 

import cv2

import numpy as np

def template(template):

temp = cv2.imread(template, 0)

# temp_gray = cv2.cvtcolor(temp, cv2.color_bgr2gray)

_, temp_thresh = cv2.threshold(temp, 127, 255, 0)

plate_contours, temp_hierarchy = cv2.findcontours(temp_thresh, 2, 1)

return plate_contours, temp_hierarchy

def matchtemp(img):

# 模版特徵

templates_contours =

fang_contours, temp_hierarchy = template('./pic/temp_fang1.png')

hua_contours, temp_hierarchy = template('./pic/temp_hua.png')

circle_contours, temp_hierarchy = template('./pic/temp_circle.png')

long_contours, temp_hierarchy = template('./pic/temp_long5.png')

templates_contours = [fang_contours[0], hua_contours[0], circle_contours[0]]

# print(fang_contours[0])

# templates_contours = sorted(templates_contours, key=lambda x: x[0])

# cv2.drawcontours(temp, hua_contours, 0, (0, 0, 255), 2) # 花盤子

# cv2.drawcontours(temp, fang_contours, 1, (0, 255, 0), 2) # 方盤子

# 盤子特徵

img = cv2.imread(img)

img_gray = cv2.cvtcolor(img, cv2.color_bgr2gray)

_, img_thresh = cv2.threshold(img_gray, 150, 255, 0)

# 腐蝕 >> 膨脹

kernel_1 = np.ones((5, 5), np.uint8)

kernel_2 = np.ones((5, 5), np.uint8)

img_erosion = cv2.erode(img_thresh, kernel_1, iterations=1)

img_dilation = cv2.dilate(img_erosion, kernel_2, iterations=1)

img_contours, img_hierarchy = cv2.findcontours(img_dilation, 2, 1)

img_contours_list =

for img_contour in img_contours:

# if cv2.contourarea(img_contour) > 500:

# 獲取最大

img_contour_area = max(img_contours_list)

i = img_contours_list.index(img_contour_area)

ret1 = cv2.matchshapes(hua_contours[0], img_contours[i], 1, 0)

ret2 = cv2.matchshapes(fang_contours[0], img_contours[i], 1, 0)

ret3 = cv2.matchshapes(circle_contours[0], img_contours[i], 1, 0)

ret4 = cv2.matchshapes(long_contours[0], img_contours[i], 1, 0)

print(ret4)

ret = min(ret1, ret2, ret3, ret4)

if ret > 0.03:

# 無匹配

plate_type = "無"

else:

if ret1 == ret:

# 匹配花盤子

plate_type = "花盤子"

elif ret2 == ret:

# 匹配方盤子

plate_type = "方盤子"

elif ret4 == ret:

# 匹配方盤子

plate_type = "長盤子"

else:

plate_type = "圓盤子"

return plate_type, ret

if __name__ == '__main__':

plate_type, ret = matchtemp("./pic/fangcai.png")

print(plate_type, ret)

python opencv 基於SSD的人臉檢測

ssd ssd是一種基於深度學習的目標檢測演算法,opencv在3.3版本以後將其引入作為基於深度學習的人臉檢測器 opencv實現的ssd人臉檢測器的骨幹網路是resnet 10,當前它提供了兩個訓練好的模型 基於深度學習框架caffe訓練的模型和基於tensorflow訓練的模型 face de...

基於python OpenCV的車牌號碼識別

基於python opencv的車牌號碼識別 車牌識別行業已具備一定的市場規模,在電子警察 公路卡口 停車場 商業管理 汽修服務等領域已取得了部分應用。乙個典型的車輛牌照識別系統一般包括以下4個部分 車輛影象獲取 車牌定位 車牌字元分割和車牌字元識別 1 車牌定位的主要工作是從獲取的車輛影象中找到汽...

Python Opencv的環境配置

安裝好anaconda後,我們利用anaconda建立虛擬環境 接下來,我們將在cmd中進行操作 在cmd中輸入 condarc系統會自動開啟condarc檔案 然後執行conda clean i清楚快取 換源完成 在cmd中輸入以下 後回車 conda create n py27test pyth...