1

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())
Pragyan
  • 567
  • 1
  • 5
  • 18
  • 1
    Can you provide the error? – Eskapp Mar 25 '19 at 15:28
  • I am not getting error ,when i am executing the last for loop,python crashes and i have to restart the system – Pragyan Mar 25 '19 at 15:50
  • 1
    I'm confused. You're trying to find the homography of each pair of points separately. You need at least 8 points to theoretically obtain a good homography. BTW, the code to obtain the keypoint locations is from: https://stackoverflow.com/questions/30716610/how-to-get-pixel-coordinates-from-feature-matching-in-opencv-python?noredirect=1&lq=1. I recognize the comments. In any case, it is most likely that many of us don't have access to the article. It would help if you summarized what you did, rather than have us read the article. Most will not do that for you. Also please see [ask]. – rayryeng Mar 25 '19 at 16:50
  • I have used SLIC superpixel segmentation and passed each segmentation to ORB to obtain key points and descriptors, sorted them and now trying to remove outliers using ransac and plot them in a single image.Yes the mentioned code is from your comment – Pragyan Mar 26 '19 at 05:21
  • The main objective is to detect copy move forgery within the image and plot the on the same image – Pragyan Mar 26 '19 at 05:34

0 Answers0