opencv模板匹配相同位置去除重復(fù)的框
使用opencv自帶的模板匹配
1、目標(biāo)匹配函數(shù):cv2.matchTemplate()
res=cv2.matchTemplate(image, templ, method, result=None, mask=None)
image:待搜索圖像
templ:模板圖像
result:匹配結(jié)果
method:計(jì)算匹配程度的方法,主要有以下幾種:
- CV_TM_SQDIFF 平方差匹配法:該方法采用平方差來進(jìn)行匹配;最好的匹配值為0;匹配越差,匹配值越大。
- CV_TM_CCORR 相關(guān)匹配法:該方法采用乘法操作;數(shù)值越大表明匹配程度越好。
- CV_TM_CCOEFF 相關(guān)系數(shù)匹配法:1表示完美的匹配;-1表示最差的匹配。
- CV_TM_SQDIFF_NORMED 計(jì)算歸一化平方差,計(jì)算出來的值越接近0,越相關(guān)
- CV_TM_CCORR_NORMED 計(jì)算歸一化相關(guān)性,計(jì)算出來的值越接近1,越相關(guān)
- CV_TM_CCOEFF_NORMED 計(jì)算歸一化相關(guān)系數(shù),計(jì)算出來的值越接近1,越相關(guān)
待檢測(cè)的圖片如下,需要檢測(cè)里面金幣的位置
需要檢測(cè)金幣的模板如下:
2、基本的多對(duì)象模板匹配效果代碼如下:
import cv2 import numpy as np img_rgb = cv2.imread('mario.jpg') img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) template = cv2.imread('mario_coin.jpg', 0) h, w = template.shape[:2] res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) threshold = 0.8 # 取匹配程度大于%80的坐標(biāo) loc = np.where(res >= threshold) #np.where返回的坐標(biāo)值(x,y)是(h,w),注意h,w的順序 for pt in zip(*loc[::-1]): bottom_right = (pt[0] + w, pt[1] + h) cv2.rectangle(img_rgb, pt, bottom_right, (0, 0, 255), 2) cv2.imwrite("001.jpg",img_rgb) cv2.imshow('img_rgb', img_rgb) cv2.waitKey(0)
檢測(cè)效果如下:
通過上圖可以看到對(duì)同一個(gè)圖有多個(gè)框標(biāo)定,需要去重,只需要保留一個(gè)
解決方案:對(duì)于使用同一個(gè)待檢區(qū)域使用NMS進(jìn)行去掉重復(fù)的矩形框
3、使用NMS對(duì)模板匹配出來的矩形框進(jìn)行去掉臨近重復(fù)的,代碼如下:
import cv2 import time import numpy as np def py_nms(dets, thresh): """Pure Python NMS baseline.""" #x1、y1、x2、y2、以及score賦值 # (x1、y1)(x2、y2)為box的左上和右下角標(biāo) x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4] #每一個(gè)候選框的面積 areas = (x2 - x1 + 1) * (y2 - y1 + 1) #order是按照score降序排序的 order = scores.argsort()[::-1] # print("order:",order) keep = [] while order.size > 0: i = order[0] keep.append(i) #計(jì)算當(dāng)前概率最大矩形框與其他矩形框的相交框的坐標(biāo),會(huì)用到numpy的broadcast機(jī)制,得到的是向量 xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) #計(jì)算相交框的面積,注意矩形框不相交時(shí)w或h算出來會(huì)是負(fù)數(shù),用0代替 w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) inter = w * h #計(jì)算重疊度IOU:重疊面積/(面積1+面積2-重疊面積) ovr = inter / (areas[i] + areas[order[1:]] - inter) #找到重疊度不高于閾值的矩形框索引 inds = np.where(ovr <= thresh)[0] # print("inds:",inds) #將order序列更新,由于前面得到的矩形框索引要比矩形框在原order序列中的索引小1,所以要把這個(gè)1加回來 order = order[inds + 1] return keep def template(img_gray,template_img,template_threshold): ''' img_gray:待檢測(cè)的灰度圖片格式 template_img:模板小圖,也是灰度化了 template_threshold:模板匹配的置信度 ''' h, w = template_img.shape[:2] res = cv2.matchTemplate(img_gray, template_img, cv2.TM_CCOEFF_NORMED) start_time = time.time() loc = np.where(res >= template_threshold)#大于模板閾值的目標(biāo)坐標(biāo) score = res[res >= template_threshold]#大于模板閾值的目標(biāo)置信度 #將模板數(shù)據(jù)坐標(biāo)進(jìn)行處理成左上角、右下角的格式 xmin = np.array(loc[1]) ymin = np.array(loc[0]) xmax = xmin+w ymax = ymin+h xmin = xmin.reshape(-1,1)#變成n行1列維度 xmax = xmax.reshape(-1,1)#變成n行1列維度 ymax = ymax.reshape(-1,1)#變成n行1列維度 ymin = ymin.reshape(-1,1)#變成n行1列維度 score = score.reshape(-1,1)#變成n行1列維度 data_hlist = [] data_hlist.append(xmin) data_hlist.append(ymin) data_hlist.append(xmax) data_hlist.append(ymax) data_hlist.append(score) data_hstack = np.hstack(data_hlist)#將xmin、ymin、xmax、yamx、scores按照列進(jìn)行拼接 thresh = 0.3#NMS里面的IOU交互比閾值 keep_dets = py_nms(data_hstack, thresh) print("nms time:",time.time() - start_time)#打印數(shù)據(jù)處理到nms運(yùn)行時(shí)間 dets = data_hstack[keep_dets]#最終的nms獲得的矩形框 return dets if __name__ == "__main__": img_rgb = cv2.imread('mario.jpg')#需要檢測(cè)的圖片 img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)#轉(zhuǎn)化成灰色 template_img = cv2.imread('mario_coin.jpg', 0)#模板小圖 template_threshold = 0.8#模板置信度 dets = template(img_gray,template_img,template_threshold) count = 0 for coord in dets: cv2.rectangle(img_rgb, (int(coord[0]),int(coord[1])), (int(coord[2]),int(coord[3])), (0, 0, 255), 2) cv2.imwrite("result.jpg",img_rgb)
檢測(cè)效果如下所示:
參考資料:
https://blog.csdn.net/qq_39507748/article/details/104598222
https://docs.opencv.org/3.4/d4/dc6/tutorial_py_template_matching.html
https://blog.csdn.net/mdjxy63/article/details/81037860
https://github.com/rbgirshick/fast-rcnn/blob/master/lib/utils/nms.py
https://www.pyimagesearch.com/2015/02/16/faster-non-maximum-suppression-python/
到此這篇關(guān)于opencv模板匹配相同位置去除重復(fù)的框的文章就介紹到這了,更多相關(guān)opencv模板匹配內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python pandas的describe函數(shù)參數(shù)示例詳解
describe()函數(shù)是pandas 中一個(gè)十分實(shí)用的工具,用于快速獲取數(shù)據(jù)集的描述性統(tǒng)計(jì)信息,本文詳細(xì)介紹了該函數(shù)的各種參數(shù)及其用法,包括控制輸出的百分位數(shù)、列類型以及是否將日期時(shí)間列視為數(shù)值型列等,感興趣的朋友一起看看吧2018-04-04python 正則表達(dá)式如何實(shí)現(xiàn)重疊匹配
這篇文章主要介紹了python 正則表達(dá)式如何實(shí)現(xiàn)重疊匹配,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07在Django中實(shí)現(xiàn)定時(shí)任務(wù)的多種方法
在 Django 項(xiàng)目中實(shí)現(xiàn)定時(shí)任務(wù)可以幫助自動(dòng)化執(zhí)行一些后臺(tái)任務(wù),如數(shù)據(jù)清理、定期報(bào)告生成等,以下是幾種常見的實(shí)現(xiàn)方式,每種方法都有其獨(dú)特的優(yōu)勢(shì)和適用場(chǎng)景,感興趣的小伙伴跟著小編一起來看看吧2024-08-08Python實(shí)現(xiàn)將目錄中TXT合并成一個(gè)大TXT文件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將目錄中TXT合并成一個(gè)大TXT文件的方法,涉及Python針對(duì)目錄下文本文件的遍歷、讀取及寫入等技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07使用python爬取連續(xù)降水?dāng)?shù)據(jù)信息實(shí)例
這篇文章主要為大家介紹了使用python提取連續(xù)降水?dāng)?shù)據(jù)信息實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01