成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

圖像配準:基于 OpenCV 的高效實現

人工智能
本文我將對圖像配準進行一個簡單概述,展示一個最小的 OpenCV 實現,并展示一個可以使配準過程更加高效的簡單技巧。

在這篇文章中,我將對圖像配準進行一個簡單概述,展示一個最小的 OpenCV 實現,并展示一個可以使配準過程更加高效的簡單技巧。

什么是圖像配準

圖像配準被定義為將不同成像設備或傳感器在不同時間和角度拍攝的兩幅或多幅圖像,或來自同一場景的兩幅或多幅圖像疊加起來,以幾何方式對齊圖像以進行分析的過程(Zitová 和 Flusser,2003 年)。

百度百科給出的解釋

圖像配準:圖像配準(Image registration)就是將不同時間、不同傳感器(成像設備)或不同條件下(天候、照度、攝像位置和角度等)獲取的兩幅或多幅圖像進行匹配、疊加的過程,它已經被廣泛地應用于遙感數據分析、計算機視覺、圖像處理等領域。

醫學科學、遙感和計算機視覺都使用圖像配準。

有兩種主要方法:

  • 經典計算機視覺方法(使用 OpenCV)——我們將在本文中關注的內容
  • 基于深度學習的方法

雖然后者可以更好地工作,但它可能需要一些“域”適應(在你的數據上微調神經網絡)并且可能計算量太大。

使用 OpenCV 進行圖像配準

基于特征的方法:由單應變換關聯的圖像對

此操作試圖發現兩張照片之間的匹配區域并在空間上對齊它們以最大限度地減少錯誤。

我們的目標是找到一個單應性矩陣 H,它告訴我們需要如何修改其中一張圖像,使其與另一張圖像完美對齊。

第 1 步:關鍵點檢測

關鍵點定義了圖像中一個獨特的小區域(角、邊緣、圖案)。關鍵點檢測器的一個重要方面是找到的區域應該對圖像變換(例如定位、比例和亮度)具有魯棒性,因為這些區域很可能出現在我們試圖對齊的兩個圖像中。有許多執行關鍵點檢測的算法,例如 SIFT、ORB、AKAZE、SURF 等。

第 2 步:特征匹配

現在我們必須匹配來自兩個圖像的關鍵點,這些關鍵點實際上對應于同一點。

第 3 步:單應性

單應性通常由一個 3x3 矩陣表示,它描述了應該應用于一個圖像以與另一個圖像對齊的幾何變換。

第 4 步:圖像變形

找到單應性矩陣后,我們可以用它來對齊圖像。下面是該過程的代碼:


import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img1 = cv.imread('image1.jpg', cv.IMREAD_GRAYSCALE)  # referenceImage
img2 = cv.imread('image2.jpg', cv.IMREAD_GRAYSCALE)  # sensedImage

# Initiate SIFT detector
sift_detector = cv.SIFT_create()
# Find the keypoints and descriptors with SIFT
kp1, des1 = sift_detector.detectAndCompute(img1, None)
kp2, des2 = sift_detector.detectAndCompute(img2, None)

# BFMatcher with default params
bf = cv.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)

# Filter out poor matches
good_matches = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good_matches.append(m)

matches = 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, :] = kp1[match.queryIdx].pt
    points2[i, :] = kp2[match.trainIdx].pt

# Find homography
H, mask = cv2.findHomography(points1, points2, cv2.RANSAC)

# Warp image 1 to align with image 2
img1Reg = cv2.warpPerspective(img1, H, (img2.shape[1], img2.shape[0]))
cv.imwrite('aligned_img1.jpg', img1Reg)The problem is that this matrix H is found via a compute-intensive optimization process.

高效的圖像配準

無論您為每個步驟選擇的參數如何,對執行時間影響最大的是圖像的分辨率。您可以大幅調整它們的大小,但如果您需要對齊的圖像具有原始分辨率,會發生什么情況?

幸運的是,有辦法解決這個問題。事實證明,您可以計算低分辨率圖像的變換,然后調整此變換以適用于全分辨率圖像。

詳細步驟:

  1. 調整圖像大小
  2. 在低分辨率圖像上計算矩陣 H
  3. 變換矩陣 H 使其適用于全分辨率圖像
  4. 將新矩陣應用于原始圖像。

第 3 步可能是這里最不明顯的部分,所以讓我們看看它是如何工作的:

我們想要調整在低分辨率圖像上計算的變換以適用于高分辨率圖像。因此,我們希望高分辨率圖像中的每個像素執行以下操作:

幸運的是,所有這些步驟都只是矩陣乘法,我們可以將所有這些步驟組合在一個單一的轉換中。

設 H 為您計算出的變換。您可以將 H 乘以另一個單應性 A,得到 AH = H',其中 H' 是進行兩種變換的單應性,相當于先應用 H,然后應用 A。

下面是詳細代碼:


