python opencv实现图像配准与比较


Posted in Python onFebruary 09, 2021

本文实例为大家分享了python opencv实现图像配准与比较的具体代码,供大家参考,具体内容如下

代码 

from skimage import io
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
 
img_path1 = '2_HE_maxarea.png'
img_path2 = '2_IHC_maxarea.png'
 
img1 = io.imread(img_path1)
img2 = io.imread(img_path2)
img1 = np.uint8(img1)
img2 = np.uint8(img2)
 
# find the keypoints and descriptors with ORB
orb = cv.ORB_create()
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
 
# def get_good_match(des1,des2):
#  bf = cv.BFMatcher()
#  matches = bf.knnMatch(des1, des2, k=2)
#  good = []
#  for m, n in matches:
#   if m.distance < 0.75 * n.distance:
#    good.append(m)
#  return good,matches
# goodMatch,matches = get_good_match(des1,des2)
# img3 = cv.drawMatchesKnn(img1,kp1,img2,kp2,matches[:20],None,flags=2)
 
# create BFMatcher object
bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True)
# Match descriptors.
matches = bf.match(des1,des2)
# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)
# Draw first 20 matches.
img3 = cv.drawMatches(img1,kp1,img2,kp2,matches[:20],None, flags=2)
 
 
goodMatch = matches[:20]
if len(goodMatch) > 4:
 ptsA= np.float32([kp1[m.queryIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
 ptsB = np.float32([kp2[m.trainIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
 ransacReprojThreshold = 4
 H, status =cv.findHomography(ptsA,ptsB,cv.RANSAC,ransacReprojThreshold);
 #其中H为求得的单应性矩阵矩阵
 #status则返回一个列表来表征匹配成功的特征点。
 #ptsA,ptsB为关键点
 #cv2.RANSAC, ransacReprojThreshold这两个参数与RANSAC有关
 imgOut = cv.warpPerspective(img2, H, (img1.shape[1],img1.shape[0]),flags=cv.INTER_LINEAR + cv.WARP_INVERSE_MAP)
 
# 叠加配准变换图与基准图
rate = 0.5
overlapping = cv.addWeighted(img1, rate, imgOut, 1-rate, 0)
io.imsave('HE_2_IHC.png', overlapping)
err = cv.absdiff(img1,imgOut) 
 
# 显示对比
plt.subplot(221)
plt.title('orb')
plt.imshow(img3)
 
plt.subplot(222)
plt.title('imgOut')
plt.imshow(imgOut)
 
plt.subplot(223)
plt.title('overlapping')
plt.imshow(overlapping)
 
plt.subplot(224)  
plt.title('diff') 
plt.imshow(err)
 
plt.show()

结果:

python opencv实现图像配准与比较

python opencv实现图像配准与比较

python opencv实现图像配准与比较

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
全面解读Python Web开发框架Django
Jun 30 Python
跟老齐学Python之有容乃大的list(1)
Sep 14 Python
解决Linux系统中python matplotlib画图的中文显示问题
Jun 15 Python
使用Pyinstaller的最新踩坑实战记录
Nov 08 Python
python使用正则表达式来获取文件名的前缀方法
Oct 21 Python
Python列表list排列组合操作示例
Dec 18 Python
选择python进行数据分析的理由和优势
Jun 25 Python
使用python写的opencv实时监测和解析二维码和条形码
Aug 14 Python
python数据类型可变不可变知识点总结
Mar 06 Python
python将logging模块封装成单独模块并实现动态切换Level方式
May 12 Python
python使用Word2Vec进行情感分析解析
Jul 31 Python
Python获取指定网段正在使用的IP
Dec 14 Python
python urllib和urllib3知识点总结
Feb 08 #Python
Python3.9.1中使用match方法详解
Feb 08 #Python
python读取excel数据并且画图的实现示例
Feb 08 #Python
Python爬取某平台短视频的方法
Feb 08 #Python
利用Python批量识别电子账单数据的方法
Feb 08 #Python
Python命令行参数argv和argparse该如何使用
Feb 08 #Python
python 实现Requests发送带cookies的请求
Feb 08 #Python
You might like
小偷PHP+Html+缓存
2006/12/20 PHP
PHP写的获取各搜索蜘蛛爬行记录代码
2012/08/21 PHP
php抓取页面的几种方法详解
2013/06/17 PHP
使用php语句将数据库*.sql文件导入数据库
2014/05/05 PHP
PHP常量使用的几个需要注意的地方(谨慎使用PHP中的常量)
2014/09/12 PHP
php中in_array函数用法分析
2014/11/15 PHP
php操作路径的经典方法(必看篇)
2016/10/04 PHP
PHP有序表查找之二分查找(折半查找)算法示例
2018/02/09 PHP
javascript+css 网页每次加载不同样式的实现方法
2009/12/27 Javascript
jQuery拖拽div实现思路
2014/02/19 Javascript
JQuery中DOM事件合成用法实例分析
2015/06/13 Javascript
由简入繁实现Jquery树状结构的方法(推荐)
2016/06/10 Javascript
angularjs 中$apply,$digest,$watch详解
2016/10/13 Javascript
用vue和node写的简易购物车实现
2017/04/25 Javascript
jquery平滑滚动到顶部插件使用详解
2017/05/08 jQuery
深入理解Vue.js源码之事件机制
2017/09/27 Javascript
laravel-admin 与 vue 结合使用实例代码详解
2019/06/04 Javascript
微信小程序实现点击卡片 翻转效果
2019/09/04 Javascript
详解基于element的区间选择组件校验(交易金额)
2021/01/07 Javascript
[37:35]DOTA2上海特级锦标赛A组资格赛#1 Secret VS MVP.Phx第二局
2016/02/25 DOTA
python编程线性回归代码示例
2017/12/07 Python
Python实现App自动签到领取积分功能
2018/09/29 Python
python实现剪切功能
2019/01/23 Python
Django之路由层的实现
2019/09/09 Python
python GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的详细方法与实例
2020/03/09 Python
windows python3安装Jupyter Notebooks教程
2020/04/13 Python
css3翻牌翻数字的示例代码
2020/02/07 HTML / CSS
亿阳信通股份有限公司C#笔试题
2016/12/06 面试题
安全生产管理合理化建议书
2014/03/12 职场文书
幼儿教师求职信
2014/05/24 职场文书
校园活动策划方案
2014/06/13 职场文书
大学生创业计划书
2014/08/14 职场文书
2014年作风建设工作总结
2014/10/29 职场文书
生死牛玉儒观后感
2015/06/11 职场文书
python基础详解之if循环语句
2021/04/24 Python
如何开启Apache,Nginx和IIS服务器的GZIP压缩功能
2022/04/29 Servers