how to draw lines for feature match within the same image

asked 2019-03-24 03:34:28 -0500

Pragyan gravatar image

updated 2019-03-24 04:11:08 -0500

image description

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()
edit retag flag offensive close merge delete

Comments

please insert your code in your question as text. Read the doc

LBerger gravatar imageLBerger ( 2019-03-24 03:38:39 -0500 )edit

In the doc they are comparing with two different image ,here i am finding matches within the same image,

Pragyan gravatar imagePragyan ( 2019-03-24 03:47:20 -0500 )edit

I found https://stackoverflow.com/questions/1... ,will it be useful in my case

Pragyan gravatar imagePragyan ( 2019-03-24 04:56:34 -0500 )edit

If it is same image use line with keypoint coordinate

LBerger gravatar imageLBerger ( 2019-03-24 08:46:43 -0500 )edit

Is keypoint coordinate and https://stackoverflow.com/questions/3... gives the same thing ?

Pragyan gravatar imagePragyan ( 2019-03-24 09:42:12 -0500 )edit
1

stackoverflow is not the doc : use the doc

LBerger gravatar imageLBerger ( 2019-03-24 11:32:32 -0500 )edit