这里的cv2是:opencv-contrib-python 3.4.2.16,3.4.3以上的版本有算法保护,无法直接调用SIFT算法
import numpy as np
import cv2
from matplotlib import pyplot as plt
imgname = '../hmi.sharp_720s.1.20100504_160000_TAI.magnetogram.jpg'
sift = cv2.xfeatures2d.SIFT_create()
img = cv2.imread(imgname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kp,des = sift.detectAndCompute(img,None) # 关键点,SIFT特征向量
img1=cv2.drawKeypoints(img,kp,img,color=(255,255,0))
for i in kp:
print(list(i.pt)) # 打印点坐标
cv2.imshow('point',img1)
cv2.waitKey(0)
以下是根据SIFT关键点的灰度值进行分类:
import numpy as np
import cv2
from matplotlib import pyplot as plt
from PIL import Image
imgname = r'D:\train_jpg_input\hmi.sharp_720s.3295.20131023_062400_TAI.magnetogram.jpg'
sift = cv2.xfeatures2d.SIFT_create()
img = cv2.imread(imgname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kp, des = sift.detectAndCompute(img, None) # 关键点,SIFT特征向量
im = Image.open(imgname) # Can be many different formats.
pix = im.load() # 读取灰度值
# print(im.size) # 获取图像宽度和高度
exists_ls = []
new_kp_white = []
new_kp_grey = []
new_kp_black = []
for i in kp:
if list(i.pt) not in exists_ls: # 取点去重
exists_ls.append(list(i.pt))
gray_light = pix[list(i.pt)[0], list(i.pt)[1]]
if gray_light >= 150: # 根据灰度值分成两类
new_kp_white.append(i)
elif gray_light <= 100:
new_kp_black.append(i)
else:
continue
img1 = cv2.drawKeypoints(img, new_kp_white, img, color=(0, 255, 0)) # 白色点画成绿色
img2 = cv2.drawKeypoints(img, new_kp_black, img, color=(255, 0, 0)) # 黑色点画成蓝色
cv2.imshow('point', img1)
cv2.imshow('point', img2)
cv2.waitKey(0)