美人鱼图像双目配准-Mermaid

2024-03-26 13:52

本文主要是介绍美人鱼图像双目配准-Mermaid,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:

我在进行一项双目测距的项目,已经通过matlab 进行了相机标定,如果手动选择左右图像里的相同物体,是可以给出可接受距离的。

但是现在我希望能够让左视图的坐标点和右视图的坐标点进行匹配(如下图)

于是,我分别使用了ORB 关键点匹配进行投射变换、美人鱼图像配准得到变换矩阵

ORB 关键点匹配

思路:

先进行关键点匹配,由于双目图像已经是标定后的,所以一般来说可以认为图像是水平的。

那么可以过滤一下匹配点,把斜率超过0.1的全部筛掉,只保留水平匹配线。

效果:

不是那么理想,进行变换后,发现由于物体的远近,导致配准时候只保留了最前端物体的匹配。所有只有部分数据是可以进行距离测量的。

代码:

'''
-*- coding: utf-8 -*-
@File  : match.py
@Author: Shanmh
@Time  : 2024/03/22 上午9:50
@Function: 关键点匹配进行投射变换
'''import cv2
import imutils
import numpy as np
import math
import time
import osdef align_images(image, template, maxFeatures=100000, keepPercent=1,debug=False):# use the homography matrix to align the images(h, w) = template.shape[:2]print(image.shape)print(template.shape)# convert both the input image and template to grayscaleimageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)# use ORB to detect keypoints and extract (binary) local# invariant featuresorb = cv2.ORB_create(nfeatures = maxFeatures,scaleFactor = 1.2,nlevels = 32,edgeThreshold = 16,firstLevel = 2,WTA_K = 4,scoreType = 1,patchSize = 8,fastThreshold = 20)(kpsA, descsA) = orb.detectAndCompute(imageGray, None)(kpsB, descsB) = orb.detectAndCompute(templateGray, None)method = cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMINGmatcher = cv2.DescriptorMatcher_create(method)matches = matcher.match(descsA, descsB, None)# sort the matches by their distance (the smaller the distance,matchesall = sorted(matches, key=lambda x: x.distance)keep = int(len(matchesall) * 0.5)matches=matchesall[:keep]new_match=[]image_show=np.concatenate((image,template),axis=1)# 匹配角度过滤for match in matches:query_idx = match.queryIdxtrain_idx = match.trainIdxdistance = match.distancept1=(int(kpsA[query_idx].pt[0]),int(kpsA[query_idx].pt[1]))pt2=int(kpsB[train_idx].pt[0]+w),int(kpsB[train_idx].pt[1])dw=abs(pt1[0]-pt2[0])dh=abs(pt1[1]-pt2[1])if dh/dw<0.1  :if debug:# cv2.line(image_show,pt1,pt2,(0,0,255), thickness=1)cv2.circle(image_show, pt1, 2, (0, 255, 0), -1)cv2.circle(image_show, pt2, 2, (0, 255, 0), -1)new_match.append(match)if debug:cv2.imshow("show",image_show)matches=new_match# check to see if we should visualize the matched keypointsif debug:matchedVis = cv2.drawMatches(image, kpsA, template, kpsB,matches, None)matchedVis = imutils.resize(matchedVis, width=1000)cv2.imshow("Matched Keypoints", matchedVis)cv2.waitKey(0)ptsA = np.zeros((len(matches), 2), dtype="float")ptsB = np.zeros((len(matches), 2), dtype="float")for (i, m) in enumerate(matches):ptsA[i] = kpsA[m.queryIdx].ptptsB[i] = kpsB[m.trainIdx].pt(H, mask) = cv2.findHomography(ptsA, ptsB, method=cv2.RANSAC)(h, w) = template.shape[:2]print(template.shape)# aligned2 = cv2.warpPerspective(image, H, (w, h))return Hif __name__ == '__main__':passimgl=cv2.imread("../test240421/llll.jpg")[100:-100,:,:]imgl=cv2.imread("/home/hxzh/PycharmProjects/StereoVision/im1E.png")imgl=cv2.resize(imgl,(900,720))imgr=cv2.imread("../test240421/rrrr.jpg")[100:-100,:,:]imgr=cv2.imread("/home/hxzh/PycharmProjects/StereoVision/im1L.png")imgr=cv2.resize(imgr,(900,720))homography=align_images(imgl,imgr,debug=True)image_con = np.concatenate((imgl, imgr), axis=1)# 定义imgl上的点pointl=[607,425]srcPoint = np.array([[pointl]], dtype=np.float32)# 进行坐标变换dstPoint = cv2.perspectiveTransform(srcPoint, homography)[0][0]print(dstPoint)cv2.line(image_con, pointl, [int(dstPoint[0]+imgr.shape[1]),int(dstPoint[1])], (0, 0, 255), thickness=1)cv2.imshow("aaaaaaaa",image_con)align = cv2.warpPerspective(imgl, homography, imgr.shape[:2][::-1])cv2.imshow("imgl",imgl)cv2.imshow("imgr",imgr)cv2.imshow("align",align)cv2.imwrite("testimg.jpg",align)cv2.imwrite("imgr.jpg",imgr)cv2.imwrite("imgl.jpg",imgl)print(align.shape)cv2.waitKey(0)

