先看效果:
背景:
origin的自动跟踪曲线要下载软件,我还打不开图像数字化工具箱,安装插件里面还有bug;
打pu桥程序员的HaoCurve还要收费;
我就简单提取曲线数据,搞得麻烦得很,我脾气就上来了,还能惯着他们
我自己就写了个代码能自动提取具有色彩的曲线
思路:
1、图片由像素点组成,每个像素点还有RGB色彩参数
2、利用坐标轴三个点的像素位置来定位坐标系位置
3、识别图片中曲线的RGB值,设定该RGB的邻域(冗余量)
4、提取该RGB范围的像素点
5、对像素点数据进行唯一值处理
6、根据曲线像素点位置和坐标系位置关系求像素点在坐标系的位置
7、输出结果
上代码:
clc;close all;clear
%% 设定初始参数
img = imread('G:\岚山港潮汛\11.png'); % 替换'your_image.jpg'为你的图片文件路径
[rows, cols, ~] = size(img);
% imshow(img);
% % 从图像上获取坐标轴位置
[x1, y1] = ginput(1) % 获取xy轴起始像素位置 (xy的0刻度在左上方)
[x2, y2] = ginput(1) % 获取x轴最大的像素位置
[x3, y3] = ginput(1) % 获取y轴最大的像素位置
[x4, y4] = ginput(1) % 获取目标曲线的rgb值
specifiedRGB = img(round(y1), round(x1), :) % 获取选中点的RGB值
y_xiangsu_min = rows - y1 %(xy的0刻度在左下方)
y_xiangsu_max = rows - y3
x_xiangsu_min = x1
x_xiangsu_max = x2
u = 15 % 色彩冗余量
chaoxichaoliu_max = [6 1 6 0.8 5 1 5 1 6 1]
jiange = [0: 1 :24*7] % 三次样条插值步长
pngFiles = dir(fullfile('G:\岚山港潮汛\新建文件夹\', '*.png')); % 获取文件夹中所有PNG文件的列表
numPngFiles = length(pngFiles); % 显示文件夹中PNG文件的数量
xy_zuobiao_new = []
for k = 1:numPngFiles
img = imread(fullfile('G:\小炎(博)\AAA胡老师\任务\岚山港潮汛\新建文件夹\', pngFiles(k).name)); % 替换'your_image.jpg'为你的图片文件路径
% imshow(img);
[rows, cols, ~] = size(img);
matchedPixels = [];
for col = 1:cols % 遍历图像中的每个像素
for row = 1:rows
% 获取当前像素的RGB值
currentPixel = squeeze(img(row, col, :))';
currentPixel = double(currentPixel);
% 比较当前像素的RGB值与指定的RGB值
if currentPixel(1)>=specifiedRGB(1)-u && currentPixel(1)<=specifiedRGB(1)+u && currentPixel(2)>=specifiedRGB(2)-u && currentPixel(2)<=specifiedRGB(2)+u && currentPixel(3)>=specifiedRGB(3)-u && currentPixel(3)<=specifiedRGB(3)+u
% 如果匹配,保存该像素的位置
matchedPixels = [matchedPixels; [col,rows-row ]];
end
end
end
% 显示匹配像素的位置
disp('匹配像素的位置 (行, 列):');
disp(matchedPixels);
num = find(matchedPixels(:,2)<rows-330 | matchedPixels(:,2)>rows-28)
matchedPixels(num,:)=[]
uni_data = unique(matchedPixels(:,1));
for i=1:numel(uni_data)
adf = find(matchedPixels(:,1) == uni_data(i))
dfdf = mean(matchedPixels(adf,2))
data_use(i,:) = [uni_data(i) dfdf]
end
xy_zuobiao = [(data_use(:,1)-x_xiangsu_min)./(x_xiangsu_max-x_xiangsu_min)*7*24 ...
(data_use(:,2)-y_xiangsu_min)./(y_xiangsu_max-y_xiangsu_min)*chaoxichaoliu_max(k) ]
yy = spline(xy_zuobiao(:,1),xy_zuobiao(:,2),jiange);
xy_zuobiao_new = [xy_zuobiao_new [jiange; yy]']
figure
plot(xy_zuobiao_new(:,2*k-1),xy_zuobiao_new(:,2*k))
axis([0 24*7 0 chaoxichaoliu_max(k)])
clear data_use
end
总结
如果本篇文章对您有用的话,欢迎点赞收藏噢,谢谢谢谢,哈哈哈哈哈哈!!
如需帮助,请加扣联系:1097522625
主页还有更加丰富的内容噢 O(∩_∩)O :
Matlab 地理(经纬度)坐标 转 笛卡尔(直角)坐标
Matlab 土法求航海DCPA和TCPA,并根据DCPA正负判断目标船过本船船首or船尾
基于Matlab雷达视窗的来船运动矢量绘制
Matlab 沿着曲线的动态图制作
Matlab改进埃尔米特(Hermite)分段三次插值——(可在pchip函数中自定义导数值)
Matlab 四元素船舶领域代码复现
Matlab图片曲线数据提取