python-3.x - 在 openCV 中特定坐标处的另一幅图像上显示图像

标签 python-3.x opencv image-processing aruco opencv4

我正在尝试在特定坐标处的另一张图像上显示图像。我已经使用网络摄像头检测到了 aruco 标记,我想在 aruco 标记上显示另一个图像。可以移动 aruco 标记,并且叠加图像应与标记一起移动。

有各种绘图功能并将文本输入到图像中。我尝试过图像叠加和图像单应性。

我可以获得角落的坐标。
是否有任何功能可以在这些坐标处插入图像?

import cv2
import cv2.aruco as aruco
import glob

markerLength = 0.25

cap = cv2.VideoCapture(0)

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)

objpoints = [] 
imgpoints = []

images = glob.glob('calib_images/*.jpg')

for fname in images:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    ret, corners = cv2.findChessboardCorners(gray, (7,6),None)

    if ret == True:
        objpoints.append(objp)

        corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
        imgpoints.append(corners2)
        img = cv2.drawChessboardCorners(img, (7,6), corners2,ret)


ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)

calibrationFile = "calibrationFileName.xml"
calibrationParams = cv2.FileStorage(calibrationFile, cv2.FILE_STORAGE_READ) 
camera_matrix = calibrationParams.getNode("cameraMatrix").mat() 
dist_coeffs = calibrationParams.getNode("distCoeffs").mat() 

while(True):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)
    arucoParameters =  aruco.DetectorParameters_create()

    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=arucoParameters)
    if np.all(ids != None):
        rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corners, markerLength, mtx, dist) 
        axis = aruco.drawAxis(frame, mtx, dist, rvec, tvec, 0.3) 
        print(ids)
        display = aruco.drawDetectedMarkers(axis, corners)
        display = np.array(display)
    else:
        display = frame

    cv2.imshow('Display',display)
    if cv2.waitKey(1) & 0xFF == ord('q'):
            break

cap.release()
cv2.destroyAllWindows()```

最佳答案

替换图像的一部分

import cv2
import numpy as np

img1 = cv2.imread('Desert.jpg')
img2 = cv2.imread('Penguins.jpg')

img3 = img1.copy()
# replace values at coordinates (100, 100) to (399, 399) of img3 with region of img2
img3[100:400,100:400,:] = img2[100:400,100:400,:]
cv2.imshow('Result1', img3)

enter image description here

对两个图像进行 alpha 混合
alpha = 0.5
img3 = np.uint8(img1*alpha + img2*(1-alpha))
cv2.imshow('Result2', img3)

enter image description here

关于python-3.x - 在 openCV 中特定坐标处的另一幅图像上显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56002672/

相关文章:

python-3.x - 在KubernetesPods上设置HTTP请求设置

c++ - OpenCV - Mat 的零填充

image - 洪水填充算法 - 忽略边缘

python - 比较python中的计算机视觉库

python - 使用 openCV : empty video 在 python 中保存视频捕获

opencv - accumulateWeighted导致图像变暗

algorithm - 如何将图像中的对象与背景分开?

javascript - 如何使用 javascript、JSON、python 和 mongodb 之间的日期时间?

python-3.x - 设置 Nginx 超时和默认 JSON 响应

python - 如何在 __new__ 中正确构建一个具有 type(3 args) 和 2 个祖先的类?