Преобразовать ключевые точки SIFT, которые были сохранены в файл CSV, в график - PullRequest
0 голосов
/ 18 октября 2019

Я хочу сопоставить два изображения, поэтому я извлек их функции, используя SIFT. Я сохранил извлеченные ключевые точки просеивания в файл CSV, а затем хочу преобразовать его в ориентированный граф для сопоставления.

Я хочу сделать хорошее сопоставление изображения между эскизом и реальным изображением:

import networkx as nx
import cv2
import numpy as np

import csv
# Load the query image
image1 = cv2.imread('C:/Users/Pc1/Desktop/0.png')
# Convert the training image to RGB
training_image = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
# Convert the training image to gray scale
training_gray = cv2.cvtColor(training_image, cv2.COLOR_RGB2GRAY)
# Load the test image
imageTest = cv2.imread('C:/Users/Pc1/Desktop/false.jpg')
# Convert the training image to RGB
Test_image = cv2.cvtColor(imageTest, cv2.COLOR_BGR2RGB)
# Convert the training image to gray scale
test_gray = cv2.cvtColor(Test_image, cv2.COLOR_RGB2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
#orb = cv2.ORB_create()
train_keypoints, train_descriptor = sift.detectAndCompute(training_gray, None)
test_keypoints, test_descriptor = sift.detectAndCompute(test_gray, None)

index = []
for point in train_keypoints:
    temp = (point.pt, point.size, point.angle, point.response, point.octave, point.class_id)
    index.append(temp)

# Write to csv
with open('data.csv', 'w') as f:
    fields = ['keypoint coordinates', 'point size', 'angle', 'response', 'octave', 'point-classId']
    w = csv.DictWriter(f, fieldnames=fields)
    w.writeheader()
    for line in index:
        w.writerow( {'keypoint coordinates':line[0],'point size':line[1], 'angle':line[2],'response':line[3],'octave':line[4],'point-classId':line[5]} )

#with open ('data.csv','r') as csv_file:
   #csv_reader = csv.reader(csv_file)
   #for lnie in csv_reader:
      #print(lnie)


Data  = open('data.csv', "r", encoding='utf8')
read = csv.reader(Data)
Graphtype = nx.DiGraph()
next(Data, None)  # skip the first line in the input file

G = nx.parse_edgelist(Data, delimiter=',', create_using=Graphtype, nodetype=int, data=(('weight', float),))

for x in G.nodes():
      print ("Node:", x, "has total #degree:",G.degree(x), " , In_degree: ", G.out_degree(x)," and out_degree: ", G.in_degree(x))
for u,v in G.edges():
      print ("Weight of Edge ("+str(u)+","+str(v)+")", G.get_edge_data(u,v))

nx.draw(G)
plt.show()
...