我发现How to get pixel coordinates from Feature Matching in OpenCV Python可以提取匹配的特征坐标,而How to draw a line on an image in OpenCV?可以画出两个点的黑白线,如附图所示。我没有在我的情况下使用此方法。
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from skimage import io
import matplotlib.pyplot as plt
import cv2
import numpy as np
image=cv2.imread('C:\\Users\\pragyan.prakash\\001_F.png')
segments = slic(img_as_float(image), n_segments = 100, sigma = 5)
fig = plt.figure("Superpixels")
ax = fig.add_subplot(1, 1, 1)
ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(image,
cv2.COLOR_BGR2RGB)), segments))
plt.axis("off")
plt.show()
feature=[]
# loop over the unique segment values
for (i, segVal) in enumerate(np.unique(segments)):
# construct a mask for the segment
mask = np.zeros(image.shape[:2], dtype = "uint8")
mask[segments == segVal] = 255
img=cv2.bitwise_and(image, image, mask = mask)
feature.append(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
cv2.waitKey(0)
orb = cv2.ORB_create()
key=[];desc=[];sel= [];
for i in feature:
kp,des=orb.detectAndCompute(i,None)
if des is not None:
key.append(kp)
desc.append(des)
sel.append(i)
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
match=[];pairs = [];
for i in range(len(desc)):
for j in range(len(desc)):
#print (i,j)
if i!=j:
match.append(bf.match(desc[i],desc[j]))
pairs.append((i,j))
sorted_match=[]
for i in match:
sorted_match.append(sorted(i,key=lambda x:x.distance))