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 相关文章推荐
Django中利用filter与simple_tag为前端自定义函数的实现方法
Jun 15 Python
python中关于for循环的碎碎念
Jun 30 Python
Python计算斗牛游戏概率算法实例分析
Sep 26 Python
python实现Dijkstra静态寻路算法
Jan 17 Python
python time.sleep()是睡眠线程还是进程
Jul 09 Python
python 一篇文章搞懂装饰器所有用法(建议收藏)
Aug 23 Python
Python实现发票自动校核微信机器人的方法
May 22 Python
Python爬虫开发与项目实战
Dec 16 Python
Python基于argparse与ConfigParser库进行入参解析与ini parser
Feb 02 Python
详解Python常用的魔法方法
Jun 03 Python
Pytorch反向传播中的细节-计算梯度时的默认累加操作
Jun 05 Python
python数字类型和占位符详情
Mar 13 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 获得汉字拼音首字母的函数
2009/08/01 PHP
如何使用“PHP” 彩蛋进行敏感信息获取
2013/08/07 PHP
php上传文件,创建递归目录的实例代码
2013/10/18 PHP
php生成静态页面的简单示例
2014/04/17 PHP
ThinkPHP控制器间实现相互调用的方法
2014/10/31 PHP
php+curl 发送图片处理代码分享
2015/07/09 PHP
PHP简单实现模拟登陆功能示例
2017/09/15 PHP
网站被黑的假象--ARP欺骗之页面中加入一段js
2007/05/16 Javascript
基于jquery的高性能td和input切换并可修改内容实现代码
2011/01/09 Javascript
JavaScript高级程序设计 事件学习笔记
2011/09/10 Javascript
JavaScript设置IFrame高度自适应(兼容各主流浏览器)
2013/06/05 Javascript
JavaScript获取FCK编辑器信息的具体方法
2013/07/12 Javascript
jQuery实现文本框输入同步的方法
2015/06/20 Javascript
jQuery Mobile中的button按钮组件基础使用教程
2016/05/23 Javascript
原生JS轮播图插件
2017/02/09 Javascript
SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题的解决方法
2018/01/09 Javascript
AngularJS双向数据绑定原理之$watch、$apply和$digest的应用
2018/01/30 Javascript
JavaScript实现获取select下拉框中第一个值的方法
2018/02/06 Javascript
详解关于webpack多入口热加载很慢的原因
2019/04/24 Javascript
vue-simple-uploader上传成功之后的response获取代码
2020/09/07 Javascript
[01:36:57]【09DOTA2第一视角】小骷髅
2014/04/16 DOTA
[46:55]Ti4 冒泡赛第二轮 LGD vs C9
2014/07/14 DOTA
[00:43]DOTA2小紫本全民票选福利PA至宝全方位展示
2014/11/25 DOTA
Python实现统计文本文件字数的方法
2017/05/05 Python
Python 读取图片文件为矩阵和保存矩阵为图片的方法
2018/04/27 Python
利用nohup来开启python文件的方法
2019/01/14 Python
详解Python做一个名片管理系统
2019/03/14 Python
Python3读写Excel文件(使用xlrd,xlsxwriter,openpyxl3种方式读写实例与优劣)
2020/02/13 Python
python实现猜数游戏
2020/03/27 Python
利用PyTorch实现VGG16教程
2020/06/24 Python
python中Pexpect的工作流程实例讲解
2021/03/02 Python
Michael Kors香港官网:美国奢侈品品牌
2019/12/26 全球购物
租房协议书范例
2014/10/14 职场文书
Rhit高效可视化Nginx日志查看工具
2021/11/01 Servers
仅仅使用 HTML/CSS 实现各类进度条的方式汇总
2021/11/11 HTML / CSS
“鬼灭之刃”热度不减,其成功背后的原因是什么?
2022/03/22 日漫