python opencv 特征匹配_Python opencv 特征匹配 支持h264 多线程 队列

本文介绍了使用Python和OpenCV进行特征匹配的步骤,包括SIFT检测和FLANN匹配,并展示了如何将匹配结果可视化。此外,还讨论了如何处理H264编码、图片和视频的转换、多线程以及使用队列。同时提供了处理不支持H264问题的方法,以及使用ffmpeg进行音频和视频的合并。文章最后提到了项目依赖管理和程序打包的相关技巧。
摘要由CSDN通过智能技术生成

特征匹配pip install opencv-python

pip install opencv-contrib-python==3.4.2.17

这里一定要加版本号,负责会报一个版权错误

代码# import numpy as np

import cv2

import matplotlib.pyplot as plt

img1 = cv2.imread('a1.jpg', 0) # queryImage

img2 = cv2.imread('a4.jpg', 0) # trainImage

# Initiate SIFT detector

sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT

kp1, des1 = sift.detectAndCompute(img1, None)

kp2, des2 = sift.detectAndCompute(img2, None)

# FLANN parameters

FLANN_INDEX_KDTREE = 1

index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)

search_params = dict(checks=50) # or pass empty dictionary

flann = cv2.FlannBasedMatcher(index_params, search_params)

matches = flann.knnMatch(des1, des2, k=2)

# print(matches)

# Need to draw only good matches, so create a mask

matchesMask = [[0, 0] for i in range(len(matches))]

# ratio test as per Lowe's paper

for i, (m, n) in enumerate(matches):

if m.distance < 0.7*n.distance:

matchesMask[i] = [1, 0]

draw_params = dict(matchColor=(0, 255, 0),

singlePointColor=(255, 0, 0),

matchesMask=matchesMask,

flags=0)

img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, matches, None, **draw_params)

plt.imshow(img3,), plt.show()

pil和cv2图片互转import cv2

import numpy as np

from PIL import Image

cap = cv2.VideoCapture(0)

img2 = Image.open("./1.jpg")

while(True):

ret, frame = cap.read()

if ret is False:

cv2.waitKey()

break

cv2img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

pilimg = Image.fromarray(cv2img)

pilimg.paste(img2, (200, 200))

cv2img = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)

cv2.imshow('frame', cv2img)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

cap.release()

cv2.destroyAllWindows()

支持H264import cv2

import numpy as np

from PIL import Image

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'avc1')

out = cv2.VideoWriter('./output.mp4', fourcc, 30.0, (640, 480), True)

img2 = Image.open("./1.jpg")

while(True):

ret, frame = cap.read()

if ret is False:

cv2.waitKey()

break

cv2img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

pilimg = Image.fromarray(cv2img)

pilimg.paste(img2, (200, 200))

cv2img = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)

cv2.imshow('frame', cv2img)

out.write(cv2img)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

cap.release()

out.release()

cv2.destroyAllWindows()

当提示不支持h264时候可以下载对应版本的openh264-1.7.0-win64.dll,然后放在python的安装目录即可

下载文件import urllib.request

url = "https://baidu.com"

path = "./"

x = "index.html"

urllib.request.urlretrieve(url, '{0}{1}'.format(path, x))

ffmpegpip install ffmpeg-python==0.1.1

import ffmpeg

def combine(video_path, audio_path, out_path):

video = ffmpeg.input(video_path)

audio = ffmpeg.input(audio_path)

out = ffmpeg.output(

audio,

video,

out_path,

vcodec='copy',

acodec='copy',

threads='5'

)

ffmpeg.overwrite_output(out).run()

本地安装依赖requirements.txt

存放依赖关系

例如

ffmpeg-python==0.2.0

pip install -t . -r requirements.txt -U

多线程,队列from queue import PriorityQueue

import threading

def run(n):

print("task ", n, 'start')

time.sleep(n)

print("task ", n, 'finish')

start_time = time.time()

t1 = threading.Thread(target=run, args=(1,))

t2 = threading.Thread(target=run, args=(4,))

t1.start()

t2.start()

t2.join()

t1.join()

print("cost :", time.time()-start_time)

---

Q = PriorityQueue()

Q.put((i, frame))

for i in range(0, Q.qsize()):

frame = Q.get()[1]

打包pip install pyinstaller

程序目录

pyinstaller -F test.py

centos打包

https://www.cnblogs.com/jerryzh/p/10937905.html

阿里云源pip3 命令最后加上 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

获取当前目录# 打包后路径错误

print(sys.path[0])

# 当前文件地址

print(sys.argv[0])

# Python或打包后名字

print(os.path.dirname(os.path.realpath(sys.executable)))

# 文件目录

print(os.path.dirname(os.path.realpath(sys.argv[0])))

最后更新于 2019-12-14 07:03:09 并被添加「python opencv 特征匹配」标签,已有 248 位童鞋阅读过。

相关文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值