python保存多张图片_使用python实现两张图片对齐并保存

该博客介绍了如何使用Python的OpenCV库进行图像对齐。通过ORB特征检测和匹配,找到最佳匹配点,然后应用Homography来实现两张图片的对齐。最后,将对齐后的图像保存到磁盘。
摘要由CSDN通过智能技术生成

输入图像:

241175ee7bf66522cf9755c9a0b39eaf.png

1e8d6ee357135c5a0d40b5a5d452812b.png

实例代码:

from __future__ import print_function

import cv2

import numpy as np

MAX_FEATURES = 500

GOOD_MATCH_PERCENT = 0.15

def alignImages(im1, im2):

# Convert images to grayscale

im1Gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)

im2Gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)

# Detect ORB features and compute descriptors.

orb = cv2.ORB_create(MAX_FEATURES)

keypoints1, descriptors1 = orb.detectAndCompute(im1Gray, None)

keypoints2, descriptors2 = orb.detectAndCompute(im2Gray, None)

# Match features.

matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)

matches = matcher.match(descriptors1, descriptors2, None)

# Sort matches by score

matches.sort(key=lambda x: x.distance, reverse=False)

# Remove not so good matches

numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)

matches = matches[:numGoodMatches]

# Draw top matches

imMatches = cv2.drawMatches(im1, keypoints1, im2, keypoints2, matches, None)

cv2.imwrite("matches.jpg", imMatches)

# Extract location of good matches

points1 = np.zeros((len(matches), 2), dtype=np.float32)

points2 = np.zeros((len(matches), 2), dtype=np.float32)

for i, match in enumerate(matches):

points1[i, :] = keypoints1[match.queryIdx].pt

points2[i, :] = keypoints2[match.trainIdx].pt

# Find homography

h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)

# Use homography

height, width, channels = im2.shape

im1Reg = cv2.warpPerspective(im1, h, (width, height))

return im1Reg, h

if __name__ == '__main__':

# Read reference image

refFilename = "3.jpg"

print("Reading reference image : ", refFilename)

imReference = cv2.imread(refFilename, cv2.IMREAD_COLOR)

# Read image to be aligned

imFilename = "5.jpg"

print("Reading image to align : ", imFilename);

im = cv2.imread(imFilename, cv2.IMREAD_COLOR)

print("Aligning images ...")

# Registered image will be resotred in imReg.

# The estimated homography will be stored in h.

imReg, h = alignImages(im, imReference)

# Write aligned image to disk.

outFilename = "aligned.jpg"

print("Saving aligned image : ", outFilename);

cv2.imwrite(outFilename, imReg)

# Print estimated homography

print("Estimated homography : \n", h)

运行结果:

187d4a2280ab7b98fa2ec99cb2f0c1ad.png

081d07fd735a44a76a7b13572e688f70.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要对两张图片中的两个点进行对齐,可以使用OpenCV库中的`cv2.findHomography`函数。以下是一个简单的示例代码: ```python import cv2 import numpy as np # 读取两张图片 img1 = cv2.imread('image1.jpg') img2 = cv2.imread('image2.jpg') # 在第一张图片上选择一个点 points1 = [] def select_point1(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: points1.append((x, y)) cv2.circle(img1, (x, y), 5, (0, 0, 255), -1) cv2.imshow('image1', img1) cv2.imshow('image1', img1) cv2.setMouseCallback('image1', select_point1) cv2.waitKey(0) cv2.destroyAllWindows() # 在第二张图片上选择一个点 points2 = [] def select_point2(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: points2.append((x, y)) cv2.circle(img2, (x, y), 5, (0, 0, 255), -1) cv2.imshow('image2', img2) cv2.imshow('image2', img2) cv2.setMouseCallback('image2', select_point2) cv2.waitKey(0) cv2.destroyAllWindows() # 对齐两个点 if len(points1) == 1 and len(points2) == 1: src = np.float32([points1[0], points2[0]]) dst = np.float32([[0, 0], [img1.shape[1], 0]]) M, _ = cv2.findHomography(src, dst, cv2.RANSAC, 5.0) aligned_img = cv2.warpPerspective(img2, M, (img1.shape[1], img1.shape[0])) cv2.imshow('aligned', aligned_img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在这个示例中,我们首先使用`cv2.imread`函数读取两张图片,然后分别在两张图片上选择一个点。当用户选择两个点时,我们使用`cv2.findHomography`函数计算单应矩阵,并使用`cv2.warpPerspective`函数对第二张图像进行透视变换,使两个点对齐。最后,我们使用`cv2.imshow`函数显示变换后的图像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值