1、基于 matlab 的 HSV 阴影消除代码,毕设所用,用完贡献先给代码 后有说明function varargout = untitled111111(varargin)% UNTITLED111111 M-file for untitled111111.fig% UNTITLED111111, by itself, creates a new UNTITLED111111 or raises the existing% singleton*.% H = UNTITLED111111 returns the handle to a new UNTITLED111111 or the hand
2、le to% the existing singleton*.% UNTITLED111111(CALLBACK,hObject,eventData,handles,.) calls the local% function named CALLBACK in UNTITLED111111.M with the given input arguments.% UNTITLED111111(Property,Value,.) creates a new UNTITLED111111 or raises the% existing singleton*. Starting from the left
3、, property value pairs are% applied to the GUI before untitled111111_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to untitled111111_OpeningFcn via varargin.% *See GUI Options on GUIDEs Tools menu. Choose “GUI allows o
4、nly one% instance to run (singleton)“.% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help untitled111111% Last Modified by GUIDE v2.5 31-Mar-2014 09:39:29% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct(gui_Name, mfilename, .gui_Sin
5、gleton, gui_Singleton, .gui_OpeningFcn, untitled111111_OpeningFcn, .gui_OutputFcn, untitled111111_OutputFcn, .gui_LayoutFcn, , .gui_Callback, );if nargin endif nargoutvarargout1:nargout = gui_mainfcn(gui_State, varargin:);elsegui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT ED
6、IT% - Executes just before untitled111111 is made visible.function untitled111111_OpeningFcn(hObject, , handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and
7、 user data (see GUIDATA)% varargin command line arguments to untitled111111 (see VARARGIN)% Choose default command line output for untitled111111handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes untitled111111 wait for user response (see UIRESUME)% uiwait(ha
8、ndles.figure1);% - Outputs from this function are returned to the command line.function varargout = untitled111111_OutputFcn(, , handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% han
9、dles structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout1 = handles.output;% - Executes on button press in pushbutton1.function pushbutton1_Callback(, , )handles=guihandles();global s;global e;global fn;global mov;for i=s:e mov_gray(i).
10、image=mov(i).cdata; % mov_gray(i).image 为第 i 帧图像endih,iw,id=size(mov_gray(1).image); % 取得视频帧的大小% 计算 1 到第 n 帧的各个像素的均值for i=s:eu=mov_gray(i).image;h=u(:); % 转置t(i,:)=h(:); % median(t)是对列进行取均值的end%close(figure(gcf)% %关闭初始化窗口% 试试不用转置直接用 u 来进行median(t ) ;tmp=median(t); % 对 t 数组各列取均值global mean;mean=resha
11、pe(tmp,ih,iw,id); % 将 n 帧均值图像恢复成 h*w*d 的图像矩阵,得到背景subplot(2,2,1),imshow(uint8(mean);title(背景图像); %第一幅 背景图%取第 fn 帧作为对象subplot(2,2,2),imshow(uint8(mov_gray(fn).image);title(目标图像 );%第二幅 完整图像global ok; ok=mean; %ok-背景%tmp(1:3)=0;di=15;th=ones(1,1,3)*di; % 预设阀值diff=double(ok)-double(mov_gray(fn).image); %
12、 计算第 fn 帧图像与背景的差值diff1=abs(diff);count2=0;for ai=1:ihfor bi=1:iwif abs(diff(ai,bi,1:3)=th(1,1,:)ok(ai,bi,1:3)=mov_gray(fn).image(ai,bi,1:3);count2=count2+1;elseok(ai,bi,1:3)=0;diff1(ai,bi,1:3)=0;endendend%subplot(2,2,3),imshow(uint8(ok);title(提取前景图像); % %第三幅 前景图count2=count2*1.25;e1=num2str(count2);
13、sc=共,e1;sc=sc,点;set(handles.text16,String,sc);g=rgb2hsv(mean); % 将背景图像由 RGB 格式转为 HSV 格式g1=rgb2hsv(ok); % 将前景图像由 RGB 格式转为 HSV 格式% 阀值分割global TH1;global TH2;global TS;global TV;count4=0;for ci=1:ihfor di=1:iwif (g1(ci,di,3)/g(ci,di,3)TS|abs(g1(ci,di,1)-g(ci,di,1)TH2% else% ok(ai,bi,1:3)=0;% diff1(ai,b
14、i,1:3)=0;% end% end%end%subplot(2,2,3),imshow(uint8(ok);title(提取前景图像); % %第三幅 前景图QHuidu=rgb2ycbcr(Qianjing); %RGB 颜色空间转换到 YUV 颜色空间BHuidu=rgb2ycbcr(Beijing);%subplot(2,2,4),imshow(BHuidu);title(提取前景图像); % %第三幅 前景图%figure,imshow(QHuidu);%figure,imshow(BHuidu);Yyu=double(QHuidu(:,:,1)-double(BHuidu(:,:
15、,1); %Y 分量的差分YErzhihua=Yyu;Q= abs(Yyu)10; %Y 分量差分图像的二值化YErzhihua(Q)=1;Q= abs(Yyu)10; %UV 分量的图像的合成 UVErzhihua(Q)=1;Q= abs(Uyu)+abs(Vyu)=10;UVErzhihua(Q)=0;%figure,imshow(UVErzhihua);YUVhecheng=Uyu;Q=(UVErzhihua=1) %Y 分量和 UV 分量的图像合成YUVhecheng(Q)=1;Q=(UVErzhihua=0)|(YErzhihua=0);YUVhecheng(Q)=0;YUV=YUV
16、hecheng;for ci=1:ihfor di=1:iwif YUV(ci,di)=1ok(ci,di,1:3)=mov_gray(fn).image(ci,di,1:3); elseok(ci,di,1:3)=mean(ci,di,1:3);endendendsubplot(2,2,4),imshow(ok);title(YUV 阴影消除);% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structu
17、re with handles and user data (see GUIDATA)% - Executes on button press in pushbutton3.function pushbutton3_Callback(, , )clc;close all;close(gcf);clear;% hObject handle to pushbutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and us
18、er data (see GUIDATA)ai% - Executes on button press in pushbutton4.function pushbutton4_Callback(, , )h1=figure(toolbar,none,name,Shadow Removal); % 读取受损图像FileName,PathName=uigetfile( *.avi,avi (*.avi);*.*,All Files (*.*),Open video sequence );if isequal(FileName,PathName,0,0)return;elsepic_path=ful
19、lfile(PathName,FileName);global mov;mov=aviread(pic_path);endn=size(mov,2); %计算视频的帧数(试试用这个代替下面的)%n=100; % 读取视频帧数global s;s=1; % 起始为 1global e;e=n; % 结束为 n%global fn;%fn=11; % hObject handle to pushbutton4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure
20、 with handles and user data (see GUIDATA)function edit1_Callback(, , )% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,String) returns contents of edit1 as text% str2
21、double(get(hObject,String) returns contents of edit1 as a double% - Executes during object creation, after setting all properties.function edit1_CreateFcn(hObject, , )% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not c
22、reated until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc endfunction edit2_Callback(hObject, , handles)% hObject handle to edit2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles struc
23、ture with handles and user data (see GUIDATA)% Hints: get(hObject,String) returns contents of edit2 as text% str2double(get(hObject,String) returns contents of edit2 as a doubleinput = str2num(get(hObject,String);if (isempty(input)set(hObject,String,11)endguidata(hObject, handles)% - Executes during
24、 object creation, after setting all properties.function edit2_CreateFcn(hObject, , )% hObject handle to edit2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white
25、background on Windows.% See ISPC and COMPUTER.if ispc end% - Executes on button press in pushbutton5.function pushbutton5_Callback(, , handles)% hObject handle to pushbutton5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (s
26、ee GUIDATA)global fn;global e;e=num2str(e);sc=共,e;sc=sc,帧;%a = get(handles.edit1,String);fn = str2double(get(handles.edit2,String);set(handles.text15,String,sc);e=str2double(e);function edit3_Callback(hObject, , handles)% hObject handle to edit3 (see GCBO)% eventdata reserved - to be defined in a fu
27、ture version of MATLAB% handles structure with handles and user data (see GUIDATA)input = str2num(get(hObject,String);if (isempty(input)set(hObject,String,1)endguidata(hObject, handles)% Hints: get(hObject,String) returns contents of edit3 as text% str2double(get(hObject,String) returns contents of
28、edit3 as a double% - Executes during object creation, after setting all properties.function edit3_CreateFcn(hObject, , )% hObject handle to edit3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint:
29、 edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc endfunction edit4_Callback(hObject, , handles)% hObject handle to edit4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)in
30、put = str2num(get(hObject,String);if (isempty(input)set(hObject,String,1)endguidata(hObject, handles)% Hints: get(hObject,String) returns contents of edit4 as text% str2double(get(hObject,String) returns contents of edit4 as a double% - Executes during object creation, after setting all properties.f
31、unction edit4_CreateFcn(hObject, , )% hObject handle to edit4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if
32、 ispc endfunction edit5_Callback(hObject, , handles)% hObject handle to edit5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)input = str2num(get(hObject,String);if (isempty(input)set(hObject,String,2)endguidata(
33、hObject, handles)% Hints: get(hObject,String) returns contents of edit5 as text% str2double(get(hObject,String) returns contents of edit5 as a double% - Executes during object creation, after setting all properties.function edit5_CreateFcn(hObject, , )% hObject handle to edit5 (see GCBO)% eventdata
34、reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc endfunction edit6_Callback(hObject, , handles)% hObject handle to edit6 (see G
35、CBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)input = str2num(get(hObject,String);if (isempty(input)set(hObject,String,0.32)endguidata(hObject, handles)% Hints: get(hObject,String) returns contents of edit6 as text%
36、str2double(get(hObject,String) returns contents of edit6 as a double% - Executes during object creation, after setting all properties.function edit6_CreateFcn(hObject, , )% hObject handle to edit6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles n
37、ot created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc end% - Executes on button press in pushbutton6.function pushbutton6_Callback(, , handles)global TH1;TH1 = str2double(get(handles.edit3,String);% hObject handle
38、 to pushbutton6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% - Executes on button press in pushbutton7.function pushbutton7_Callback(, , handles)% hObject handle to pushbutton7 (see GCBO)global TH2;TH2 = str
39、2double(get(handles.edit4,String);% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% - Executes on button press in pushbutton8.function pushbutton8_Callback(, , handles)% hObject handle to pushbutton8 (see GCBO)global TS;TS
40、 = str2double(get(handles.edit5,String);% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% - Executes on button press in pushbutton9.function pushbutton9_Callback(, , handles)global TV;TV = str2double(get(handles.edit6,String);% hObject handle to pushbutton9 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)界面如图结果如图Button 和 Edit text 未按照顺序来排, 自己可以稍微看下 先点打开 输入数据后再依次点选择 确定 确定 确定 确定 之后进行消除 最后的结果与所选的图像及参数有关。