Home > Blockchain >  Feature extraction and Matching with Cuda/Opencv/Python
Feature extraction and Matching with Cuda/Opencv/Python

Time:10-14

I share a working code of feature extraction and matching using ORB, my problem is that I need to make it work with more than two images could someone help me ? ()

from distutils.command.upload import upload
import cv2
import numpy as np

GOOD_MATCH_PERCENT = 0.15
max_features = 9000
orb = cv2.cuda_ORB.create(max_features)
matcherGPU = cv2.cuda.DescriptorMatcher_createBFMatcher(cv2.NORM_HAMMING)
# Read the images as normal
npMat1 = cv2.imread("C:\Users\Geddy\Desktop\0.png")
npMat2 = cv2.imread("C:\Users\Geddy\Desktop\1.png")#cv2.imread("path_to_reference_image")

# Load the images onto the GPU
cuMat1 = cv2.cuda_GpuMat()
cuMat2 = cv2.cuda_GpuMat()
cuMat1.upload(npMat1)
cuMat2.upload(npMat2)

# Convert the color on the GPU
cuMat1 = cv2.cuda.cvtColor(cuMat1, cv2.COLOR_BGR2GRAY)
cuMat2 = cv2.cuda.cvtColor(cuMat2, cv2.COLOR_BGR2GRAY)
 
# Create the CUDA ORB detector and detect keypoints/descriptors
kps1, descs1 = orb.detectAndComputeAsync(cuMat1, None) # Both are returned as GPU Mats
kps2, descs2 = orb.detectAndComputeAsync(cuMat2, None)
matches = matcherGPU.match(descs1, descs2)
matches2=sorted(matches,key=lambda x: x.distance, reverse=False)
numGoodMatches = int(len(matches2) * GOOD_MATCH_PERCENT)
matches2 = matches2[:numGoodMatches]  
kps1c = orb.convert(kps1)
kps2c = orb.convert(kps2)
imMatches = cv2.drawMatches(npMat1, kps1c, npMat2, kps2c, matches2, None)
cv2.imwrite("gpu_matches.jpg", imMatches)  

CodePudding user response:

I'm not quite sure what you're asking, but if you want to match more than two images you'll need to use a for loop:

<code>for image in images:
    kps, descs = orb.detectAndComputeAsync(image, None)
    matches = matcherGPU.match(descs1, descs2)
    matches2=sorted(matches,key=lambda x: x.distance, reverse=False)
    numGoodMatches = int(len(matches2) * GOOD_MATCH_PERCENT)
    matches2 = matches2[:numGoodMatches]  
    kps1c = orb.convert(kps1)
    kps2c = orb.convert(kps2)
    imMatches = cv2.drawMatches(npMat1, kps1c, npMat2, kps2c, matches2, None)
    cv2.imwrite("gpu_matches.jpg", imMatches)
</code>

CodePudding user response:

from distutils.command.upload import upload
import cv2
import numpy as np
import glob as gb

GOOD_MATCH_PERCENT = 0.15
max_features = 9000
orb = cv2.cuda_ORB.create(max_features)
matcherGPU = cv2.cuda.DescriptorMatcher_createBFMatcher(cv2.NORM_HAMMING)
   
img_files = ['0.png', '1.png', '2.png', '3.png']
image_gpu= cv2.cuda_GpuMat()

for i in range(len(img_files)):
    screenshot = cv2.imread(f"C:\Users\charlie\Desktop\{img_files[i]}")
    image_gpu.upload(screenshot)
    screenshot = cv2.cuda.cvtColor(image_gpu, cv2.COLOR_RGB2BGR)
    kp1, des1 = orb.detectAndComputeAsync(image_gpu[i-1], None)
    kp2, des2 = orb.detectAndComputeAsync(image_gpu[i], None)
    kp1 = orb.convert(kp1)
    kp2 = orb.convert(kp2)
    matches = matcherGPU.match(des1, des2)
    matches2=sorted(matches,key=lambda x: x.distance, reverse=False)
    numGoodMatches = int(len(matches2) * GOOD_MATCH_PERCENT)
    matches2 = matches2[:numGoodMatches]  

I think this is a good way but I have this error:

kp1, des1 = orb.detectAndComputeAsync(image_gpu[i], None) TypeError: 'cv2.cuda_GpuMat' object is not subscriptable

  • Related