Python 3.x 如何使用摄影机矩阵查找图像中点的位置(以毫米为单位)?

Python 3.x 如何使用摄影机矩阵查找图像中点的位置(以毫米为单位)?,python-3.x,opencv,camera-calibration,camera-matrix,Python 3.x,Opencv,Camera Calibration,Camera Matrix,我使用的是标准的640x480网络摄像头。我已经在Python 3的OpenCV中完成了相机校准。这就是我正在使用的代码。代码正在运行,并成功地为我提供了摄像机矩阵和失真系数。 现在,我如何在我的场景图像中找到640像素中有多少毫米。我将摄像头水平地安装在桌子上方,并在桌子上放置了一个机械臂。使用相机,我可以找到物体的质心。使用摄影机矩阵我的目标是将该对象的位置(例如300x200像素)转换为毫米单位,以便我可以将毫米数提供给机械臂以拾取该对象。 我已经搜索过了,但没有找到任何相关信息。 请告诉

我使用的是标准的640x480网络摄像头。我已经在Python 3的OpenCV中完成了相机校准。这就是我正在使用的代码。代码正在运行,并成功地为我提供了摄像机矩阵失真系数。 现在,我如何在我的场景图像中找到640像素中有多少毫米。我将摄像头水平地安装在桌子上方,并在桌子上放置了一个机械臂。使用相机,我可以找到物体的质心。使用摄影机矩阵我的目标是将该对象的位置(例如300x200像素)转换为毫米单位,以便我可以将毫米数提供给机械臂以拾取该对象。 我已经搜索过了,但没有找到任何相关信息。 请告诉我,这有什么方程式或方法。非常感谢

import numpy as np
import cv2
import yaml
import os

# Parameters
#TODO : Read from file
n_row=4  #Checkerboard Rows
n_col=6  #Checkerboard Columns
n_min_img = 10 # number of images needed for calibration
square_size = 40  # size of each individual box on Checkerboard in mm  
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) # termination criteria
corner_accuracy = (11,11)
result_file = "./calibration.yaml"  # Output file having camera matrix

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(n_row-1,n_col-1,0)
objp = np.zeros((n_row*n_col,3), np.float32)
objp[:,:2] = np.mgrid[0:n_row,0:n_col].T.reshape(-1,2) * square_size

# Intialize camera and window
camera = cv2.VideoCapture(0) #Supposed to be the only camera
if not camera.isOpened():
    print("Camera not found!")
    quit()
width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))  
height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
cv2.namedWindow("Calibration")


# Usage
def usage():
    print("Press on displayed window : \n")
    print("[space]     : take picture")
    print("[c]         : compute calibration")
    print("[r]         : reset program")
    print("[ESC]    : quit")

usage()
Initialization = True

while True:    
    if Initialization:
        print("Initialize data structures ..")
        objpoints = [] # 3d point in real world space
        imgpoints = [] # 2d points in image plane.
        n_img = 0
        Initialization = False
        tot_error=0
    
    # Read from camera and display on windows
    ret, img = camera.read()
    cv2.imshow("Calibration", img)
    if not ret:
        print("Cannot read camera frame, exit from program!")
        camera.release()        
        cv2.destroyAllWindows()
        break
    
    # Wait for instruction 
    k = cv2.waitKey(50) 
   
    # SPACE pressed to take picture
    if k%256 == 32:   
        print("Adding image for calibration...")
        imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

        # Find the chess board corners
        ret, corners = cv2.findChessboardCorners(imgGray, (n_row,n_col),None)

        # If found, add object points, image points (after refining them)
        if not ret:
            print("Cannot found Chessboard corners!")
            
        else:
            print("Chessboard corners successfully found.")
            objpoints.append(objp)
            n_img +=1
            corners2 = cv2.cornerSubPix(imgGray,corners,corner_accuracy,(-1,-1),criteria)
            imgpoints.append(corners2)

            # Draw and display the corners
            imgAugmnt = cv2.drawChessboardCorners(img, (n_row,n_col), corners2,ret)
            cv2.imshow('Calibration',imgAugmnt) 
            cv2.waitKey(500)        
                
    # "c" pressed to compute calibration        
    elif k%256 == 99:        
        if n_img <= n_min_img:
            print("Only ", n_img , " captured, ",  " at least ", n_min_img , " images are needed")
        
        else:
            print("Computing calibration ...")
            ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, (width,height),None,None)
            
            if not ret:
                print("Cannot compute calibration!")
            
            else:
                print("Camera calibration successfully computed")
                # Compute reprojection errors
                for i in range(len(objpoints)):
                   imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
                   error = cv2.norm(imgpoints[i],imgpoints2, cv2.NORM_L2)/len(imgpoints2)
                   tot_error += error
                print("Camera matrix: ", mtx)
                print("Distortion coeffs: ", dist)
                print("Total error: ", tot_error)
                print("Mean error: ", np.mean(error))
                
                # Saving calibration matrix
                try:
                    os.remove(result_file)  #Delete old file first
                except Exception as e:
                    #print(e)
                    pass
                print("Saving camera matrix .. in ",result_file)
                data={"camera_matrix": mtx.tolist(), "dist_coeff": dist.tolist()}
                with open(result_file, "w") as f:
                    yaml.dump(data, f, default_flow_style=False)
                
    # ESC pressed to quit
    elif k%256 == 27:
            print("Escape hit, closing...")
            camera.release()        
            cv2.destroyAllWindows()
            break
    # "r" pressed to reset
    elif k%256 ==114: 
         print("Reset program...")
         Initialization = True
