提取特征点并定位(缺陷检测)

项目中需要进行缺陷检测。先做图像差分,然后检测出缺陷并标注缺陷位置。

import cv2
import sys
import numpy as np

# 第一个参数 图片路径
imgpath = sys.argv[1]
img = cv2.imread(imgpath)
alg = sys.argv[2]

# 可根据参数选择算法
def fd(algorithm):
    if algorithm == 'SIFT':
        return cv2.xfeatures2d.SIFT_create()
    if algorithm == 'SURF':
        return cv2.xfeatures2d.SURF_create(float(sys.argv[3]) if
                                           len(sys.argv) == 4 else 4000)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

sift = cv2.xfeatures2d.SIFT_create()
keypoints, descriptor = sift.detectAndCompute(gray, None)


img = cv2.drawKeypoints(image=img, outImage=img, keypoints=keypoints,
                        flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,
                        color=(51, 163, 236))

# 循环获取点的位置
for i in range(len(keypoints)):
    p = keypoints[i].pt
    print(p)

cv2.imshow('sift', img)
cv2.waitKey()
cv2.destroyAllWindows()

 

你可能感兴趣的:(Image,Process)