利⽤Matlab提取图⽚曲线
⽬录
利⽤ MATLAB 提取图⽚曲线
⾏⽂动机
图像的读⼊与裁剪
颜⾊拾取
颜⾊转换与⾊差计算
分离曲线
⼆值化,提取数据
数据点分类与排序
后话
利⽤ MATLAB 提取图⽚曲线
给你⼀张图⽚,如何提取⾥⾯曲线的数据,从⽽利⽤这些数据进⾏图像重绘、加⼯处理、测距、拟合得到函数表达式等操作呢?
⾏⽂动机
前段时间,有个朋友问了我⼀个问题,⼤概意思就是要给图像的流线测距离,在我的印象⾥⾯,MATLAB 是似乎没有这种直接的功能的。
那么换个⾓度来理解⼀下这个问题,如果给你⼀张图像,如何提取⾥⾯点的数据?其实,有了曲线的数据,后⾯想⼲嘛就⼲嘛了。
⼀直没空弄这个,今天偷闲,安排
图像的读⼊与裁剪
以下⾯的图像作为例⼦。
我们先导⼊图像,进⾏简单的裁剪。为什么要裁剪呢?其实不裁剪也没关系,因为我后⾯是基于像素点的颜⾊来提取的曲线。如果你想提取的曲线不能通过颜⾊区分,那么,最好通过裁剪,把你不想要的部分尽可能地剪掉。
%% 读⼊图⽚,展⽰,有必要的话可以适当做⼀些裁剪
A = imread('a.jpg');%读取到⼀张图⽚
imshow(A);
A = imcrop(A);%使⽤⿏标裁剪⼀波
imshow(A);
[low_num,col_num,~] = size(A);
颜⾊拾取
观察图像发现,我们要提取的曲线是蓝⾊的,所以我希望通过颜⾊把它区分出来。那么我们就要知道这个曲线的 RGB 值。我希望通过⿏标点选的⽅式获取到颜⾊值。
这⾥我偷个懒,直接采⽤了开源的颜⾊提取⼯具。这个模块不是我写的,特此声明,请尊重原创。
%% 颜⾊提取
getcolor();
color = color_list_temp(1,:);
function getcolor
global control;
global ima;
global GUI;
global x_limit;
global y_limit;
global color;
global a;
global color_number;
global color_list;
global page;
global total_page;
global color_list_temp;
color=[];
page=1;
total_page=2;
color_list=[0 0 0];
color_list(1,:)=[];
color_number=1;
rgb_type=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
GUI.fig=figure('units','pixels',...
'position',[350 100 800 500],...
'Numbertitle','off',...
'menubar','none',...
'resize','off',...
'name','getcolor',...
'color',[0.95 0.95 0.95]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
uh1=uimenu('label','设置');
uimenu(uh1,'label','RGB类型设置','callback',@RGBset)
function RGBset(~,~)
'position',[360 370 180 200],...
'Numbertitle','off',...
'menubar','none',...
'name','RGBset',...
'resize','off');
GUI.axes=axes('Units','pixels',...
'parent',bfig,...
'PlotBoxAspectRatio',[1 1 1],...
'Color',[0.95 0.95 0.95],...
'Box','on', ...
'XLim',[0 500],...
'YLim',[0 500], ...
'XColor',[0.95 0.95 0.95],...
'YColor',[0.95 0.95 0.95],...
'YDir','reverse', ...
'xtick',[],'ytick',[]);
GUI.checkbox1=uicontrol('parent',bfig,...
'style','checkbox',...
'string','范围:0-1',...
'position',[45 150 400 30],...
'fontsize',10,...
'value',rgb_type,...
'callback',@ifon1);
GUI.checkbox255=uicontrol('parent',bfig,...
'style','checkbox',...
'string','范围:0-255',...
'position',[45 120 400 30],...
'fontsize',10,...
'value',~rgb_type,...
'callback',@ifon255);
GUI.makesurebutton=uicontrol('parent',bfig,...
'style','pushbutton',...
'string','确定设置',...
'position',[45 70 100 25],...
'fontsize',10,...
'callback',@settype);
function ifon1(~,~)
if(get(GUI.checkbox1,'value')==1)
set(GUI.checkbox1,'value',1);
set(GUI.checkbox255,'value',0);
else
set(GUI.checkbox1,'value',1);
end
end
function ifon255(~,~)
if(get(GUI.checkbox255,'value')==1)
set(GUI.checkbox255,'value',1);
set(GUI.checkbox1,'value',0);
else
set(GUI.checkbox255,'value',1);
end
end
function settype(~,~)
rgb_type=get(GUI.checkbox1,'value');
if ~isempty(color)
2,'string',['[',num2str((color/255).*rgb_type+color.*(~rgb_type)),']']);
end
show_color(page);
bfig)
end
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% uh2=uimenu('label','保存');
uimenu(uh2,'label','储存为mat','callback',@saveas_mat)
uimenu(uh2,'label','储存为txt','callback',@saveas_txt)
uimenu(uh2,'label','储存为excel','callback',@saveas_exl)
uimenupic=uimenu(uh2,'label','储存为对照图');
uimenu(uimenupic,'label','储存全部页码','callback',@saveas_pic_all);
uimenu(uimenupic,'label','储存当前页码','callback',@saveas_pic_now);
function saveas_mat(~,~)
try
[filename, pathname] = uiputfile({'*.mat','mat'});
color_list_temp=(color_list/255).*rgb_type+color_list.*(~rgb_type);
save([pathname,filename],'color_list_temp');
catch
end
end
function saveas_txt(~,~)
try
[filename, pathname] = uiputfile({'*.txt','记事本'});
color_list_temp=(color_list/255).*rgb_type+color_list.*(~rgb_type);
[m,n]=size(color_list_temp);
fid=fopen([ pathname,filename],'w');
for ii=1:m
for jj=1:n
if jj==n
fprintf(fid,'%d\r\n',color_list_temp(ii,jj));
else
fprintf(fid,'%d\r\t',color_list_temp(ii,jj));
end
end
end
fclose(fid);
catch
end
end
function saveas_exl(~,~)
[filename, pathname] = uiputfile({'*.xlsx','记事本'});
color_list_temp=(color_list/255).*rgb_type+color_list.*(~rgb_type);
xlswrite([ pathname,filename],color_list_temp)
end
function saveas_pic_all(~,~)
page_with_color=total_page-1;
px=50;
gap_px=10;
pic=ones(9*px,page_with_color*px+(page_with_color-1)*gap_px,3);
for p=1:page_with_color
for ii=(p-1)*9+1:p*9
for kk=1:3
if ii<=length(color_list)
pic((ii-(p-1)*9-1)*px+1:(ii-(p-1)*9)*px,(p-1)*(px+gap_px)+1:(p-1)*(px+gap_px)+px,kk)=color_list(ii,kk)/255;
end
end
end
end
[filename, pathname] = uiputfile({'*.jpg;*.png','All Image Files';...
'*.jpg','JPG';'*.png','PNG' });
imwrite(pic,[pathname,filename]);
end
function saveas_pic_now(~,~)
try
[m,~]=size(color_list);
m=m-(page-1)*9;
m(m>9)=9;
px=50;
pic=ones(9*px,1*px,3);
if m>0
for ii=(page-1)*9+1:(page-1)*9+m
for kk=1:3
pic((ii-1)*px+1:ii*px,1:px,kk)=color_list(ii,kk)/255;
end
end
else
end
[filename, pathname] = uiputfile({'*.jpg;*.png','All Image Files';...
'*.jpg','JPG';'*.png','PNG' });
imwrite(pic,[pathname,filename]);
catch
end
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %uh3=uimenu('label','导⼊'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% =uicontrol('parent',GUI.fig,...
'style','text',...
'string','⾊彩识别',...
'horizontalalign','center',...
'position',[50 440 400 30],...
'backgroundcolor',[0.85 0.89 0.85],...
'foregroundcolor','k',...
'fontsize',15);
<1=uicontrol('parent',GUI.fig,...
'style','text',...
'string','',...
'horizontalalign','center',...
'position',[460 330 100 100],...
'backgroundcolor',[1 1 1],...
'foregroundcolor','k',...
'fontsize',10);
<2=uicontrol('parent',GUI.fig,...
'style','text',...
'string','',...
'horizontalalign','center',...
'position',[350 440 210 30],...
'backgroundcolor',[1 1 1],...
'foregroundcolor','k',...
'fontsize',10);
GUI.savecolorbutton=uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','储存颜⾊',...
'position',[460 290 100 30],...
'backgroundcolor',[0.85 0.89 0.85],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@save_color);
GUI.deletedatabutton=uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','清空数据',...
'position',[460 230 100 30],...
'backgroundcolor',[0.8 0.9 0.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@clear_data);
GUI.deletepicbutton=uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','删除图⽚',...
'position',[460 180 100 30],...
'backgroundcolor',[0.8 0.9 0.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@delete_pic);
'style','pushbutton',...
'string','屏幕截图',...
'position',[460 130 100 30],...
'backgroundcolor',[0.8 0.9 0.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@get_capture);
'style','pushbutton',...
'string','读取图⽚',...
'position',[460 80 100 30],...
'backgroundcolor',[0.8 0.9 0.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@getImage);
'style','pushbutton',...
'tag','recc',...
'string','获取颜⾊',...
'position',[460 30 100 30],...
'backgroundcolor',[0.8 0.9 0.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@get_color); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for i=1:9
<=uicontrol('parent',GUI.fig,...
'tag',num2str(i),...
'style','text',...
'string','',...
'horizontalalign','left',...
'position',[600 440-40*(i-1) 30 30],...
'backgroundcolor',[1 1 1],...
'foregroundcolor','k',...
'fontsize',10);
end
for i=1:9
<=uicontrol('parent',GUI.fig,...
'tag',[num2str(i),'t'],...
'style','text',...
'string','',...
'horizontalalign','center',...
'position',[640 440-40*(i-1) 150 30],...
'backgroundcolor',[1 1 1],...
'foregroundcolor','k',...
'fontsize',8);
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% GUI.inputbutton=uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','清除最后⼀个颜⾊',...
'position',[600 80 190 30],...
'backgroundcolor',[0.85 0.89 0.85],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@delete_last);
%GUI.inputbutton=uicontrol('parent',GUI.fig,...
%'style','pushbutton',...
%'string','导出数据',...
%'position',[600 30 190 30],...
%'backgroundcolor',[0.85 0.89 0.85],...
getsavefilename
%'foregroundcolor','k',...
%'fontsize',15,...
%'callback',@output_data); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% GUI.lpbutton=uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','<;上⼀页',...
'position',[600 30 70 30],...
'backgroundcolor',[0.85 0.85 0.85],...
'foregroundcolor','k',...
'fontsize',12,...
'callback',@lastpage);
GUI.npbutton=uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','下⼀页>',...
'position',[720 30 70 30],...
'backgroundcolor',[0.85 0.85 0.85],...
'foregroundcolor','k',...
'fontsize',12,...
'callback',@nextpage);
GUI.page=uicontrol('parent',GUI.fig,...
'style','text',...
'string',[num2str(page),'/',num2str(total_page)],...
'horizontalalign','center',...
'position',[670 30 50 27],...
'backgroundcolor',[0.95 0.95 0.95],...
'foregroundcolor','k',...
'fontsize',12);
function lastpage(~,~)
page=page-1;
page(page<1)=1;
set(GUI.page,'string',[num2str(page),'/',num2str(total_page)]);
show_color(page);
end
function nextpage(~,~)
page=page+1;
page(page>total_page)=total_page;
set(GUI.page,'string',[num2str(page),'/',num2str(total_page)]);
show_color(page);
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% GUI.axes=axes('Units','pixels',...
'PlotBoxAspectRatio',[1 1 1],...
'Position',[50 30 400 400],...
'Color',[0.98 0.98 0.98],...
'Box','on', ...
'XLim',[0 500],...
'YLim',[0 500], ...
'XColor','w','YColor','w',...
'YDir','reverse', ...
'Tag','picbagaxes',...
'xtick',[],'ytick',[]);
hold on; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %function output_data(~,~)
%if color_number>1
%disp(color_list)
%end
%end
function show_color(cur_page)
len_list=size(color_list,1);
for ii=(cur_page-1)*9+1:(cur_page-1)*9+9
if(ii<=len_list)
set(findobj('tag',num2str(ii-(cur_page-1)*9)),'backgroundcolor',color_list(ii,:)/255)
set(findobj('tag',[num2str(ii-(cur_page-1)*9),'t']),'string',['[',num2str((color_list(ii,:)/255).*rgb_type+color_list(ii,:).*(~rgb_type)),']'])
else
set(findobj('tag',num2str(ii-(cur_page-1)*9)),'backgroundcolor',[1 1 1])
set(findobj('tag',[num2str(ii-(cur_page-1)*9),'t']),'string','')
end
end
end
function save_color(~,~)
if ~isempty(2,'string'))
%set(findobj('tag',num2str(color_number)),'backgroundcolor',color/255)
%set(findobj('tag',[num2str(color_number),'t']),'string',['[',num2str((color/255).*rgb_type+color.*(~rgb_type)),']'])
color_list(color_number,:)=color;%(color/255).*rgb_type+color.*(~rgb_type);
color_number=color_number+1;
if color_number-1>(total_page-1)*9
page=ceil(color_number/9);
total_page=total_page+1;
set(GUI.page,'string',[num2str(page),'/',num2str(total_page)]);
end
show_color(page)
end
end
function delete_last(~,~)
if color_number>=1
%set(findobj('tag',num2str(color_number-1)),'backgroundcolor',[1 1 1])
%set(findobj('tag',[num2str(color_number-1),'t']),'string','')
color_list(end,:)=[];
color_number=color_number-1;
if color_number-2<=(total_page-2)*9
page=ceil((color_number-1)/9);
total_page=total_page-1;
set(GUI.page,'string',[num2str(page),'/',num2str(total_page)]);
end
show_color(page)
end
end
function delete_pic(~,~)
control=0;
set(findobj('Tag','picbagaxes'),...
'XLim',[0 500],...
'YLim',[0 500],...
'Position',[50 30 400 400],...
'Color',[0.98 0.98 0.98]);
delete(a);
end
function clear_data(~,~)
control=0;
1,'backgroundcolor',[1 1 1]);
2,'string','');
set(findobj('Tag','picbagaxes'),...
'XLim',[0 500],...
'YLim',[0 500],...
'Position',[50 30 400 400],...
'Color',[0.98 0.98 0.98]);
set(findobj('tag','recc'),'string','获取颜⾊');
delete(a);
end
function get_color(~,~)
if control==0
1,'backgroundcolor',[1 1 1]);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。