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(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, :); % 将质心坐标显示在GUI中 d = 100; % 假设质心到相机的距离为100mm pixel_coord = [centroid(1); centroid(2); 1]; world_coord = inv(M) * pixel_coord * d; disp(world_coord); % 显示质心的实际坐标 % 显示帧和质心 imshow(centroid); hold on; plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 10); hold off; drawnow; end设计gui显示坐标
时间: 2023-11-27 19:04:12 浏览: 37
你可以参考以下代码实现GUI界面,显示质心坐标:
```matlab
function varargout = centroid_gui(varargin)
% CENTROID_GUI MATLAB code for centroid_gui.fig
% CENTROID_GUI, by itself, creates a new CENTROID_GUI or raises the existing
% singleton*.
%
% H = CENTROID_GUI returns the handle to a new CENTROID_GUI or the handle to
% the existing singleton*.
%
% CENTROID_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CENTROID_GUI.M with the given input arguments.
%
% CENTROID_GUI('Property','Value',...) creates a new CENTROID_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before centroid_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to centroid_gui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help centroid_gui
% Last Modified by GUIDE v2.5 29-Jun-2021 11:59:01
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @centroid_gui_OpeningFcn, ...
'gui_OutputFcn', @centroid_gui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before centroid_gui is made visible.
function centroid_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to centroid_gui (see VARARGIN)
% Choose default command line output for centroid_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes centroid_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = centroid_gui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in start_btn.
function start_btn_Callback(hObject, eventdata, handles)
% hObject handle to start_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load ('6mm_matlab.mat'); % 相机标定基本参数
M = cameraParams.IntrinsicMatrix';
R = cameraParams.RotationMatrices(:,:,1);
T = cameraParams.TranslationVectors(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(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, :);
% 将质心坐标显示在GUI中
d = 100; % 假设质心到相机的距离为100mm
pixel_coord = [centroid(1); centroid(2); 1];
world_coord = inv(M) * pixel_coord * d;
set(handles.coord_text, 'String', ['x: ', num2str(world_coord(1)), ' y: ', num2str(world_coord(2)), ' z: ', num2str(world_coord(3))]);
% 显示帧和质心
axes(handles.video_axes);
imshow(frame);
hold on;
plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 10);
hold off;
drawnow;
end
% --- Executes on button press in stop_btn.
function stop_btn_Callback(hObject, eventdata, handles)
% hObject handle to stop_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Stop the video
set(handles.coord_text, 'String', '');
axes(handles.video_axes);
cla;
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Stop the video and delete the GUI
stop_btn_Callback(handles.stop_btn, eventdata, handles);
delete(hObject);
```
然后在GUIDE中创建一个新的GUI,将上面的代码复制到.m文件中,并在GUI中添加一个Axes(用于显示视频)和一个Static Text(用于显示坐标)。最后将按钮的回调函数链接到相应的.m文件中的函数即可。