加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

检测两张图片之间的相似点然后叠加它们(Python)

发布时间:2020-12-16 23:38:51 所属栏目:Python 来源:网络整理
导读:我有两张相同神经切割的图片,深度略有不同,每个切片上使用不同的染料进行染色.我想覆盖这两个图像,但它们在幻灯片/照片上没有完全对齐,只是为了做到这一点.我想要做的是编写代码,检测两个切片之间的相似形状(即相同的单元格),然后根据这些单元格的位置覆盖图
我有两张相同神经切割的图片,深度略有不同,每个切片上使用不同的染料进行染色.我想覆盖这两个图像,但它们在幻灯片/照片上没有完全对齐,只是为了做到这一点.我想要做的是编写代码,检测两个切片之间的相似形状(即相同的单元格),然后根据这些单元格的位置覆盖图片.有没有办法做到这一点?

我到目前为止的代码是:

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as nb
from skimage import data,io,filters
import skimage.io
from PIL import Image
from scipy import misc
import numpy as np
from skimage.transform import resize 
%matplotlib inline

picture1 = "Images/294_R_C3_5" # define your image pathway

i1 = Image.open(picture1 + ".jpg").convert('L') # open your first image and convert it to greyscale
i1 = i1.point(lambda p: p * 5) # brighten the image
region=i1.crop((600,4000,4000)) # crop the image
region.save(picture1 + ".png","PNG") # save the cropped image as a PNG

i1 = matplotlib.image.imread(picture1 + ".png",format=None) # print the new cropped image
io.imshow(i1)
io.show()
I1 = Image.open(picture1 + ".png") # reopen your image using a different module
I1
picture2 = "Images/294_R_B3_6" #define your image pathway
i2 = Image.open(picture2 + ".jpg").convert('L') # open your second image and convert it to greyscale
i2 = i2.point(lambda p: p * 5)
region=i2.crop((600,4000)) # crop the image
region.save(picture2 + ".png","PNG") # save the cropped image as a PNG

i2 = matplotlib.image.imread(picture2 + ".png",format=None) # print the new cropped image
io.imshow(i2)
io.show()
I2 = Image.open(picture2 + ".png") # open your image using a different module
I2

我尝试过使用skimage,但似乎它收集了太多积分.另外,我不知道如何根据这些点堆叠图像.这是我的代码:

from skimage.feature import ORB
orb = ORB(n_keypoints=800,fast_threshold=0.05)

orb.detect_and_extract(i1)
keypoints1 = orb.keypoints
descriptors1 = orb.descriptors

orb.detect_and_extract(i2)
keypoints2 = orb.keypoints
descriptors2 = orb.descriptors

from skimage.feature import match_descriptors
matches12 = match_descriptors(descriptors1,descriptors2,cross_check=True)

from skimage.feature import plot_matches
fig,ax = plt.subplots(1,1,figsize=(12,12))

plot_matches(ax,i1,i2,keypoints1,keypoints2,matches12)

ax.axis('off');

然后我尝试将它清理一下,但这比我想要的要多得多:

from skimage.transform import ProjectiveTransform
from skimage.measure import ransac

src = keypoints1[matches12[:,0]][:,::-1]
dst = keypoints2[matches12[:,1]][:,::-1]

module_robust12,inliers12 = ransac((src,dst),ProjectiveTransform,min_samples=4,residual_threshold=1,max_trials=300)

fig,matches12[inliers01])

ax.axis('off');

有任何想法吗?谢谢.

解决方法

这种问题经常出现在计算机视觉中.自动执行与全景拼接完全相同的问题.你基本上需要做的就是你几乎完成了什么:

>提取特征点(您正在使用ORB特征 – SIFT可能会给您带来更好的结果,如果重要的话,它只是一个非自由算法)及其描述符
>匹配他们
>使用RANSAC过滤它们
>计算两组点之间的单应性
>缝合

我从未使用skimage进行特征提取/处理,但您的管道看起来很好.我还发现了这个可爱的(由作者写的)图像拼接指南,你会觉得非常有用! https://github.com/scikit-image/scikit-image-paper/blob/master/skimage/pano.txt

它基本上完成了你所做的一半,并完成了接下来的步骤!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读