matlab图⽚RGB颜⾊提取
APP designer版本链接:
新版本链接:
写了⼀个程序能实现导⼊图⽚识别颜⾊
⽀持屏幕截图和从电脑倒⼊图⽚
⿏标移动到想要取⾊的地⽅点击可以获取颜⾊
可以将颜⾊暂时保存在右边
⽬前导出颜⾊数据是从命令⾏窗⼝输出
完整代码
function getcolor
clc;clear;%清空⼯作区数据
%设置全局变量============================================================== global selected;%是否选中颜⾊,值为1时选中颜⾊,⽅形颜⾊展⽰框内颜⾊不随⿏标移动⽽变化%值为0时选未中颜⾊,⽅形颜⾊展⽰框内颜⾊随⿏标移动⽽改变
global ima;%图⽚展⽰区要展⽰的图⽚
global GUI;%绘制GUI的类
global x_limit;%展⽰区图⽚x坐标范围
global y_limit;%展⽰区图⽚y坐标范围
global color;%当前⿏标选中的颜⾊
global IMA;%ima图像的绘制函数
global color_list;%存储的颜⾊(列表形式)
global color_number;%下⼀次存储颜⾊会存储到颜⾊列表的第⼏⾏
color_number=1;%第⼀次存储颜⾊会存储到颜⾊列表第⼀⾏
color_list=[];
%GUI绘制=================================================================== GUI.fig=figure('units','pixels',...    %绘制背景
'position',[350100800500],...
'Numbertitle','off',...
'menubar','none',...
'resize','off',...
'name','getcolor',...
'color',[0.950.950.95]);
%图像及颜⾊展⽰部分的⽂本和画框--------------------------------------------
GUI.axes=axes('Units','pixels',...%绘制图⽚展⽰框
'PlotBoxAspectRatio',[111],...
'Position',[5030400400],...
'Color',[0.980.980.98],...
'Box','on',...
'XLim',[0500],...
'YLim',[0500],...
'XColor','w','YColor','w',...
'YDir','reverse',...
'Tag','imageaxes',...
'xtick',[],'ytick',[]);
GUI.title=uicontrol('parent',GUI.fig,... %绘制⼩标题
'style','text',...
动漫少女图'string','⾊彩识别',...
'horizontalalign','center',...
'position',[5044040030],...
'backgroundcolor',[0.850.890.85],...
'foregroundcolor','k',...
'fontsize',15);
uicontrol('parent',GUI.fig,...
'style','text',...
'string','',...
切糕的做法
'horizontalalign','left',...
'position',[460330100100],...
'backgroundcolor',[111],...
'foregroundcolor','k',...
'fontsize',10);
uicontrol('parent',GUI.fig,...
华为和荣耀是一个品牌吗
uicontrol('parent',GUI.fig,...
'style','text',...
'string','',...
'horizontalalign','center',...
'position',[35044021030],...
'backgroundcolor',[111],...
'foregroundcolor','k',...
'fontsize',10);
%功能性按钮⽣成------------------------------------------------------------
%该部分每个按钮的基础设置⼏乎相同,最⼤的区别就是不同的按钮有不同的回调函数GUI.savecolorbutton=...%颜⾊储存按钮
uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','储存颜⾊',...
'position',[46029010030],...
'backgroundcolor',[0.850.890.85],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@save_color);
GUI.deletedatabutton=...%清空数据按钮
uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','清空数据',...
'position',[46023010030],...
'backgroundcolor',[0.80.90.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@clear_data);
GUI.deletepicbutton=...%删除图⽚按钮
uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','删除图⽚',...
'position',[46018010030],...
'backgroundcolor',[0.80.90.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@delete_pic);
uicontrol('parent',GUI.fig,...
'style','pushbutton',...
芦苇坡'string','屏幕截图',...
'position',[46013010030],...
'backgroundcolor',[0.80.90.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@get_capture);
uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','读取图⽚',...
'position',[4608010030],...
'backgroundcolor',[0.80.90.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@get_image);
uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','获取颜⾊',...%该按钮的⽂本会在'获取颜⾊'和'继续取⾊'之间切换...%主要取决于selected的数值
'position',[4603010030],...
'backgroundcolor',[0.80.90.9],...
'backgroundcolor',[0.80.90.9],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@get_color);
GUI.inputbutton=uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','清除最后⼀个颜⾊',...
'position',[6008019030],...
'backgroundcolor',[0.850.890.85],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@delete_last);感冒可以喂奶吗
GUI.inputbutton=uicontrol('parent',GUI.fig,...
'style','pushbutton',...
'string','导出数据',...
'position',[6003019030],...
'backgroundcolor',[0.850.890.85],...
'foregroundcolor','k',...
'fontsize',15,...
'callback',@output_data);
%颜⾊储存区⽣成------------------------------------------------------------
for i=1:9
uicontrol('parent',GUI.fig,...
'style','text',...
'string','',...
'horizontalalign','left',...
'position',[600440-40*(i-1)3030],...
电脑壁纸图片'backgroundcolor',[111],...
'foregroundcolor','k',...
'fontsize',10);
end
for i=1:9%颜⾊储存区存储数据的九个长⽅形框
uicontrol('parent',GUI.fig,...
'style','text',...
'string','',...
'horizontalalign','center',...
'position',[640440-40*(i-1)15030],...
'backgroundcolor',[111],...
'foregroundcolor','k',...
'fontsize',8);
end
%回调函数部分============================================================== function get_image(~,~)%图像选择函数
warning off;%当你未选择图⽚直接关闭图库时,命令窗⼝会有报错,
%但是并不会影响程序运⾏,不妨使⽤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(GUI.axes,...%调整图⽚展⽰框横纵轴范围
'XLim',[0 leng],...
'YLim',[0 leng]);
delete(IMA);
IMA=imshow(ima);%展⽰图⽚
catch
end
end