Opencvにおける特徴点の解釈 Keypoint
特徴点検出を学習する際には、ORBやSIFTなどの特徴点検出器を使って特徴点を生成します(FASTやSURFは特許申請中のようで、新しいバージョンでは使えない可能性があります)。 iterator の 。
KeyPoint
クラスによって生成されたオブジェクト です 。
特徴点の生成
まず、特徴点の生成を見てみましょう。
import cv2
import numpy as np
import matplotlib.pyplot as plt
#初始化ORB特征点检测器
orb=cv2.ORB_create()
#初始化sift特征点检测器
sift=cv2.xfeatures2d.SIFT_create()
#使用orb检测图片关键点
orb_kp=orb.detect(img)
#使用sift检测图片关键点
sift_kp=sift.detect(newimg)
#画orb关键点
img=cv2.drawKeypoints(img,orb_kp,None,color=[0,0,255])
#画sift关键点
newimg=cv2.drawKeypoints(newimg,sift_kp,None,color=[0,0,255])
#绘图
plt.subplot(121)
plt.title('orbimg')
plt.imshow(img)
plt.subplot(122)
plt.title('siftimg')
plt.imshow(newimg)
plt.show()
print(orb_kp)
print(sift_kp)
print(type(orb_kp[0]))
結果:
(<KeyPoint 000001BC7B2A22A0>, <KeyPoint 000001BC7CFDF1E0>, <KeyPoint 000001BC7CFDFF30>.....)
<class 'cv2.KeyPoint'>
特徴点の座標への変換
では、どのように変換すればよいでしょうか? これはKeyPoint
オブジェクトなので、最初にdir()
と を使用して、次のようなクラスの属性help()
を表示できます。KeyPoint
print(dir(orb_kp[0]))
['__class__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'angle',
'class_id',
'convert',
'octave',
'overlap',
'pt',
'response',
'size']
その後、プロパティにアクセスして関連情報を印刷できます。ここで、kp は特徴点の座標です。
print(orb_kp[0].angle)
print(orb_kp[0].class_id)
print(orb_kp[0].convert)
print(orb_kp[0].octave)
print(orb_kp[0].overlap)
print(orb_kp[0].pt)
print(orb_kp[0].response)
print(orb_kp[0].size)
249.6773681640625
-1
<built-in method convert of type object at 0x000001BC30C626D0>
0
<built-in method overlap of type object at 0x000001BC30C626D0>
(164.0, 189.0)
0.0004739729920402169
31.0
Opencv の公式ドキュメントを参照して、関連情報を見つけることもできます。
次に、Opencv が公式に提供する関数を通じて変換する別の方法があります。cv2.KeyPoint_convert()
関数を使用して変換します。
例えば:
kp=cv2.KeyPoint_convert(orb_kp)
print(kp)
[[164. 189. ]
[356. 325. ]
[100. 36. ]
[ 56. 272. ]
[111. 283. ]
[114. 280. ]
[ 60. 276. ]....]太长了我省略了
次のように入力すると、エラーが発生します。
kp=cv2.KeyPoint_convert(orb_kp[0])
> - Can't parse 'keypoints'. Input argument doesn't provide sequence protocol
> - Can't parse 'points2f'. Input argument doesn't provide sequence protocol
最初のものを出力したい場合は、次のように入力するだけです。
kp=cv2.KeyPoint_convert(orb_kp,keypointIndexes=[0])
print(kp)
[[164. 189.]]
座標から特徴点への変換
最初に座標を特徴点に変換したい場合は、以下を使用してcv2.KeyPoint
特徴点を作成できます。
kp=cv2.KeyPoint(x=1,y=2,size=3)
print(kp)
<KeyPoint 00000120B1114A80>