Kuunganisha picha kwa kutumia Python na OpenCV kunahusisha kuchanganya picha nyingi ili kuunda mtazamo wa panoramiki au uwanja mpana. Utaratibu huu hutumiwa sana katika upigaji picha ili kuunganisha picha zinazopishana na kuunda panorama isiyo na mshono. Hapa kuna muhtasari wa msingi wa jinsi ya kushona picha kwa kutumia OpenCV:
Uchakataji wa Picha
Pakia picha unazotaka kushona kwa kutumia cv2.imread()
kazi ya OpenCV.
Badilisha picha ziwe za kijivu ukitumia cv2.cvtColor()
ikibidi.
Tambua vipengele muhimu katika picha kwa kutumia kanuni za ugunduzi wa vipengele kama vile SIFT, ORB, au AKAZE.
Ulinganishaji wa Kipengele
Tumia mbinu za kulinganisha vipengele ili kupata pointi zinazolingana kati ya picha.
OpenCV hutoa huduma kama cv2.BFMatcher()
au cv2.FlannBasedMatcher()
kwa kulinganisha vipengele.
Ukadiriaji wa Homografia
Kokotoa matrix ya homografia kwa kutumia pointi zinazolingana zilizopatikana katika hatua ya awali.
Matrix ya homografia inaelezea mabadiliko kati ya picha hizo mbili.
Kupiga na Kushona
Tumia matriki ya homografia kukunja moja ya picha ili kupatana na nyingine.
Kazi cv2.warpPerspective()
inaweza kutumika kwa kusudi hili.
Changanya picha iliyopotoka na picha nyingine ili kuunda panorama iliyounganishwa.
Kuchanganya(Si lazima)
Tumia mbinu za kuchanganya picha ili kuunganisha kwa urahisi picha zilizounganishwa.
Mbinu kama vile uchanganyaji wa mstari au uchanganyaji wa bendi nyingi zinaweza kutumika.
Onyesha au Hifadhi
Onyesha panorama iliyounganishwa ukitumia cv2.imshow()
au uihifadhi kwa kutumia cv2.imwrite()
.
Hapa kuna mfano wa nambari iliyorahisishwa inayoonyesha mchakato wa kushona picha kwa kutumia 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()
Tafadhali kumbuka kuwa mfano huu ni onyesho lililorahisishwa. Kwa mazoezi, huenda ukahitaji kushughulikia masuala kama vile kupanga picha, kuchanganya, na urekebishaji wa upotoshaji ili kufikia picha za panoramiki za ubora wa juu.