美人鱼配准

github:GitHub - uncbiag/mermaid: Image registration using pytorch

doc:mermaid: iMagE Registration via autoMAtIc Differentiation — mermaid 0.1 documentation

思路:

使用美人鱼进行迭代配准,得到转换矩阵,计算对应坐标

效果:

可以看到红色栅格线,已经进行了流变换,保证了左右视图物体对应。对于这种背景单一,只有一个物体的效果还是不错的。但是这种对左右视图要求比较高,左右视图对应物体最好不要超过一个网格的距离。

代码:

需要先参考doc配置好环境,然后再jupyter运行,迭代到 relF 为0 就差不多了

'''pip install -U numpy==1.22.4'''import time
import cv2
import numpy as np
import matplotlib.pyplot as plt
import mermaid.module_parameters as pars
import mermaid.example_generation as EGtime.sleep(10)
def show_warped_with_grid(I, phi, title):plt.imshow(I[0,0,:,:] ,cmap='gray')plt.contour(phi[0, 0, :, :],np.linspace(-1, 1, 20),colors='r',linestyles='solid',linewidths=0.5)plt.contour(phi[0, 1, :, :],np.linspace(-1, 1, 20),colors='r',linestyles='solid',linewidths=0.5)
def show_image(I, title):plt.imshow(I[0,0,:,:], cmap='gray')plt.axis('off')plt.title(title)#创建大小黑格
params = pars.ParameterDict()
use_synthetic_test_case = True
add_noise_to_bg = True
dim = 2
sz = 64
szEx = np.tile(sz, dim)
I0_syn, I1_syn, spacing_syn = EG.CreateSquares(dim, add_noise_to_bg).create_image_pair(szEx, params)
print("I0_syn",I0_syn.dtype,I0_syn)
print("I1_syn",I1_syn.dtype,I1_syn)
print("spacing_syn",spacing_syn.dtype ,spacing_syn)print('Size I0:', I0_syn.shape)
print('Size I1:', I1_syn.shape)
plt.figure(figsize=(10,4))
plt.subplot(121)
plt.imshow(I0_syn[0,0,:,:],cmap='gray')
plt.title('Source')
plt.axis('off');
plt.subplot(122)
plt.imshow(I1_syn[0,0,:,:],cmap='gray')
plt.title('Target')
plt.axis('off');

