python opencv实现图像配准与比较

作者:ericohe 时间:2023-03-01 15:30:24 

本文实例为大家分享了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实现图像配准与比较

来源:https://blog.csdn.net/Ericohe/article/details/113755837

标签:python,图像,配准
0
投稿

猜你喜欢

  • python神经网络学习使用Keras进行简单分类

    2023-09-18 04:37:23
  • Win7彻底卸载Oracle 11g图文步骤(靠谱)

    2024-01-22 22:22:05
  • 利用Python将时间或时间间隔转为ISO 8601格式方法示例

    2022-12-06 22:18:25
  • 垂直无缝滚动图片(兼容性好)实例教程源码下载

    2010-04-06 12:16:00
  • python中如何写类

    2023-09-02 18:19:58
  • Oracle 存储过程总结(一、基本应用)

    2009-07-07 10:21:00
  • Python Django form 组件动态从数据库取choices数据实例

    2024-01-21 17:00:52
  • python实现超市管理系统(后台管理)

    2022-08-17 06:50:22
  • Python四款GUI图形界面库介绍

    2023-02-20 20:12:04
  • vue 打包后的文件部署到express服务器上的方法

    2024-05-09 09:33:05
  • python3 使用traceback定位异常实例

    2023-05-03 12:42:35
  • 8个asp生成随机字符的函数

    2007-08-04 10:17:00
  • JavaScript中利用各种循环进行遍历的方式总结

    2024-04-17 10:10:36
  • CentOS 7下MySQL服务启动失败的快速解决方法

    2024-01-13 16:33:34
  • mysql之TIMESTAMP(时间戳)用法详解

    2024-01-13 16:06:42
  • Server.HTMLEncode让代码在页面里显示为源代码

    2023-11-24 13:40:28
  • Python利用fastapi实现上传文件

    2023-12-09 05:54:40
  • 最全的mysql查询语句整理

    2024-01-27 03:10:01
  • python 爬取英雄联盟皮肤图片

    2021-08-10 10:16:06
  • 关于基于字体大小(em)的设计

    2008-06-17 15:01:00
  • asp之家 网络编程 m.aspxhome.com