Gambar jahitan nggunakake Python lan OpenCV melu nggabungake macem-macem gambar kanggo nggawe tampilan panorama utawa sudhut-lapangan. Proses iki umume digunakake ing fotografi kanggo nggabungake gambar sing tumpang tindih lan nggawe panorama sing mulus. Mangkene garis dhasar babagan carane nggawe gambar nganggo OpenCV:
Gambar Preprocessing
Muat gambar sing pengin digawe nganggo cv2.imread()
fungsi OpenCV.
Ngonversi gambar menyang grayscale nggunakake cv2.cvtColor()
yen perlu.
Ndeteksi fitur utama ing gambar nggunakake algoritma deteksi fitur kaya SIFT, ORB, utawa AKAZE.
Fitur Matching
Gunakake teknik pencocokan fitur kanggo nemokake titik sing cocog ing antarane gambar.
OpenCV nyedhiyakake fungsi kaya cv2.BFMatcher()
utawa cv2.FlannBasedMatcher()
kanggo cocog fitur.
Estimasi Homografi
Etung matriks homografi nggunakake titik sing cocog sing ditemokake ing langkah sadurunge.
Matriks homografi nggambarake transformasi antarane rong gambar kasebut.
Warping lan Jahitan
Gunakake matriks homography kanggo warp salah siji saka gambar kanggo selaras karo liyane.
Fungsi kasebut cv2.warpPerspective()
bisa digunakake kanggo tujuan kasebut.
Gabungke gambar sing njedhul karo gambar liyane kanggo nggawe panorama sing dijahit.
Campuran(Opsional)
Gunakake teknik campuran gambar kanggo nggabungake gambar sing dijahit kanthi lancar.
Teknik kaya campuran linear utawa campuran multi-band bisa digunakake.
Tampilan utawa Simpen
Tampilake panorama sing dijahit nganggo cv2.imshow()
utawa simpen nganggo cv2.imwrite()
.
Iki conto kode sing disederhanakake sing nuduhake proses jahitan gambar nggunakake OpenCV:
import cv2
# Load images
image1 = cv2.imread('image1.jpg')
image2 = cv2.imread('image2.jpg')
# Convert images to grayscale
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
# Detect key features and descriptors
orb = cv2.ORB_create()
keypoints1, descriptors1 = orb.detectAndCompute(gray1, None)
keypoints2, descriptors2 = orb.detectAndCompute(gray2, None)
# Feature matching
matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = matcher.match(descriptors1, descriptors2)
# Apply ratio test to filter good matches
matches = [match for match in matches if match.distance < 0.7 * max(len(matches), 1)]
# Find corresponding points
src_pts = [keypoints1[match.queryIdx].pt for match in matches]
dst_pts = [keypoints2[match.trainIdx].pt for match in matches]
# Calculate homography matrix
homography, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC)
# Warp and stitch images
stitched_image = cv2.warpPerspective(image1, homography,(image1.shape[1] + image2.shape[1], image1.shape[0]))
stitched_image[0:image2.shape[0], 0:image2.shape[1]] = image2
# Display or save the stitched image
cv2.imshow('Stitched Image', stitched_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Elinga yen conto iki minangka demonstrasi sing disederhanakake. Ing praktik, sampeyan bisa uga kudu ngatasi masalah kayata keselarasan gambar, campuran, lan koreksi distorsi kanggo entuk gambar panorama sing berkualitas.