python opencv 基于ORB的传统图像配准算法

  • 20191013

0.博客背景
  1. 病理切片常见的染色方式有 H&E(苏木精和伊红) 和 IHC(免疫组化),用于检测病理组织的癌变情况。大体情况可以参考此处链接
  2. 由于cycle GAN能够转换图像模态的特性,由此产生了很多基于改进cycle GAN进行染色模态转换(用H&E染色切片生成虚拟IHC染色切片)的论文。
  3. 而使用cycle GAN进行模态转换对数据集的基本要求是同类别同组织结构图像之间的转换,所以对H&E和IHC切图生成的patch需要分类对应。众所周知一张WSI的按照256*256切图一般都会有几千上万张patches,手动分类肯定是个大工程,所以在此产生了一种切图思路。
  4. 利用传统图像算法对H&E和IHC的WSI进行配准,然后按照配准变换在两张WSI上对应切图,这样能够保证大部分patches两两之间的组织形态是一致的,由此着手研究了传统的图像配准算法。
  5. PS :如果有现成分类好的公开数据集就完全没必要这么折腾。
1.基于ORB的图像配准
  1. 配准图像

    由于病理图像结构复杂性,就直接选取对应组织部分的mask进行配准。

    1. H&E缩略图tissue部分mask
      在这里插入图片描述
    2. IHC缩略图tissue部分mask
      在这里插入图片描述
  2. 代码实现

    参考 https://blog.csdn.net/lyxleft/article/details/89476175

    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)
    
    # 叠加配准变换图与基准图
    overlapping = cv.addWeighted(img1, 0.6, imgOut, 0.4, 0)
    io.imsave('HE_2_IHC.png', overlapping)
    # 显示对比
    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.show()
    
  3. 结果展示

    此处结果使用H&E为基准图像,将IHC配准到H&E图像。能够看到效果其实还可以,如果后续切图可以选择mask重叠区域保证两者都能切到有效图像。

    在这里插入图片描述

图像配准是将多幅图像中同一场景的像素点对齐的过程,常用于图像拼接、去除运动模糊等领域。在Python中,OpenCV是一个常用的图像处理库,提供了多种图像配准算法。 下面介绍两种常见的图像配准算法: 1. 特征点匹配 特征点匹配是一种基于关键点的图像配准算法OpenCV提供了SIFT、SURF、ORB等多种特征点检测和描述算法。特征点匹配的基本流程是: 1. 提取源图像和目标图像的特征点和特征描述符。 2. 使用匹配算法(如FLANN)计算两幅图像中的特征点之间的相似度。 3. 通过RANSAC等算法去除误匹配的特征点对。 4. 计算变换矩阵,并将源图像进行配准。 以下是示例代码: ```python import cv2 import numpy as np # 读取源图像和目标图像 src = cv2.imread('src.jpg') dst = cv2.imread('dst.jpg') # 创建SIFT特征点检测器 sift = cv2.xfeatures2d.SIFT_create() # 检测特征点和描述符 keypoints1, descriptors1 = sift.detectAndCompute(src, None) keypoints2, descriptors2 = sift.detectAndCompute(dst, None) # 创建FLANN匹配器 flann = cv2.FlannBasedMatcher() # 匹配特征点 matches = flann.knnMatch(descriptors1, descriptors2, k=2) # 去除误匹配 good_matches = [] for m, n in matches: if m.distance < 0.7 * n.distance: good_matches.append(m) # 计算变换矩阵 src_pts = np.float32([keypoints1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2) dst_pts = np.float32([keypoints2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) # 将源图像进行配准 result = cv2.warpPerspective(src, M, (dst.shape[1], dst.shape[0])) # 显示结果 cv2.imshow('result', result) cv2.waitKey() ``` 2. 相关性匹配 相关性匹配是一种基于像素点的图像配准算法。该算法通过计算两幅图像中像素点之间的相似度,找到最佳的配准位置。常用的相似度计算方法有SAD、SSD、NCC等。以下是示例代码: ```python import cv2 import numpy as np # 读取源图像和目标图像 src = cv2.imread('src.jpg', cv2.IMREAD_GRAYSCALE) dst = cv2.imread('dst.jpg', cv2.IMREAD_GRAYSCALE) # 计算相关性矩阵 result = cv2.matchTemplate(src, dst, cv2.TM_CCOEFF_NORMED) # 找到最佳匹配位置 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) top_left = max_loc bottom_right = (top_left[0] + dst.shape[1], top_left[1] + dst.shape[0]) # 将源图像进行配准 result = cv2.cvtColor(src, cv2.COLOR_GRAY2BGR) cv2.rectangle(result, top_left, bottom_right, (0, 0, 255), 2) # 显示结果 cv2.imshow('result', result) cv2.waitKey() ``` 以上是两种常见的图像配准算法的示例代码,可以根据实际需求选择合适的算法进行使用。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值