close all; clear all; clc; load ('6mm_matlab.mat') % 相机标定基本参数 M = cameraParams.IntrinsicMatrix'; R = cameraParams.RotationMatrices(:,:,1); T = cameraParams.TranslationVectors(1,:)'; UV = cameraParams.ReprojectedPoints(:,:,1); % 将标定板角点像素坐标转换成齐次 for i = 1:size(UV,1) UV_H(i,:) = [UV(i,:),1]; end % 将像素坐标系转换到像素坐标系:参考https://blog.csdn.net/qq_43222384/article/details/101516807 leftMatrix = inv(R)*inv(M)*UV_H'; rightMatrix = inv(R)*T; rightMatrix_H = repmat(rightMatrix(3),[1,size(UV,1)]); temp_s = rightMatrix_H./leftMatrix(3,:);%leftMatrix和rightMatrix_H表示相机内参矩阵和外参矩阵, %UV是像素坐标系下的二维点,CB_World是相机坐标系下的三维点。 N_rightMatrix = repmat(rightMatrix,1,size(UV,1)); CB_World = temp_s.*leftMatrix - N_rightMatrix; WorldPoint = cameraParams.WorldPoints';
时间: 2023-12-06 14:40:05 浏览: 46
这段代码是用于相机标定的,包含了相机的内参矩阵M、旋转矩阵R、平移向量T和重投影点UV。代码的主要功能是将标定板角点的像素坐标转换为相机坐标系下的三维点坐标CB_World,并将其保存在变量WorldPoint中。具体实现方式是通过将像素坐标系下的二维点转换为齐次坐标形式,然后通过相机的内参矩阵和外参矩阵计算出相机坐标系下的三维点坐标。
相关问题
close all; clear all; clc; load ('6mm_matlab.mat') % 相机标定基本参数 M = cameraParams.IntrinsicMatrix'; R = cameraParams.RotationMatrices(:,:,1); T = cameraParams.TranslationVectors(1,:)'; UV = cameraParams.ReprojectedPoints(:,:,1); % 将标定板角点像素坐标转换成齐次 for i = 1:size(UV,1) UV_H(i,:) = [UV(i,:),1]; end % 将像素坐标系转换到像素坐标系:参考https://blog.csdn.net/qq_43222384/article/details/101516807 leftMatrix = inv(R)*inv(M)*UV_H'; rightMatrix = inv(R)*T; rightMatrix_H = repmat(rightMatrix(3),[1,size(UV,1)]); temp_s = rightMatrix_H./leftMatrix(3,:); N_rightMatrix = repmat(rightMatrix,1,size(UV,1)); CB_World = temp_s.*leftMatrix - N_rightMatrix; WorldPoint = cameraParams.WorldPoints';
这段代码是用于相机标定后计算标定板上角点的世界坐标的。具体来说,它假设已经知道了相机的内参矩阵、旋转矩阵和平移向量,以及标定板上角点在图像中的像素坐标,然后通过一系列的矩阵运算,计算出标定板上角点的世界坐标。
具体来说,代码中首先将相机内参矩阵和旋转矩阵求逆,并将像素坐标系下的角点像素坐标转换成齐次坐标。然后通过矩阵运算,将像素坐标系下的角点转换到相机坐标系下的坐标,并进一步转换到世界坐标系下的坐标。最后,将计算出的世界坐标保存在变量"WorldPoint"中。
close all; clear all; clc;load ('6mm_matlab.mat') % 相机标定基本参数 M = cameraParams.IntrinsicMatrix'; R = cameraParams.RotationMatrices(:,:,1); T = cameraParams.TranslationVectors(1,:)'; UV = cameraParams.ReprojectedPoints(:,:,1); v = VideoReader('shoujilux7.mp4'); while hasFrame(v) frame = readFrame(v); gray_frame = rgb2gray(frame); % gamma校正 gamma = 1.5; gamma_corrected = imadjust(gray_frame,[],[],gamma); % 高斯滤波 sigma = 1; hsize = ceil(6sigma); h = fspecial('gaussian', hsize, sigma); filtered_frame = imfilter(gamma_corrected, h); % Otsu阈值分割 T = graythresh(filtered_frame); [m, n] = size(filtered_frame); E = bwareaopen(im2bw(filtered_frame, T), round(mn/1000), 8); % Canny边缘检测 canny_edge = edge(E, 'canny'); % 形态学膨胀 se = strel('disk', 2); dilated_edge = imdilate(canny_edge, se); % 连通域分析 stats = regionprops('table', dilated_edge, 'Area', 'Centroid'); % 筛选面积最大的连通区域 [~, idx] = max(stats.Area); centroid = stats.Centroid(idx, :); % 显示帧和质心 imshow(dilated_edge); hold on; plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 10); hold off; 像素坐标转换为实际坐标显示
假设相机的焦距为f,像素坐标为(u,v),实际坐标为(x,y,z),则有以下公式:
x = (u - cx) * z / f
y = (v - cy) * z / f
其中cx和cy是图像中心点的像素坐标,z是实际坐标的深度。
在代码中,可以根据质心的像素坐标和相机内参计算出质心的实际坐标。代码如下:
% 计算质心的实际坐标
z = depth(centroid(2), centroid(1)); % 从深度图中获取深度值
x = (centroid(1) - M(1,3)) * z / M(1,1);
y = (centroid(2) - M(2,3)) * z / M(2,2);
% 显示帧和质心
imshow(frame);
hold on;
plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 10);
plot(x, y, 'bo', 'MarkerSize', 10);
hold off;
其中depth是深度图,可以通过调用Kinect或者其他深度相机的API获取。
阅读全文
相关推荐















