Py open c4 сшивает 2 изображения с веб-камеры в перекрывающийся панорамный поток – не работаетPython

Программы на Python
Ответить
Anonymous
 Py open c4 сшивает 2 изображения с веб-камеры в перекрывающийся панорамный поток – не работает

Сообщение Anonymous »

поэтому я пытаюсь снять 2 потока с веб-камеры (wecams фиксированы, разделены на расстоянии 200 мм друг от друга) есть определенная зона перекрытия... я хочу сделать панорамный вид и использовать зону перекрытия для обнаружения расстояния, но пока не получается даже получить панорамное сшивание . перепробовал все типы кода и даже не закрыл ресилсты, даже похоже, что это должно работать
введите здесь описание изображения
import cv2
import numpy as np

# Function to detect ORB keypoints and descriptors
def detect_and_compute(image):
orb = cv2.ORB_create(nfeatures=10000) # Maximize the number of keypoints
keypoints, descriptors = orb.detectAndCompute(image, None)
return keypoints, descriptors

# Function to match descriptors using Brute Force Matcher
def match_keypoints(des1, des2):
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x: x.distance) # Sort matches by distance
return matches

# Function to stitch images together
def stitch_images(img1, img2, matches, kp1, kp2):
# 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, :] = kp1[match.queryIdx].pt
points2[i, :] = kp2[match.trainIdx].pt

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

# Warp img2 to img1's coordinate space
height, width = img1.shape[:2]
img2_warped = cv2.warpPerspective(img2, H, (width, height))

# Combine the images (horizontal concatenation)
stitched_img = np.hstack([img1, img2_warped])

return stitched_img

# Capture two webcam streams
cap1 = cv2.VideoCapture(0) # First webcam
cap2 = cv2.VideoCapture(1) # Second webcam

while True:
ret1, frame1 = cap1.read()
ret2, frame2 = cap2.read()

if not ret1 or not ret2:
print("Failed to capture from webcams.")
break

# Detect and compute keypoints and descriptors
kp1, des1 = detect_and_compute(frame1)
kp2, des2 = detect_and_compute(frame2)

# Match keypoints
matches = match_keypoints(des1, des2)

# Draw the matches for debugging (only a subset of the best matches)
match_img = cv2.drawMatches(frame1, kp1, frame2, kp2, matches[:20], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)

# Stitch images together
stitched_img = stitch_images(frame1, frame2, matches, kp1, kp2)

# Stack original frames (frame1 and frame2) vertically for debugging
stacked_img = np.vstack([frame1, frame2])

# Display original streams, the match visualization, and the stitched panoramic image
cv2.imshow("Matches", match_img)
cv2.imshow("Original Streams", stacked_img)
cv2.imshow("Stitched Panoramic", stitched_img)

# Exit loop on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the webcams and close all windows
cap1.release()
cap2.release()
cv2.destroyAllWindows()



Подробнее здесь: https://stackoverflow.com/questions/792 ... ot-working
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»