微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 cv2.aruco.estimatePoseSingleMarkers 函数python估计 aruco 标记的姿势?

如何解决如何使用 cv2.aruco.estimatePoseSingleMarkers 函数python估计 aruco 标记的姿势?

以下是我编写的代码,它从目录 .jpg 读取 './pose_images' 图像并检测标记 id 和角点。

from absl import flags,app
from pathlib import Path
from tqdm import tqdm,trange
import cv2
import sys
import joblib
import os
import numpy as np

FLAGS = flags.FLAGS
flags.DEFINE_string('aruco_dict','7x7','aruco dict. used for board generation DEFAULT:4x4')
flags.DEFINE_integer('columns',7,'number of columns in board')
flags.DEFINE_integer('rows','number of rows in board')


def read_chessboards(images):
    """
    Charuco base pose estimation.
    """
    print("POSE ESTIMATION STARTS:")
    allCorners = []
    allIds = []
    decimator = 0
    # SUB PIXEL CORNER DETECTION CRITERION
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,100,0.00001)
    # aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)
    if '4x4' in FLAGS.aruco_dict:
        aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_1000)
        board = cv2.aruco.CharucoBoard_create(FLAGS.columns,FLAGS.rows,1,.8,aruco_dict)
    if '5x5' in FLAGS.aruco_dict:
        aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_5X5_1000)
        board = cv2.aruco.CharucoBoard_create(FLAGS.columns,aruco_dict)
    if '6x6' in FLAGS.aruco_dict:
        aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_6X6_1000)
        board = cv2.aruco.CharucoBoard_create(FLAGS.columns,aruco_dict)
    if '7x7' in FLAGS.aruco_dict:
        aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_7X7_1000)
        board = cv2.aruco.CharucoBoard_create(FLAGS.columns,aruco_dict)


    for im in tqdm(images,"PROCESSING IMAGE:",total=len(images)):
        # print("=> Processing image {0}".format(im))
        frame = cv2.imread(str(im))
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        corners,ids,rejectedImgPoints = cv2.aruco.detectMarkers(gray,aruco_dict)
        allCorners.append(corners)
        allIds.append(ids)
    
    return allCorners,allIds




def main(argv):
    if not Path('./pose_images').is_dir():
        print(f"NO DIR. NAMED \'pose_images\' EXISTS")
        sys.exit()

    images = list(Path('./pose_images').glob('*.jpg'))

    allCorners,allIds = read_chessboards(images)
    print(f"ALL-CORNERS list length : {allCorners.__len__()}")
    print(f"ALL-IDs list length : {allIds.__len__()}")
    # print(f"IMGs SHAPE : {imsize}")
    K = np.array([[2.9734482e+03,0.0000000e+00,2.0244724e+03],[0.0000000e+00,2.9734482e+03,1.1212526e+03],1.0000000e+00]],dtype=np.float32)

    rvecs,tvecs,_= cv2.aruco.estimatePoseSingleMarkers(np.array(allCorners).astype(np.float64),0.10,K,distCoeffs=None)
    print(35*'%')

    for corners,id,img_path in tqdm(zip(allCorners,allIds,images),desc='PROCESSING #',total=len(allIds)):
        img = cv2.imread(str(img_path))
        corners = list(corners[0].squeeze())  # [4,2] list
        # print(type(corners))
        # print(len(corners))
        for c in tqdm(corners,desc="CORNER #"):
            img = cv2.circle(img,tuple(c),20,(0,255),-1)
        cv2.imwrite(f"./pose_images/results/{os.path.basename(img_path).split('.')[0]}.png",img)


    print(f"RESULTS SAVED")


if __name__ == '__main__':
    app.run(main)

之后,它尝试使用 cv2.aruco.estimatePoseSingleMarkers() 函数估计姿势,但显示以下错误

cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\calib3d\src\solvepnp.cpp:65: error: (-215:Assertion Failed) ( (npoints >= 4) || (npoints == 3 && fla
gs == SOLVEPNP_IteraTIVE && useExtrinsicGuess) ) && npoints == std::max(ipoints.checkVector(2,CV_32F),ipoints.checkVector(2,CV_64F)) in function 'cv::solvePnP'

我尝试更改数据类型,但无济于事。你能找出错误在哪里吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。