% 设置图片文件夹路径 image_folder = 'C:\Users\断x\Pictures\zd'; % 读取文件夹中的所有图片 file_names = dir(fullfile(image_folder, '*.jpg')); num_images = length(file_names); % 定义数组保存结果 XX = zeros(1, num_images); YY = zeros(1, num_images); % 循环遍历每张图片并处理 for i = 1:num_images % 读取图片 image_name = file_names(i).name; image_path = fullfile(image_folder, image_name); image = imread(image_path); I= image; % 处理图片 %I1 = rgb2gray(I); I2 = imcomplement(I); thresh = graythresh(I2); I3 = im2bw(I2, thresh); Ibw = imfill(I3, 'holes'); Ilabel = bwlabel(Ibw); Area_I = regionprops(Ilabel, 'centroid'); % 绘制形心并保存结果 figure; imshow(image); hold on; for x = 1:numel(Area_I) plot(Area_I(x).Centroid(1), Area_I(x).Centroid(2), 'b*'); XX(1,i) = Area_I(x).Centroid(1); YY(1,i)= Area_I(x).Centroid(2); end hold off end % 计算位移和时间 if i > 1 displacement(1,i) = sqrt((XX(1,i)-XX(1,i-1))^2 + (YY(1,i)-YY(1,i-1))^2); time(1,i) = i-1; end加上画出位移图和加速度图的代码
时间: 2024-01-26 11:03:54 浏览: 61
代码如下:
% 设置图片文件夹路径
image_folder = 'C:\Users\断x\Pictures\zd';
% 读取文件夹中的所有图片
file_names = dir(fullfile(image_folder, '*.jpg'));
num_images = length(file_names);
% 定义数组保存结果
XX = zeros(1, num_images);
YY = zeros(1, num_images);
displacement = zeros(1, num_images);
time = zeros(1, num_images);
% 循环遍历每张图片并处理
for i = 1:num_images
% 读取图片
image_name = file_names(i).name;
image_path = fullfile(image_folder, image_name);
image = imread(image_path);
I = image;
% 处理图片
I2 = imcomplement(I);
thresh = graythresh(I2);
I3 = im2bw(I2, thresh);
Ibw = imfill(I3, 'holes');
Ilabel = bwlabel(Ibw);
Area_I = regionprops(Ilabel, 'centroid');
% 绘制形心并保存结果
figure; imshow(image);
hold on;
for x = 1:numel(Area_I)
plot(Area_I(x).Centroid(1), Area_I(x).Centroid(2), 'b*');
XX(1,i) = Area_I(x).Centroid(1);
YY(1,i) = Area_I(x).Centroid(2);
end
hold off
% 计算位移和时间
if i > 1
displacement(1,i) = sqrt((XX(1,i)-XX(1,i-1))^2 + (YY(1,i)-YY(1,i-1))^2);
time(1,i) = i-1;
end
end
% 画出位移图
figure;
plot(time, displacement);
title('Displacement vs Time');
xlabel('Time');
ylabel('Displacement');
% 计算加速度并画出加速度图
velocity = displacement./time;
acceleration = (velocity(2:end)-velocity(1:end-1))./(time(2:end)-time(1:end-1));
figure;
plot(time(2:end), acceleration);
title('Acceleration vs Time');
xlabel('Time');
ylabel('Acceleration');
阅读全文