我們先導(dǎo)入圖像,進行簡單的裁剪。為什么要裁剪呢?其實不裁剪也沒關(guān)系,因為我后面是基于像素點的顏色來提取的曲線。如果你想提取的曲線不能通過顏色區(qū)分,那么,最好通過裁剪,把你不想要的部分盡可能地剪掉。
觀察圖像發(fā)現(xiàn),我們要提取的曲線是藍色的,所以我希望通過顏色把它區(qū)分出來。那么我們就要知道這個曲線的 RGB 值。我希望通過鼠標點選的方式獲取到顏色值。
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','設(shè)置');
uimenu(uh1,'label','RGB類型設(shè)置','callback',@RGBset)
function RGBset(~,~)
GUI.rgbfig=figure('units','pixels',...
'position',[360 370 180 200],...
'Numbertitle','off',...
'menubar','none',...
'name','RGBset',...
'resize','off');
GUI.axes=axes('Units','pixels',...
'parent',GUI.rgbfig,...
'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',GUI.rgbfig,...
'style','checkbox',...
'string','范圍:0-1',...
'position',[45 150 400 30],...
'fontsize',10,...
'value',rgb_type,...
'callback',@ifon1);
GUI.checkbox255=uicontrol('parent',GUI.rgbfig,...
'style','checkbox',...
'string','范圍:0-255',...
'position',[45 120 400 30],...
'fontsize',10,...
'value',~rgb_type,...
'callback',@ifon255);
GUI.makesurebutton=uicontrol('parent',GUI.rgbfig,...
'style','pushbutton',...
'string','確定設(shè)置',...
'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)
set(GUI.text2,'string',['[',num2str((color/255).*rgb_type+color.*(~rgb_type)),']']);
end
show_color(page);
close(GUI.rgbfig)
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','導(dǎo)入');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
GUI.text=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);
GUI.text1=uicontrol('parent',GUI.fig,...
'style','text',...
'string','',...
'horizontalalign','center',...
'position',[460 330 100 100],...
'backgroundcolor',[1 1 1],...
'foregroundcolor','k',...
'fontsize',10);
GUI.text2=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','清空數(shù)據(jù)',...
'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);
GUI.getcapbutton=uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','屏幕截圖',...
'position',[460 130 100 30],...
'backgroundcolor',[0.8 0.9 0.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@get_capture);
GUI.getpicbutton=uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','讀取圖片',...
'position',[460 80 100 30],...
'backgroundcolor',[0.8 0.9 0.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@getImage);
GUI.getcolorbutton=uicontrol('parent',GUI.fig,...
'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
GUI.text=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
GUI.text=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','導(dǎo)出數(shù)據(jù)',...
%'position',[600 30 190 30],...
%'backgroundcolor',[0.85 0.89 0.85],...
%'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(page1)=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(get(GUI.text2,'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;
set(GUI.text1,'backgroundcolor',[1 1 1]);
set(GUI.text2,'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
set(GUI.text1,'backgroundcolor',[1 1 1]);
set(GUI.text2,'string','');
end
control=1;
set(gcf,'WindowButtonMotionFcn',@whilemovefcn)
set(gcf,'WindowButtonDownFcn',@whileclickfcn)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function whilemovefcn(~,~)
xy=get(gca,'CurrentPoint');
x=xy(1,2);y=xy(1,1);
if x=x_limity=y_limitx>=0y>=0
x(x>x_limit)=x_limit;
y(y>y_limit)=y_limit;
x(x1)=1;
y(y1)=1;
x=round(x);
y=round(y);
if control==1
color=double([ima(x,y,1),ima(x,y,2),ima(x,y,3)]);
set(GUI.text1,'backgroundcolor',color/255);
set(GUI.text2,'string',['[',num2str((color/255).*rgb_type+color.*(~rgb_type)),']']);
end
else
if control==1
set(GUI.text1,'backgroundcolor',[1 1 1]);
set(GUI.text2,'string','');
end
end
end
function whileclickfcn(~,~)
xy=get(gca,'CurrentPoint');
x=xy(1,2);y=xy(1,1);
if x=x_limity=y_limitx>=0y>=0
control=0;
set(GUI.text1,'backgroundcolor',color/255);
set(GUI.text2,'string',['[',num2str((color/255).*rgb_type+color.*(~rgb_type)),']']);
set(findobj('tag','recc'),'string','繼續(xù)取色');
%disp(color/255)
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function get_capture(~,~)
screensize=get(0,'screensize');
screensize=1.5*screensize;
robot=java.awt.Robot();
rectangle=java.awt.Rectangle();
rectangle.x=0;
rectangle.y=0;
rectangle.width=screensize(3);
rectangle.height=screensize(4);
image=robot.createScreenCapture(rectangle);
data=image.getData();
temp=zeros(screensize(3)*screensize(4)*3,1);
temp=data.getPixels(0,0,screensize(3),screensize(4),temp);
temp=uint8(temp);
R=temp(1:3:end);
G=temp(2:3:end);
B=temp(3:3:end);
R=reshape(R,[screensize(3),screensize(4)]);
G=reshape(G,[screensize(3),screensize(4)]);
B=reshape(B,[screensize(3),screensize(4)]);
R=R';
G=G';
B=B';
x_limit=screensize(4);
y_limit=screensize(3);
leng=max([x_limit,y_limit]);
set(findobj('Tag','picbagaxes'),...
'XLim',[0 leng],...
'YLim',[0 leng]);
ima=cat(3,R,G,B);
delete(a);
a=imshow(ima);
end
function getImage(~,~)
warning off;
try
[filename, pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' });
ima = imread([ pathname,filename]);
[x,y,~]=size(ima);
x_limit=x;y_limit=y;
leng=max([x_limit,y_limit]);
set(findobj('Tag','picbagaxes'),...
'XLim',[0 leng],...
'YLim',[0 leng]);
delete(a);
a=imshow(ima);
catch
end
end
end
用 RGB 比較顏色之間的相似度時,存在很大的問題,不建議直接使用,因為往往一個通道的一點改變,會導(dǎo)致最后融合在一起的顏色發(fā)生巨大變化,而如果三個通道的同時改變,卻只會使最后的明暗發(fā)生變化,色調(diào)并不會產(chǎn)生巨大變化。而這也是H系列色彩空間普遍存在的問題。
肉眼可見,這幾條曲線是分隔開的。我們?nèi)绾伟堰@些數(shù)據(jù)按曲線分開且其上的點按順序排好呢?我的做法是,使用鼠標選中你在意的曲線的一端,利用距離延拓法,還原整條曲線。
我不喜歡做 GUI,因為亂七八糟的界面用起來確實令人心煩。另外一方面,不管是 GUI 或者說進一步的 exe 桌面程序,都依賴于 MATLAB 的環(huán)境。也就是說,你要運行代碼,就必須先裝 MATLAB 的環(huán)境。試想,一個安裝過和簡單用過 MATLAB 的人,都是有能力可以通過代碼修改一些參數(shù)的,那么你做 GUI 不是畫蛇添足么。
到此這篇關(guān)于利用Matlab提取圖片曲線的文章就介紹到這了,更多相關(guān)Matlab提取圖片曲線內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!