function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled
% Last Modified by GUIDE v2.5 21-Nov-2019 21:53:48
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, 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 user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, 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
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
%%% 水滴算法
%%% 前景色黑色
clear;
img_rgb=imread('1.png');
img=rgb2gray(img_rgb);
level=graythresh(img);
Ibw=im2bw(img,level);
[hgt,wid]=size(Ibw);
%% 切入点
cut=zeros(hgt,3); % 共找3个切分点
startx=zeros(3,1);
starty=zeros(3,1);
for i=1:hgt/5
flag=false; % 标志是否找到候选
found=0; % 记录找到的个数
candx=0;
candy=0;
for j=2:wid-1
pre=Ibw(i,j-1);
now=Ibw(i,j);
nex=Ibw(i,j+1);
if now==0 && nex~=0
flag=true;
candx=j;
candy=i;
end
if flag==true && pre~=0 && now==0 % 说明前面找到切分点
found=found+1;
startx(found)=candx+1;
starty(found)=candy;
end
end
if found==3
break;
end
end
%% 滴水
cut=[startx(1)*ones(length(cut),1),startx(2)*ones(length(cut),1),startx(3)*ones(length(cut),1)];
for k=1:3
i=starty(k); % 从切分点开始
if k==1
Proc=Ibw;
else
for q=k:3
cut(:,q)=cut(:,q)-min(cut(:,k-1));
% startx(q)=startx(q)-min(cut(:,k-1));
end
Proc=part2;
end
j=cut(1,k);
omiga=[1,2,3,4,5]; % 权值向量
I=[0,0]; % 动量向量
sigma=0; % 相加的结果
W=zeros(hgt,1);
while i<hgt
z=[Proc(i,j-1),Proc(i,j+1),Proc(i+1,j+1),Proc(i+1,j),Proc(i+1,j-1)];
sigma=dot(omiga,z);
if sigma==0 || sigma==15
W(i)=4;
else
W(i)=max(omiga.*z);
end
if sigma==15
i=i+1;
I=[0,1];
else
if W(i)==1
if isequal(I,[1,0])
i=i+1;
I=[0,1];
else
j=j-1;
I=[-1,0];
end
elseif W(i)==2
if isequal(I,[-1,0])
i=i+1;
I=[0,1];
else
j=j+1;
I=[1,0];
end
elseif W(i)==3
j=j+1;
i=i+1;
I=[1,1];
elseif W(i)==4
if isequal(I,[1,0])
j=j+1;
i=i+1;
I=[1,1];
elseif isequal(I,[-1,0])
i=i+1;
I=[0,1];
else
i=i+1;
I=[0,1];
end
elseif W(i)==5
j=j-1;
i=i+1;
I=[-1,1];
end
end
cut(i,k)=j;
end
%% 切分
wid1=max(cut(:,k));
wid2=size(Proc,2)-min(cut(:,k));
part1=ones(hgt,wid1);
part2=ones(hgt,wid2);
for p=1:hgt
part1(p,1:cut(p,k))=Proc(p,1:cut(p,k));
part2(p,cut(p,k)-min(cut(:,k))+1:wid2)=Proc(p,cut(p,k)+1:size(Proc,2));
end
%% show
imwrite(part1,strcat(num2str(k),'.jpg'))
if k==3
imwrite(part2,strcat(num2str(k+1),'.jpg'))
end
end
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
axes(handles.axes1);
image = imread('1.png');
imshow(image);
axes(handles.axes2);
image = imread('1.jpg');
imshow(image);
axes(handles.axes3);
image = imread('2.jpg');
imshow(image);
axes(handles.axes4);
image = imread('3.jpg');
imshow(image);
axes(handles.axes5);
image = imread('4.jpg');
imshow(image);
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)