计算机视觉学习blog(二)——sift特征检测+匹配地理标记图像

摘要:python学习计算机视觉,对图像进行sift特征点检测,并采用可视化连接,使用局部描述子将同一地理位置图像进行分类

知识点概念这里就不多说了。。。

SIFT特征点检测

import sift
from PIL import Image
from pylab import *
imname = 'test/3.jpg'
im1 = array(Image.open(imname).convert('L'))
sift.process_image(imname,'3.sift')
l1,d1 = sift.read_features_from_file('3.sift')
figure()
gray()
sift.plot_features(im1,l1,circle=False)
show()

输入一张图片,使用sift算法进行特征点检测,运行图如下:
在这里插入图片描述

SIFT特征匹配

from PIL import Image
from pylab import *
from numpy import *
import sift
from pcv.localdescriptors import sift
im1f = 'test/3.jpg'
im2f = 'test/4.jpg'
im1 = array(Image.open(im1f))
im2 = array(Image.open(im2f))

sift.process_image(im1f,'out_sift_1.txt')
l1,d1=sift.read_features_from_file('out_sift_1.txt')
figure()
gray()
subplot(121)
sift.plot_features(im1,l1,circle=False)

sift.process_image(im2f,'out_sift_2.txt')
l2,d2=sift.read_features_from_file('out_sift_2.txt')
subplot(122)
sift.plot_features(im2,l2,circle=False)

matches = sift.match_twosided(d1,d2)
print('{} matches'.format(len(matches.nonzero()[0])))
figure()
gray()
sift.plot_matches(im1,im1,l1,l2,matches, show_below=True)
show()

输入两张图片,分别进行特征点检测,将检测数据存储到文本文件中,将二者进行比较使用match_twosided方法匹配相同特征,运行图如下,得到matches=100
在这里插入图片描述

匹配地理标记图像

import json
import os
import urllib
from pylab import *
from PIL import Image
from pcv.localdescriptors import sift
from pcv.tools import imtools
import pydot
download_path = "D:/Python/Python39/Scripts/计算机视觉/test"  #注意使用绝对路径
path = "D:/Python/Python39/Scripts/计算机视觉/test"

imlist = imtools.get_imlist(download_path)
nbr_images = len(imlist)

featlist = [imname[:-3] + 'sift' for imname in imlist]
for i, imname in enumerate(imlist):
    sift.process_image(imname, featlist[i])

matchscores = zeros((nbr_images, nbr_images))

for i in range(nbr_images):
    for j in range(i, nbr_images): #only compute upper triangle
        print( 'comparing ', imlist[i], imlist[j])
        l1, d1 = sift.read_features_from_file(featlist[i])
        l2, d2 = sift.read_features_from_file(featlist[j])
        matches = sift.match_twosided(d1, d2)
        nbr_matches = sum(matches>0)
        print('number of matches = ', nbr_matches)
        matchscores[i,j] = nbr_matches

# copy values
for i in range(nbr_images):
    for j in range(i + 1, nbr_images): # no need to copy diagonal
        matchscores[j, i] = matchscores[i, j]

# 可视化
threshold = 2 # min number of matches needed to craete link

g = pydot.Dot(graph_type='graph') # don't want the default directed graph

for i in range(nbr_images):
    for j in range(i+1, nbr_images):
        if matchscores[i,j] > threshold:
            #图像对中的第一幅图像
            im = Image.open(imlist[i])
            im.thumbnail((100,100))
            filename = path + str(i) + '.png'
            im.save(filename) #需要一定大小的临时文件
            g.add_node(pydot.Node(str(i), fontcolor='transparent',
                       shape='rectangle', image=filename))

            #图像对中的第二幅图像
            im = Image.open(imlist[j])
            im.thumbnail((100,100))
            filename = path + str(j) + '.png'
            im.save(filename) #需要一定大小的临时文件
            g.add_node(pydot.Node(str(j), fontcolor='transparent',
                       shape='rectangle', image=filename))
            g.add_edge(pydot.Edge(str(i), str(j)))
g.write_png('compare.png')

输入一组图片,将所有图片的特征点保存到同名的后缀.sift文件中,通过循环将文件的特征数值两两比较,通过可视化包设置达到最小数值thresold=2,将两张图片进行连线,运行图如下:
在这里插入图片描述
在这里插入图片描述
1.jpg和2.jpg共有56个匹配点;
3.jpg有964个特征点,和4.jpg共有100个匹配点;
5.jpg有773个特征点,和6.jpg共有12个匹配点,和7共有3个匹配点,和8共有590个匹配点
6.jpg有668个特征点,和7.jpg共有3个匹配点,和8共有8个匹配点
10.jpg有2916个特征点,和9.jpg共有11个匹配点,和11共有15个匹配点;
11.jpg有1896个特征点,和9.jpg共有8个匹配点

注意问题

图片需要压缩小点否则程序要跑很久,尽量保证图片像素高宽比一致

工具包使用遇到的问题

从网上下载安装pcv包(pip install pcv)调用,然后需要下载降级二进制处理文件vlfeat-0.9.20版本,下载完成后找到\bin\win64\下的sift.exe和vl.dll文件复制到你的项目底下,修改pcv路径(python/lib/pcv/localdescriptors)底下sift.py文件process_image方法底下print后面加括号;

出现了’dot’ not found问题,还需要下载graphviz(安装时授权系统环境变量),找到bin目录底下dot.exe路径,修改python/lib/site-packages/pydot.py中_inif_方法下代码为 self.prog = r’路径\bin\dot.exe’

这里给了超链接下载,具体安装方法参考网上教程,具体代码可以参考书本代码《python计算机视觉编程》

以上图片为集美大学实拍建筑(嘉庚图书馆侧门,陈延奎图书馆,嘉庚湖畔勿忘亭,集美大学东大门)部分来自百度图片

https://www.cnblogs.com/Easy-/p/14586997.html

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值