how to draw lines for feature match within the same image
Is it possible to draw lines similar to the lines in the pic using cv2.drawMatches()? I have used SLIC superpixel segmentation and passed each segmentation to ORB to obtain key points and descriptors, sorted them and now trying to plot them in a single image.
Any help is highly appreciated.
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\\Desktop\\p_orb\\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))
img3 = cv2.drawMatches(image,key[0],image,key[1],sorted_match[0],None, flags=2)
plt.imshow(img3),plt.show()