img=cv2.imread("/home/hxzh/PycharmProjects/StereoVision/im1E.png",0)
targetimg=cv2.imread("/home/hxzh/PycharmProjects/StereoVision/im1L.png",0)img=cv2.resize(img,(640,640))/255.
targetimg=cv2.resize(targetimg,(640,640))/255.print(img.shape)
print(targetimg.shape)I0_syn = img.reshape([1, 1] + list(img.shape)).astype(np.float32)
I1_syn = (targetimg.reshape([1, 1] + list(targetimg.shape))).astype(np.float32)
spacing_syn=spacing = 1. / (np.array(I0_syn.shape)[2::] - 1)   print("I0_syn",I0_syn.dtype,I0_syn)
print("I1_syn",I1_syn.dtype,I1_syn)
print("spacing_syn",spacing_syn.dtype ,spacing_syn)print('Size I0:', I0_syn.shape)
print('Size I1:', I1_syn.shape)
plt.figure(figsize=(10,4))
plt.subplot(121)
plt.imshow(I0_syn[0,0,:,:],cmap='gray')
plt.title('Source')
plt.axis('off');
plt.subplot(122)
plt.imshow(I1_syn[0,0,:,:],cmap='gray')
plt.title('Target')
plt.axis('off');
import time#LDDDM (shooting, scalar momentum)
import torch
import mermaid.utils as utils
import mermaid.visualize_registration_results as vizreg
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as pltimport mermaid.simple_interface as SIimport cv2si = SI.RegisterImagePair()
si.print_available_models()I_0 = torch.from_numpy(I0_syn)
I_1 = torch.from_numpy(I1_syn)
phi = si.get_map()
# I_W = utils.compute_warped_image_multiNC(I_0,
#                                          phi,
#                                          spacing_syn,
#                                          spline_order=1)
# vizreg.show_current_images(len(si.get_history()['energy']),
#                            I_0,
#                            I_1,
#                            I_W,
#                            phiWarped=phi)si.register_images(I0_syn, I1_syn, spacing_syn, model_name='lddmm_shooting_scalar_momentum_image',nr_of_iterations=100,use_multi_scale=True,visualize_step=True,optimizer_name='sgd',learning_rate=0.5,rel_ftol=1e-7,json_config_out_filename=('test2d_tst.json', 'test2d_tst_with_comments.json'),params='./mermaid_demos/2d_example_synth/lddmm_setting.json',
#                    params="test/json/test_lddmm_shooting_scalar_momentum_image_multi_scale_config.json",recording_step=1)

参考:

给几个双目相机标定的参考文章:

双目摄像头Matlab参数定标_matlab双目相机标定参数-CSDN博客

YOLO v5与双目测距结合,实现目标的识别和定位测距_yolo双目测距-CSDN博客

这篇关于美人鱼图像双目配准-Mermaid的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/848722

相关文章

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

c/c++的opencv图像金字塔缩放实现

《c/c++的opencv图像金字塔缩放实现》本文主要介绍了c/c++的opencv图像金字塔缩放实现,通过对原始图像进行连续的下采样或上采样操作,生成一系列不同分辨率的图像,具有一定的参考价值,感兴... 目录图像金字塔简介图像下采样 (cv::pyrDown)图像上采样 (cv::pyrUp)C++ O

Python+wxPython构建图像编辑器

《Python+wxPython构建图像编辑器》图像编辑应用是学习GUI编程和图像处理的绝佳项目,本教程中,我们将使用wxPython,一个跨平台的PythonGUI工具包,构建一个简单的... 目录引言环境设置创建主窗口加载和显示图像实现绘制工具矩形绘制箭头绘制文字绘制临时绘制处理缩放和旋转缩放旋转保存编

python+OpenCV反投影图像的实现示例详解

《python+OpenCV反投影图像的实现示例详解》:本文主要介绍python+OpenCV反投影图像的实现示例详解,本文通过实例代码图文并茂的形式给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前言二、什么是反投影图像三、反投影图像的概念四、反向投影的工作原理一、利用反向投影backproj

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

OpenCV图像形态学的实现

《OpenCV图像形态学的实现》本文主要介绍了OpenCV图像形态学的实现,包括腐蚀、膨胀、开运算、闭运算、梯度运算、顶帽运算和黑帽运算,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起... 目录一、图像形态学简介二、腐蚀(Erosion)1. 原理2. OpenCV 实现三、膨胀China编程(

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像

基于WinForm+Halcon实现图像缩放与交互功能

《基于WinForm+Halcon实现图像缩放与交互功能》本文主要讲述在WinForm中结合Halcon实现图像缩放、平移及实时显示灰度值等交互功能,包括初始化窗口的不同方式,以及通过特定事件添加相应... 目录前言初始化窗口添加图像缩放功能添加图像平移功能添加实时显示灰度值功能示例代码总结最后前言本文将

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境