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; %获取图像中心点的像素坐标 x_res=1920; y_res=1080; v_fov=46; f=50; x_c = x_res / 2; y_c = y_res / 2; %将图像中心点的像素坐标转换为相对坐标 x_c_rel = - (x_c - 1); y_c_rel = y_c - 1; %将相对坐标转换为实际坐标 x = (x_c_rel / x_res) * 2 * tan(10 / 2) * f; y = (y_c_rel / y_res) * 2 * tan(9 / 2) * f; end代码修改显示质心坐标
时间: 2023-08-31 16:06:23 浏览: 92
matlab-camer.zip_You Can!
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(6*sigma);
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(m*n/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;
% 获取图像中心点的像素坐标
x_res = 1920;
y_res = 1080;
v_fov = 46;
f = 50;
x_c = x_res / 2;
y_c = y_res / 2;
% 将图像中心点的像素坐标转换为相对坐标
x_c_rel = - (x_c - 1);
y_c_rel = y_c - 1;
% 将相对坐标转换为实际坐标并显示
x = (x_c_rel / x_res) * 2 * tan(10 / 2) * f;
y = (y_c_rel / y_res) * 2 * tan(9 / 2) * f;
fprintf('当前帧的质心坐标为:(%f, %f)\n', centroid(1), centroid(2));
fprintf('当前帧的实际坐标为:(%f, %f)\n', x, y);
end
阅读全文