opencv计算机视觉学习笔记五

本文详细介绍了OpenCV中的图像检索技术,包括特征检测算法如Harris、SIFT、SURF、FAST、BRIEF和ORB,并提供了相关代码示例。针对特征匹配,讨论了暴力匹配和Flann匹配法,还展示了在实际应用中的纹身取证场景。
摘要由CSDN通过智能技术生成

第六章 图像检索以及基于图像描述符的搜索

通过提取特征进行图像的匹配与搜索

 

1 特征检测算法

常见的特征和提取算法:

Harris 检测角点

Sift 检测斑点(blob) 有专利保护

Surf 检测斑点   有专利保护

Fast 检测角点

Brief 检测斑点

Orb  带方向的fast算法和具有旋转不变性的brief算法

 

特征的定义

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2016/12/5 12:30
# @Author  : Retacn
# @Site    : 检测图像的角点
# @File    : cornerHarris.py
# @Software: PyCharm
__author__ = "retacn"
__copyright__ = "property of mankind."
__license__ = "CN"
__version__ = "0.0.1"
__maintainer__ = "retacn"
__email__ = "zhenhuayue@sina.com"
__status__ = "Development"

import cv2
import numpy as np

# 读入图像
img = cv2.imread('../test1.jpg')
# 转换颜色空间
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
# 检测图像角点
dst = cv2.cornerHarris(gray,
                       2,
                       23# sobel算子的中孔,3-31之间的奇数
                       0.04)
# 将检测到有角点标记为红色
img[dst > 0.01 * dst.max()] = [0, 0, 255]
while (True):
    cv2.imshow("corners", img)
    if cv2.waitKey(33) & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()

 

 

使用dog和sift进行特征提取和描述

示例代码如下:

import cv2
import sys
import numpy as py

# 读入图像
# imgpath=sys.argv[1]
imgpath = '../test1.jpg'
img = cv2.imread(imgpath)
# 更换颜色空间
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 创建sift对象,计算灰度图像,会使用dog检测角点
sift = cv2.xfeatures2d.SIFT_create()
keypoints, descriptor = sift.detectAndCompute(gray, None)

# print(keypoints)
# 关键点有以下几个属性
# angle 表示特征的方向
# class_id 关键点的id
# octave 特征所在金字塔的等级
# pt 图像中关键点的坐标
# response 表示关键点的强度
# size  表示特征的直径
img = cv2.drawKeypoints(image=img,
                        outImage=img,
                        keypoints=keypoints,
                        color=(51, 163, 236),
                        flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# 显示图像
cv2.imshow('sift_keypoints', img)
while (True):
    if cv2.waitKey(int(1000 / 12)) & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()

 

使用心有快速hessian算法和SURF来提取特征

示例代码发如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2016/12/10 17:30
# @Author  : Retacn
# @Site    : sift用于检测斑点
# @File    : sift.py
# @Software: PyCharm
__author__ = "retacn"
__copyright__ = "property of mankind."
__license__ = "CN"
__version__ = "0.0.1"
__maintainer__ = "retacn"
__email__ = "zhenhuayue@sina.com"
__status__ = "Development"

import cv2
import sys
import numpy as py

# 读入图像
# imgpath=sys.argv[1]
# alg=sys.argv[2]
# threshold=sys.argv[3]

imgpath = '../test1.jpg'
img = cv2.imread(imgpath)
# alg = 'SURF'
alg = 'SIFT'
# threshold = '8000'
# 阈值越小特征点越多
threshold = '4000'


def fd(algorithm):
    if algorithm == 'SIFT':
        return cv2.xfeatures2d.SIFT_create()
    if algorithm == 'SURF':
        # return cv2.xfeatures2d.SURF_create(float(threshold) if len(sys.argv) == 4 else 4000)
        return cv2.xfeatures2d.SURF_create(
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值