龍巖新聞網(wǎng)前端seo是什么意思
模板匹配
膜版匹配不能匹配尺度變換和視角變換的圖像
圖片中查找和模板相似度最高的圖像
計算相似程度最高的位置
res = cv.matchTemplate(img , template, method)
該方法返回一個類似灰度圖的東西,如果用的相關(guān)匹配,那么亮的地方就是可能匹配上的地方
img圖像template模板
method
- 平方差匹配CV_TM_SQDIFF 模板與圖像的平方差進行匹配,最好的匹配是0,匹配越差值越大
- 相關(guān)匹配CV_TM_CCORR 模板與圖像乘法進行匹配,數(shù)值越大表示匹配程度越高
- 相關(guān)系數(shù)匹配CV_TM_CCOEFF 模板與圖像相關(guān)系數(shù)匹配,1表示完美匹配,-1表示最差匹配
cv.minMaxLoc()查找最大值/最小值位置即可
該方法返回最小值,最大值,最小值位置(數(shù)列),最大值位置(數(shù)列)
img = cv.imread....
template = cv.read...
res = cv.matchTemplate(img, template, CV_TM_CCORR)
minval,maxval,minloc,maxloc = cv.minMaxLoc(res)
top_left = maxloc # 匹配位置方框的左上角就是maxloc返回的位置,因為使用的是相關(guān)匹配
h,w = template.shape[:2]
bottom_right = (top_left[0]+w,top_left[1]+h)
cv.rectangle(img,top_left,bottom_right,(0,255,0),2) #繪制方框 綠色線框?qū)挾葹?
霍夫變換
用于提取直線和圓的形狀
霍夫直線檢測
cv.HoughLines(edges,rho,theta)
edges一般為灰度且進行過canny邊緣化的灰度圖像
- rho:以像素為單位的距離精度。
- double類型的theta:以弧度為單位的角度精度
返回的是一個array型數(shù)組,每一個元素都是一組rho,theta
import matplotlib.pyplot as plt
import cv2 as cv
import numpy as npimg = cv.imread("/Users/liruiyan/Downloads/IMG_9534.jpg")
plt.subplot(2, 2, 1)
plt.title("origin")
plt.axis("off")
plt.imshow(img[:, :, ::-1])gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
plt.subplot(2, 2, 2)
plt.title("convert_gray")
plt.axis("off")
plt.imshow(gray, cmap=plt.cm.gray)edges = cv.Canny(gray, 50, 150)
plt.subplot(2, 2, 3)
plt.axis("off")
plt.title("canny_edges")
plt.imshow(gray, cmap=plt.cm.gray)lines = cv.HoughLines(edges, 0.6, np.pi/180, 250)
# 返回的lines是一個關(guān)于rho,theta的一個array,每一個[rho,theta]都是霍夫空間內(nèi)一個關(guān)于直線的描述
for line in lines:rho, theta = line[0]a = np.cos(theta)b = np.sin(theta)x0 = a*rhoy0 = b*rho# 計算延伸的直線起點和終點x1 = int(x0 + 10000*(-b))x2 = int(x0 - 10000*(-b))y1 = int(y0 + 10000 * a)y2 = int(y0 - 10000 * a)cv.line(img, (x1, y1), (x2, y2), (0, 255, 0), 10)plt.subplot(2, 2, 4)
plt.title("result")
plt.imshow(img[:, :, ::-1])
plt.axis("off")
plt.show()plt.imshow(img[:, :, ::-1])
plt.figure(figsize=(10, 8), dpi=200)
plt.show()
霍夫圓檢測
霍夫圓對噪聲比較敏感,要進行中值濾波
cv.HoughCircles(img, method ,dp, minDist, param1, param2, minRadius, maxRadius)
img:輸入圖像,灰度圖像
method :霍夫圓檢測算法:CV_HOUGH_GRADIENT
dp:霍夫空間分辨率,1表示和原圖一致,2表示為原圖一半
minDist:圓心之間最小距離 ,兩圓心如果小于該值,視為同一個圓
param1
param2
minRadius,maxRadius:要檢測的圓半徑的最小值和最大值