python cv2 sift算法图像分类和图像搜索的使用

最近有很多图片数据,突发奇想有没有一种方法能够把一类图片分到一类文件夹中,百度一下就有了sift算法,phash算法

phash算法识别率有点低,sift算法识别率很高,图像旋转灰暗都不会影响而且专利也过期了

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

MIN_MATCH_COUNT = 100

sift = cv.SIFT_create()
img1 = cv.imread('2.jpg',cv.IMREAD_GRAYSCALE)          # queryImage
img2 = cv.imread('6.jpg',cv.IMREAD_GRAYSCALE)   
#sift = cv.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)



# Initialize and use FLANN
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
flann = cv.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)

# Lowe's ratio test
good = []
for m, n in matches:
    if m.distance < 0.7 * n.distance:
        good.append(m)

if len(good) > MIN_MATCH_COUNT:
    print('good '+str(len(good)))
    # Estimate homography between template and scene
    src_pts = np.float32([ kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
    dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)

    M = cv.findHomography(src_pts, dst_pts, cv.RANSAC, 5.0)[0]

    # Draw detected template in scene image
    h, w = img1.shape
    pts = np.float32([[0, 0],
                      [0, h - 1],
                      [w - 1, h - 1],
                      [w - 1, 0]]).reshape(-1, 1, 2)
    dst = cv.perspectiveTransform(pts, M)

    img2 = cv.polylines(img2, [np.int32(dst)], True, 255, 3, cv.LINE_AA)

    h1, w1 = img1.shape
    h2, w2 = img2.shape
    nWidth = w1 + w2
    nHeight = max(h1, h2)
    hdif = int((h2 - h1) / 2)
    newimg = np.zeros((nHeight, nWidth, 3), np.uint8)

    for i in range(3):
        newimg[hdif:hdif + h1, :w1, i] = img1
        newimg[:h2, w1:w1 + w2, i] = img2

    # Draw SIFT keypoint matches
    for m in good:
        pt1 = (int(kp1[m.queryIdx].pt[0]), int(kp1[m.queryIdx].pt[1] + hdif))
        pt2 = (int(kp2[m.trainIdx].pt[0] + w1), int(kp2[m.trainIdx].pt[1]))
        cv.line(newimg, pt1, pt2, (255, 0, 0))

    plt.imshow(newimg)
    plt.show()
else:
    print('bad')

改进版,效果更好
用vgg生成特征向量然后做聚类
https://blog.csdn.net/isyoungboy/article/details/122859530?spm=1001.2014.3001.5501

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
SIFT(尺度不变特征变换,Scale-Invariant Feature Transform)是一种广泛应用于计算机视觉领域的图像匹配算法,它能在不同尺度和旋转下找到稳定的关键点,并提取出描述它们周围局部特征的梯度方向直方图。在Python中,我们可以使用`opencv-python`库来实现SIFT。 首先,你需要安装`opencv-python`库,可以使用pip进行安装: ```bash pip install opencv-python ``` 然后,以下是使用SIFT进行图像配准的基本步骤: 1. 导入需要的模块: ```python import cv2 import numpy as np ``` 2. 加载图片并进行灰度处理: ```python img1 = cv2.imread('image1.jpg', 0) img2 = cv2.imread('image2.jpg', 0) ``` 3. 创建SIFT对象并检测关键点和描述符: ```python sift = cv2.xfeatures2d.SIFT_create() kp1, des1 = sift.detectAndCompute(img1, None) kp2, des2 = sift.detectAndCompute(img2, None) ``` 4. 使用FLANN匹配器进行匹配(如果两个图像的特征足够多): ```python bf = cv2.BFMatcher() matches = bf.knnMatch(des1, des2, k=2) ``` 5. 提取可靠匹配并进一步精确定位: ```python good_matches = [m for m, n in matches if m.distance < 0.7 * n.distance] if len(good_matches) > 4: src_pts = np.float32([ kp1[m.queryIdx].pt for m in good_matches ]).reshape(-1, 1, 2) dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good_matches ]).reshape(-1, 1, 2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) else: print("Not enough good matches are found.") M = None ``` 6. 应用 Homography 进行图像变换: ```python warped_img2 = cv2.warpPerspective(img2, M, (img1.shape, img1.shape)) ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值