Python进行QLR检验
时间: 2024-06-18 11:01:57 浏览: 11
在Python中,QLR(Quantile-Quantile,分位数-分位数)检验是一种非参数统计方法,用于检查两个分布是否相同,通常用于比较两个独立样本的分布特性。这种方法不依赖于数据的总体分布假设,而是直接比较两个样本的分位数。
使用Python进行QLR检验,你可以使用`scipy.stats.kstest()`函数,它是SciPy库的一部分。这个函数提供了Kolmogorov-Smirnov检验(一种常见的QLR检验),但同时也支持QLR检验。以下是一个简单的步骤指南:
1. 首先,确保已经安装了`scipy`库,如果没有,可以使用`pip install scipy`命令安装。
2. 导入必要的库:
```python
import numpy as np
from scipy import stats
```
3. 准备你要比较的两个样本数据,分别存储在`sample1`和`sample2`变量中。
4. 使用`kstest()`函数执行QLR检验,通常会返回一个p-value,如果p值小于显著性水平(如0.05),则拒绝原假设,认为两个样本的分布不同:
```python
# 计算QLR检验
statistic, p_value = stats.ks_2samp(sample1, sample2)
# 显示结果
print(f"Statistic: {statistic}")
print(f"P-value: {p_value}")
```
相关问题
双目相机标定python代码
以下是使用Python进行双目相机标定的示例代码:
```python
import numpy as np
import cv2
# 定义标定板的参数
board_size = (9, 6)
square_size = 0.025 # 棋盘格方块尺寸,单位为米
# 设置标定板上角点的坐标
objp = np.zeros((np.prod(board_size), 3), np.float32)
objp[:, :2] = np.indices(board_size).T.reshape(-1, 2)
objp *= square_size
# 存储棋盘格角点的世界坐标和图像坐标
objpoints = [] # 3D points in real world space
imgpoints_l = [] # 2D points in image plane.
imgpoints_r = []
# 加载左右两个相机的图像
img_l = cv2.imread('left.jpg')
img_r = cv2.imread('right.jpg')
# 转换为灰度图像
gray_l = cv2.cvtColor(img_l, cv2.COLOR_BGR2GRAY)
gray_r = cv2.cvtColor(img_r, cv2.COLOR_BGR2GRAY)
# 找到棋盘格角点
ret_l, corners_l = cv2.findChessboardCorners(gray_l, board_size, None)
ret_r, corners_r = cv2.findChessboardCorners(gray_r, board_size, None)
# 如果找到棋盘格角点则存储世界坐标和图像坐标
if ret_l and ret_r:
objpoints.append(objp)
imgpoints_l.append(corners_l)
imgpoints_r.append(corners_r)
# 标定相机
ret_l, mtx_l, dist_l, rvecs_l, tvecs_l = cv2.calibrateCamera(objpoints, imgpoints_l, gray_l.shape[::-1], None, None)
ret_r, mtx_r, dist_r, rvecs_r, tvecs_r = cv2.calibrateCamera(objpoints, imgpoints_r, gray_r.shape[::-1], None, None)
# 计算双目相机的基础矩阵和双目矫正映射
ret, mtx_l, dist_l, mtx_r, dist_r, R, T, E, F = cv2.stereoCalibrate(objpoints, imgpoints_l, imgpoints_r, mtx_l, dist_l, mtx_r, dist_r, gray_l.shape[::-1], flags=cv2.CALIB_FIX_INTRINSIC)
R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv2.stereoRectify(mtx_l, dist_l, mtx_r, dist_r, gray_l.shape[::-1], R, T, alpha=1)
# 生成双目矫正映射
map_l_x, map_l_y = cv2.initUndistortRectifyMap(mtx_l, dist_l, R1, P1, gray_l.shape[::-1], cv2.CV_32FC1)
map_r_x, map_r_y = cv2.initUndistortRectifyMap(mtx_r, dist_r, R2, P2, gray_r.shape[::-1], cv2.CV_32FC1)
# 对左右相机的图像进行矫正
img_l_rect = cv2.remap(img_l, map_l_x, map_l_y, cv2.INTER_LINEAR)
img_r_rect = cv2.remap(img_r, map_r_x, map_r_y, cv2.INTER_LINEAR)
```
以上代码演示了如何使用OpenCV进行双目相机标定,包括棋盘格角点检测、相机参数计算、双目矫正映射生成等步骤。注意,这里的代码仅为示例,实际使用时需要根据具体情况进行修改。
双目摄像机标定矫正的python代码
双目摄像机标定矫正是一个用于将两个摄像机的图像对准以进行三维重建的过程。Python中常用的双目摄像机标定矫正代码库是OpenCV,以下是一个简单的双目摄像机标定和矫正的Python代码示例:
```python
import cv2
# 设置左右摄像机ID
left_cam_id = 0
right_cam_id = 1
# 设置棋盘格尺寸
board_size = (9, 6)
# 设置棋盘格物理尺寸(单位:mm)
square_size = 30
# 设置图像路径
left_path = 'left.jpg'
right_path = 'right.jpg'
# 初始化左右摄像机
left_cam = cv2.VideoCapture(left_cam_id)
right_cam = cv2.VideoCapture(right_cam_id)
# 设置棋盘格角点的三维坐标
obj_points = []
for i in range(board_size * board_size):
obj_points.append((i // board_size * square_size, i % board_size * square_size, 0))
# 初始化图像点和对象点数组
obj_points_list = []
img_points_left_list = []
img_points_right_list = []
while True:
# 捕获图像
ret_l, img_left = left_cam.read()
ret_r, img_right = right_cam.read()
# 检测棋盘格角点
ret_l, corners_left = cv2.findChessboardCorners(img_left, board_size)
ret_r, corners_right = cv2.findChessboardCorners(img_right, board_size)
if ret_l and ret_r:
# 绘制角点
cv2.drawChessboardCorners(img_left, board_size, corners_left, ret_l)
cv2.drawChessboardCorners(img_right, board_size, corners_right, ret_r)
# 添加对象点和图像点到数组中
obj_points_list.append(obj_points)
img_points_left_list.append(corners_left)
img_points_right_list.append(corners_right)
# 显示图像
cv2.imshow('Left', img_left)
cv2.imshow('Right', img_right)
# 按下 ESC 键退出循环
if cv2.waitKey(1) == 27:
break
# 关闭摄像机
left_cam.release()
right_cam.release()
# 计算标定参数
ret_l, mtx_l, dist_l, rvecs_l, tvecs_l = cv2.calibrateCamera(obj_points_list, img_points_left_list, img_left.shape[::-1][1:], None, None)
ret_r, mtx_r, dist_r, rvecs_r, tvecs_r = cv2.calibrateCamera(obj_points_list, img_points_right_list, img_right.shape[::-1][1:], None, None)
ret_s, mtx_l, dist_l, mtx_r, dist_r, R, T, E, F = cv2.stereoCalibrate(obj_points_list, img_points_left_list, img_points_right_list,
mtx_l, dist_l, mtx_r, dist_r,
img_left.shape[::-1][1:], criteria=criteria)
# 计算矫正参数
R1, R2, P1, P2, Q, valid_roi_left, valid_roi_right = cv2.stereoRectify(mtx_l, dist_l, mtx_r, dist_r,
img_left.shape[::-1][1:], R, T,
alpha=0)
# 生成映射表
mapx_l, mapy_l = cv2.initUndistortRectifyMap(mtx_l, dist_l, R1, P1,
img_left.shape[::-1][1:], cv2.CV_32FC1)
mapx_r, mapy_r = cv2.initUndistortRectifyMap(mtx_r, dist_r, R2, P2,
img_right.shape[::-1][1:], cv2.CV_32FC1)
# 加载图像
img_left = cv2.imread(left_path)
img_right = cv2.imread(right_path)
# 根据映射表矫正图像
img_rectified_left = cv2.remap(img_left, mapx_l, mapy_l, cv2.INTER_LINEAR)
img_rectified_right = cv2.remap(img_right, mapx_r, mapy_r, cv2.INTER_LINEAR)
# 显示矫正后的图像
cv2.imshow('Left Rectified', img_rectified_left)
cv2.imshow('Right Rectified', img_rectified_right)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)