赞
踩
SIFT 是一种在尺度和旋转上不变的特征点检测算法,常用于图像匹配。
步骤:
ORB 是一种基于 FAST 特征检测和 BRIEF 特征描述的快速特征匹配算法。
步骤:
import cv2 # 读取图像 img1 = cv2.imread('image1.jpg', 0) img2 = cv2.imread('image2.jpg', 0) # 初始化 SIFT 检测器 sift = cv2.SIFT_create() # 检测关键点和计算描述子 kp1, des1 = sift.detectAndCompute(img1, None) kp2, des2 = sift.detectAndCompute(img2, None) # 使用 BFMatcher 进行匹配 bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True) matches = bf.match(des1, des2) # 绘制匹配结果 img_matches = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) cv2.imshow('Matches', img_matches) cv2.waitKey(0) cv2.destroyAllWindows()
import cv2 # 读取图像 img1 = cv2.imread('image1.jpg', 0) img2 = cv2.imread('image2.jpg', 0) # 初始化 ORB 检测器 orb = cv2.ORB_create() # 检测关键点和计算描述子 kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) # 使用 BFMatcher 进行匹配 bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(des1, des2) # 绘制匹配结果 img_matches = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) cv2.imshow('Matches', img_matches) cv2.waitKey(0) cv2.destroyAllWindows()
余弦相似度用于计算两个向量之间的夹角余弦值,常用于文本匹配和推荐系统。
公式:
cosine_similarity
(
A
,
B
)
=
A
⋅
B
∥
A
∥
∥
B
∥
\text{cosine\_similarity}(A, B) = \frac{A \cdot B}{\|A\| \|B\|}
cosine_similarity(A,B)=∥A∥∥B∥A⋅B
皮尔逊相关系数用于度量两个变量之间的线性相关性。
公式:
r
=
∑
(
x
i
−
x
‾
)
(
y
i
−
y
‾
)
∑
(
x
i
−
x
‾
)
2
∑
(
y
i
−
y
‾
)
2
r = \frac{\sum (x_i - \overline{x})(y_i - \overline{y})}{\sqrt{\sum (x_i - \overline{x})^2 \sum (y_i - \overline{y})^2}}
r=∑(xi−x)2∑(yi−y)2
∑(xi−x)(yi−y)
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
# 定义两个向量
A = np.array([1, 2, 3])
B = np.array([4, 5, 6])
# 计算余弦相似度
cos_sim = cosine_similarity([A], [B])
print("Cosine Similarity:", cos_sim[0][0])
import numpy as np
# 定义两个向量
A = np.array([1, 2, 3])
B = np.array([4, 5, 6])
# 计算皮尔逊相关系数
pearson_corr = np.corrcoef(A, B)[0, 1]
print("Pearson Correlation Coefficient:", pearson_corr)
欧几里得距离是最常见的距离度量方法,计算两个点之间的直线距离。
公式:
d
(
A
,
B
)
=
∑
i
=
1
n
(
A
i
−
B
i
)
2
d(A, B) = \sqrt{\sum_{i=1}^{n} (A_i - B_i)^2}
d(A,B)=i=1∑n(Ai−Bi)2
曼哈顿距离计算两个点在所有坐标轴上的距离之和。
公式:
d
(
A
,
B
)
=
∑
i
=
1
n
∣
A
i
−
B
i
∣
d(A, B) = \sum_{i=1}^{n} |A_i - B_i|
d(A,B)=i=1∑n∣Ai−Bi∣
import numpy as np
# 定义两个点
A = np.array([1, 2, 3])
B = np.array([4, 5, 6])
# 计算欧几里得距离
euclidean_dist = np.linalg.norm(A - B)
print("Euclidean Distance:", euclidean_dist)
import numpy as np
# 定义两个点
A = np.array([1, 2, 3])
B = np.array([4, 5, 6])
# 计算曼哈顿距离
manhattan_dist = np.sum(np.abs(A - B))
print("Manhattan Distance:", manhattan_dist)
Siamese 网络使用两个相同的神经网络结构来提取输入对的特征,通过度量特征向量之间的距离进行匹配。
步骤:
Triplet Loss 用于训练模型,使得正样本对之间的距离小于负样本对之间的距离。
公式:
L
=
max
(
0
,
d
(
a
,
p
)
−
d
(
a
,
n
)
+
α
)
L = \max(0, d(a, p) - d(a, n) + \alpha)
L=max(0,d(a,p)−d(a,n)+α)
其中,
a
a
a 为 anchor,
p
p
p 为正样本,
n
n
n 为负样本,
α
\alpha
α 为边距。
import tensorflow as tf from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense, Lambda from tensorflow.keras.models import Model from tensorflow.keras import backend as K import numpy as np def build_siamese_model(input_shape): input = Input(shape=input_shape) x = Conv2D(64, (10, 10), activation='relu')(input) x = MaxPooling2D((2, 2))(x) x = Conv2D(128, (7, 7), activation='relu')(x) x = MaxPooling2D((2, 2))(x) x = Conv2D(128, (4, 4), activation='relu')(x) x = MaxPooling2D((2, 2))(x) x = Conv2D(256, (4, 4), activation='relu')(x) x = Flatten()(x) x = Dense(4096, activation='sigmoid')(x) return Model(input, x) def euclidean_distance(vects): x, y = vects sum_square = K.sum(K.square(x - y), axis=1, keepdims=True) return K.sqrt(K.maximum(sum_square, K.epsilon())) input_shape = (105, 105, 1) left_input = Input(shape=input_shape) right_input = Input(shape=input_shape) siamese_model = build_siamese_model(input_shape) encoded_l = siamese_model(left_input) encoded_r = siamese_model(right_input) distance = Lambda(euclidean_distance, output_shape=lambda x: (x[0], 1))([encoded_l, encoded_r]) model = Model([left_input, right_input], distance) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() # Dummy data for demonstration data1 = np.random.random((10, 105, 105, 1)) data2 = np.random.random((10, 105, 105, 1)) labels = np.random.randint(2, size=(10, 1)) model.fit([data1, data2], labels, epochs=5)
import tensorflow as tf from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense from tensorflow.keras.models import Model from tensorflow.keras import backend as K def build_embedding_model(input_shape): input = Input(shape=input_shape) x = Conv2D(64, (3, 3), activation='relu')(input) x = MaxPooling2D((2, 2))(x) x = Conv2D(128, (3, 3), activation='relu')(x) x = MaxPooling2D((2, 2))(x) x = Flatten()(x) x = Dense(128, activation='sigmoid')(x) return Model(input, x) def triplet_loss(y_true, y_pred, alpha=0.2): anchor, positive, negative = y_pred[:, 0], y_pred[:, 1], y_pred[:, 2] pos_dist = K.sum(K.square(anchor - positive), axis=1) neg_dist = K.sum(K.square(anchor - negative), axis=1) loss = K.maximum(pos_dist - neg_dist + alpha, 0.0) return K.mean(loss) input_shape = (28, 28, 1) anchor_input = Input(shape=input_shape) positive_input = Input(shape=input_shape) negative_input = Input(shape=input_shape) embedding_model = build_embedding_model(input_shape) encoded_anchor = embedding_model(anchor_input) encoded_positive = embedding_model(positive_input) encoded_negative = embedding_model(negative_input) merged_output = tf.stack([encoded_anchor, encoded_positive, encoded_negative], axis=1) model = Model([anchor_input, positive_input, negative_input], merged_output) model.compile(loss=triplet_loss, optimizer='adam') model.summary() # Dummy data for demonstration data_anchor = np.random.random((10, 28, 28, 1)) data_positive = np.random.random((10, 28, 28, 1)) data_negative = np.random.random((10, 28, 28, 1)) labels = np.random.random((10, 1)) model.fit([data_anchor, data_positive, data_negative], labels, epochs=5)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。