import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img1 = cv.imread('image1.jpg', cv.IMREAD_GRAYSCALE)  # referenceImage
img2 = cv.imread('image2.jpg', cv.IMREAD_GRAYSCALE)  # sensedImage

#  Resize the image by a factor of 8 on each side. If your images are 
# very high-resolution, you can try to resize even more, but if they are 
# already small you should set this to something less agressive.
resize_factor = 1.0/8.0

img1_rs = cv.resize(img1, (0,0), fx=resize_factor, fy=resize_factor)
img2_rs = cv.resize(img2, (0,0), fx=resize_factor, fy=resize_factor)

# Initiate SIFT detector 
sift_detector = cv.SIFT_create()

# Find the keypoints and descriptors with SIFT on the lower resolution images
kp1, des1 = sift_detector.detectAndCompute(img1_rs, None)
kp2, des2 = sift_detector.detectAndCompute(img2_rs, None)

# BFMatcher with default params
bf = cv.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)

# Filter out poor matches
good_matches = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good_matches.append(m)

matches = 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, :] = kp1[match.queryIdx].pt
    points2[i, :] = kp2[match.trainIdx].pt

# Find homography
H, mask = cv2.findHomography(points1, points2, cv2.RANSAC)

# Get low-res and high-res sizes
low_height, low_width = img1_rs.shape
height, width = img1.shape
low_size = np.float32([[0, 0], [0, low_height], [low_width, low_height], [low_width, 0]])
high_size = np.float32([[0, 0], [0, height], [width, height], [width, 0]])

# Compute scaling transformations
scale_up = cv.getPerspectiveTransform(low_size, high_size)
scale_down = cv.getPerspectiveTransform(high_size, low_size)

#  Combine the transformations. Remember that the order of the transformation 
# is reversed when doing matrix multiplication
# so this is actualy scale_down -> H -> scale_up
h_and_scale_up = np.matmul(scale_up, H)
scale_down_h_scale_up = np.matmul(h_and_scale_up, scale_down)

# Warp image 1 to align with image 2
img1Reg = cv2.warpPerspective(
            img1, 
            scale_down_h_scale_up, 
            (img2.shape[1], img2.shape[0])
          )

cv.imwrite('aligned_img1.jpg', img1Reg)
責任編輯:趙寧寧 來源: 小白玩轉Python
相關推薦

2024-01-08 08:23:08

OpenCV機器學習計算機視覺

2025-05-09 10:01:06

EasyExcelMySQLMySQL8

2017-05-22 13:15:45

TensorFlow深度學習

2018-07-19 15:13:15

深度學習圖像

2017-05-12 16:25:44

深度學習圖像補全tensorflow

2015-05-28 15:01:23

程序員高效程序員

2018-08-03 09:42:01

人工智能深度學習人臉識別

2022-12-18 19:49:45

AI

2023-04-11 08:00:00

PythonOtsu閾值算法圖像背景分割

2024-09-04 15:42:38

OpenCV開發Python

2025-03-25 08:30:00

OpenCV計算機視覺圖像識別

2022-09-29 23:53:06

機器學習遷移學習神經網絡

2022-11-10 08:48:20

開源數據湖Arctic

2010-07-02 08:39:02

SQLServer數據

2022-11-16 09:03:35

Sentry前端監控

2025-04-10 08:20:00

OpenCV圖像處理計算機視覺

2024-10-10 15:51:50

2021-12-08 11:30:41

Python全景拼接代碼

2019-10-10 09:00:30

云端云遷移云計算

2022-03-01 10:51:15

領導者CIOIT團隊
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美成人免费在线视频 | 久久久精品一区二区三区四季av | 亚洲一区二区三区免费在线观看 | 日韩黄色免费 | 中文字幕不卡在线观看 | 精品国产91乱码一区二区三区 | 成人在线免费网站 | 中文字幕日韩在线观看 | 久久久久久网站 | 久久久久久久久99精品 | 国产亚洲精品区 | 久久亚洲综合 | 日韩视频在线观看一区二区 | 亚洲高清在线视频 | 91精品国产91久久久久久最新 | 日韩欧美视频 | 精品一区二区三区四区五区 | 欧美一区二区在线 | 91精品国产手机 | 亚洲精品视频在线播放 | 国产成人一区二区三区精 | 四虎影院在线观看免费视频 | 国产午夜精品一区二区三区 | 国产精品国产馆在线真实露脸 | 精品在线一区二区三区 | 久久久这里只有17精品 | 国产精品视频在线播放 | 国产精品1区2区 | 欧美亚洲高清 | 精品国产一区二区三区久久 | 欧洲精品码一区二区三区免费看 | 不卡在线视频 | 午夜在线精品偷拍 | 西西裸体做爰视频 | 色综合99 | 日本一二区视频 | 国产一在线 | 精品香蕉一区二区三区 | 中文字幕在线免费视频 | 日本成人二区 | 国产美女永久免费无遮挡 |