失真系数:

818.6   0     324.4
0      819.1  237.9
0       0      1
0.34  -5.7  0  0  33.45
再见

我实际上在想,你应该能够用一种简单的方式解决你的问题:

mm_per_pixel = real_mm_width : 640px
假设相机最初与要拾取的对象平行移动[即,固定距离],
real\u mm\u width
可以通过测量与图片中那些
640
像素相对应的物理距离找到。举个例子,假设你发现
real\u mm\u width=32cm=320mm
,那么你得到
mm\u每像素=0.5mm/px
。在固定距离下,该比率不会改变

这似乎也是来自以下方面的建议:

这种考虑有助于我们只找到X,Y值。现在是X,Y 值,我们可以简单地将点传递为(0,0),(1,0),(2,0)。。。 表示点的位置。在这种情况下,我们得到的结果 将在棋盘广场的规模。但是如果我们知道 平方大小(比如30mm),我们可以将值传递为(0,0),(30,0), (60,0), ... . 因此,我们得到了mm的结果

然后,您只需使用以下方法将以像素为单位的质心坐标[例如,
(pixel_x_质心,pixel_y_质心)=(300px,200px)
]转换为mm:

mm_x_centroid = pixel_x_centroid * mm_per_pixel
mm_y_centroid = pixel_y_centroid * mm_per_pixel
这会给你最后的答案:

(mm_x_centroid, mm_y_centroid) = (150mm, 100mm)
另一种看待同一事物的方式是这个比例,其中第一个成员是可测量/已知的比例:

real_mm_width : 640px = mm_x_centroid : pixel_x_centroid = mm_y_centroid = pixel_y_centroid
祝你今天愉快,

安东尼诺

你需要知道相机空间中物体的尺寸,比如说尺子或其他东西是的,目前我正在用尺子或卷尺测量图像中从一侧到另一侧的毫米数。然后找到每像素毫米数。但这不是一个精确的方法。我想用数学的方法来做,没有错误。谢谢你明确的答案,我会试试的。我在这里找到了另一个解决办法:你认为链接中的方法好吗?@Tehseen别担心!即使您链接的解决方案实际上也无法帮助您在不进行物理测量的情况下检索mm值。那里所有的计算都是基于在它的理论中精辟地揭示出来的理论。在我的回答中,我假设质心坐标
(300px,200px)
表示
(u,v)
系统中图像平面上的值[即图像左上角的原点]。这是真的还是您的质心坐标是
(uc,vc)
=
(cx,cy)
[投影中心]系统?我正在测量图像左上角的坐标。我知道,仅使用内部参数无法从图像坐标中找到世界坐标,除非我有部门信息。但是如果我有内在的和外在的参数,那么我就有了一切,我可以执行从图像坐标到世界坐标的重投影,而不需要外部深度测量的帮助。这是真的吗?如果是这样的话,我该怎么做?谢谢参考链接: