python opencv实现图像配准与比较
作者:ericohe 发布时间:2023-03-01 15:30:24
标签:python,图像,配准
本文实例为大家分享了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()
结果:
来源:https://blog.csdn.net/Ericohe/article/details/113755837
0
投稿
猜你喜欢
- 本文实例讲述了Python基类函数的重载与调用方法。分享给大家供大家参考。具体分析如下:刚接触Python语言的时间不长,对于这个语言的很多
- 背景在实际项目实施中,会编写很多在服务器执行的作业脚本。程序中凡是涉及到数据库链接、操作系统用户链接、IP地址、主机名称的内容都是敏感信息。
- MySql5.0以后均支持存储过程,最近有空,研究了一下这个格式:CREATE PROCEDURE 过程名 ([过程参数[,...]])[特
- 字符串转list数组str = '1,2,3'arr = str.split(',')gpu_ids分配na
- sort() 函数用于对数组单元从低到高进行排序。rsort() 函数用于对数组单元从高到低进行排序。asort() 函数用于对数组单元从低
- 一 代码编排1 缩进4个空格的缩进(编辑器都可以完成此功能),不要使用Tap,更不能混合使用Tap和空格。2 每行最大长度79,换行可以使用
- 在部署一套内网测试环境时,频繁宕机,开机后不断的吃内存,重启apache之后内存占用会不停的上涨,直到swap用完,直到死机,由于是内网环境
- 本文研究的主要是Django中Forms的使用,具体如下。创建文件do.html{% extends 'base.html'
- <html><head>//搜索暂时没做,数据是出来了,但是却没法显示<link rel="styl
- 在利用javascript内置的eval函数,将json格式的字符串转换成JS对象时,需要用一对"()"先将该字符串包住
- 希望这是window.open()打开fullscreen全屏窗口时无提示关闭父窗口的完美解决方案:我们通常都是用控件法,在父窗口文件里放一
- django ajax发送post请求的两种方法,具体内容如下所述:第一种:将csrf_token放在from表单里 <script&
- os.makedir(path)和os.makedirs(path)今天工作中将hadoop文件同步到服务器磁盘,由于文件类别目录较多,迁移
- 以下是通过Excel 的VBA连接Oracle并操作Oracle相关数据的示例Excel 通过VBA连接数据库需要安装相应的Oracle客户
- 一、导包案例我们导入第三方库,可以使用import。那我们现在有一个需求,我需要动态输入一个模块名,然后导入,这应该怎么做呢?#!/usr/
- 如下所示:def read_data(file_name): ''' file_name:文件地址 '
- python批量命名照片的具体代码,供大家参考,具体内容如下废话不多说,上效果图全部代码from tkinter import *impor
- 由于XML本身的诸多优点,XML技术已被广泛的使用,目前的好多软件技术同XML紧密相关,比如微软的.net 平台对xml提供了强大的支持,提
- 今天在这里,不以设计师的身份,而从一个普通用户的角度和各位聊聊设计中蕴含的那份情感,关于情感再产品设计中的意义,聊聊设计中的那份源于“心”的
- 新手,参考了以下链接:python opencv在图像上画矩形(已验证)本文可以实现在指定图片上动态绘制圆和矩形。import cv2imp