Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to apply Ransac on segments and plot them

I am trying to implement https://rd.springer.com/chapter/10.1007/978-3-319-68505-2_6 , but facing issues to filter and plot the results.I am using https://docs.opencv.org/3.4.2/d1/de0/tutorial_py_feature_homography.html to filter the matches.When i am running cv2.findHomography(sr[i], de[i], cv2.RANSAC,5.0) ,python crashes everytime.

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
# load the image and convert it to a floating point data type
image = cv2.imread('C:\\Users\\pragyan.prakash\\Desktop\\p_orb\\001_F.png')
img1=image, cv2.COLOR_BGR2GRAY


segments = slic(img_as_float(image), n_segments = 100, sigma = 5)

# show the output of SLIC
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
    #print ("[x] inspecting segment %d" % (i))
    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)

# Initiate ORB detector
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))


list_kp1=[]
list_kp2=[]
sr=[]
de=[]

for m in sorted_match:
    for e in m:
        img1_idx = e.queryIdx
        img2_idx = e.trainIdx

        # x - columns
        # y - rows
        # Get the coordinates
        i = sorted_match.index(m)
        p,q = pairs[i]


        (x1,y1) = key[p][img1_idx].pt
        (x2,y2) = key[q][img2_idx].pt
        sr.append(np.float32((x1,y1)).reshape(-1,1,2))
        de.append(np.float32((x2,y2)).reshape(-1,1,2))

        # Append to each list
        x1=int(x1)
        x2=int(x2)
        y1=int(y1)
        y2=int(y2)
        list_kp1.append((x1, y1))
        list_kp2.append((x2, y2))

lst=[]
ls1=[]
for i in range(len(sr)):
    lst.append(cv2.findHomography(sr[i], de[i], cv2.RANSAC,5.0))
    ls1.append(mask.ravel().tolist())