背景介绍:
windows10,python3.x,64位AMD R9处理器,ROG电脑,pycharm开发,已经安装好了PCV库
前置import代码如下 涉及到的库有:
# coding: utf-8
import math
import pickle
import sys
from matplotlib.pylab import *
from matplotlib import cbook, mlab, pyplot as plt
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import pygame, pygame.image
from pygame.locals import *
from PCV.geometry import homography, camera
from PCV.localdescriptors import sift
import matplotlib
在使用PCV库的SIFT文件时候,有这样几行代码一直报错,就是process_image这个函数
具体报错:
Traceback (most recent call last):
File "C:\Users\ultra\Desktop\壶子.py", line 133, in <module>
l0, d0 = sift.read_features_from_file('im0.sift')
File "C:\Users\ultra\AppData\Local\Programs\Python\Python39\lib\site-packages\pcv-1.0-py3.9.egg\PCV\localdescriptors\sift.py", line 29, in read_features_from_file
File "C:\Users\ultra\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\npyio.py", line 1373, in loadtxt
arr = _read(fname, dtype=dtype, comment=comment, delimiter=delimiter,
File "C:\Users\ultra\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\npyio.py", line 992, in _read
fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
File "C:\Users\ultra\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\_datasource.py", line 193, in open
return ds.open(path, mode, encoding=encoding, newline=newline)
File "C:\Users\ultra\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\_datasource.py", line 533, in open
raise FileNotFoundError(f"{path} not found.")
FileNotFoundError: im0.sift not found.
大概就是说
这个文件没有生成出来,一看我的项目结构 确实没有
于是定位问题到sift.process_image这个函数中.
def process_image(imagename,resultname,params="--edge-thresh 10 --peak-thresh 5"):
""" Process an image and save the results in a file. """
if imagename[-3:] != 'pgm':
# create a pgm file
im = Image.open(imagename).convert('L')
im.save('tmp.pgm')
imagename = 'tmp.pgm'
cmmd = str("sift "+imagename+" --output="+resultname+
" "+params)
status=os.system(cmmd)
if status != 0:
print("命令执行失败,状态码:", status)
else:
print("命令执行成功")
print('processed', imagename, 'to', resultname)
上面就是核心代码,我对这段代码进行了修改 原始的代码如下图所示
def process_image(imagename,resultname,params="--edge-thresh 10 --peak-thresh 5"):
""" Process an image and save the results in a file. """
if imagename[-3:] != 'pgm':
# create a pgm file
im = Image.open(imagename).convert('L')
im.save('tmp.pgm')
imagename = 'tmp.pgm'
cmmd = str("sift "+imagename+" --output="+resultname+
" "+params)
os.system(cmmd)
print('processed', imagename, 'to', resultname)
我增加了对于os.system语句的判定检查,从而定位到了错误就是这里.
cmmd = str("sift "+imagename+" --output="+resultname+
" "+params)
status=os.system(cmmd)
这两个语句发生了问题 os.system没有执行成功.
于是我在网上开始查找资料 找了半天 找到了一个
原因是:需要使用开源工具包VLFeat提供的二进制文件来计算图像的SIFT特征
打开下面网址进行下载VLFeat - Homehttps://www.vlfeat.org/
页面如下:
下载完毕后 解压 或者直接打开压缩包 找到bin文件目录
注意系统版本我们一般都是win64 不用打开其他的maci或者glnxa64.
然后我们只需要三个文件vl.dll和sift.exe和vl.lib 复制到我们的项目文件里面去 如下图所示
然后就能运行了!