You are on page 1of 27

function varargout = COINS_COUNTERespaldo(varargin)

gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ...

'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @COINS_COUNTERespaldo_OpeningFcn, ... 'gui_OutputFcn', @COINS_COUNTERespaldo_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 COINS_COUNTERespaldo is made visible. function COINS_COUNTERespaldo_OpeningFcn(hObject, eventdata, handles, varargin) % Choose default command line output for COINS_COUNTERespaldo

handles.output = hObject; handles.rgb = []; handles.noback = [];

% Update handles structure guidata(hObject, handles);

% UIWAIT makes COINS_COUNTERespaldo wait for user response (see UIRESUME) % uiwait(handles.figure1); if strcmp(get(hObject,'Visible'),'off') try handles.vidobj = videoinput('winvideo',2); % Update handles structure start(handles.vidobj); guidata(hObject, handles); vidRes = get(handles.vidobj, 'VideoResolution'); nBands = get(handles.vidobj, 'NumberOfBands'); hImage = image(zeros(vidRes(2), vidRes(1), nBands), 'Parent',... handles.video_cam); preview(handles.vidobj,hImage); catch msgbox('NO HAY CMARA CONECTADA. Cargando Profile.jpg.') hImage = image(imread('profile.jpg'), 'Parent', handles.video_cam); end

end

% --- Outputs from this function are returned to the command line. function varargout = COINS_COUNTERespaldo_OutputFcn(hObject, eventdata, handles)

% Get default command line output from handles structure varargout{1} = handles.output;

% --- Executes during object creation, after setting all properties. function figure1_CreateFcn(hObject, eventdata, handles)

% --- Executes on button press in CAPTURE. function CAPTURE_Callback(hObject, eventdata, handles) %handles.rgb=getsnapshot(handles.vidobj) im_rgb=imread('mon2.jpg'); try %handles.rgb=getsnapshot(handles.vidobj) im_rgb=imread('mon2.jpg');

catch handles.rgb = imread('profile.jpg'); end

% Update handles structure guidata(hObject, handles); %image(handles.rgb,'Parent',handles.img_capture); image(im_rgb,'Parent',handles.img_capture); axes(handles.img_capture) axis off

% --- Executes on button press in SAVE. function SAVE_Callback(hObject, eventdata, handles) % hObject handle to SAVE (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) rgb = getimage(handles.img_capture); if isempty(rgb), return, end %guardar como archivo fileTypes = supportedImageTypes; % Funcin auxiliar. [f,p] = uiputfile(fileTypes); if f==0, return, end fName = fullfile(p,f); imwrite(rgb,fName); msgbox(['Imagen guardada en ' fName]); function fileTypes = supportedImageTypes % Funcin auxiliar: formatos de imgenes.

fileTypes = {'*.jpg','JPEG (*.jpg)';'*.tif','TIFF (*.tif)';... '*.bmp','Bitmap (*.bmp)';'*.*','All files (*.*)'};

% --- Executes on button press in CONTADOR. function CONTADOR_Callback(hObject, eventdata, handles) im_color = imread('mon2.jpg'); %im_color = imread(handles.rgb); % brillo=5; % im_color=im_color-(brillo); % [f c]=size(im_color(:,:,1)); % k=3; % for m=1:k % % j=0; i=0;

% for i=1:f % % % % % % end for j=1:c if(im_color(i,j,k)>245); im_color(i,j,k)=245; end

% end % end

% image(im_color,'Parent',handles.img_capture); % axes(handles.img_capture); % imshow(im_color); % axis off; im_colorbw1=im_color(:,:,1)<128; im_colorc1=imcomplement(im_colorbw1); im_colorbw2=im_color(:,:,2)<1280; im_colorc2=imcomplement(im_colorbw2); im_colorbw3=im_color(:,:,3)<128; im_colorc3=imcomplement(im_colorbw3);

im_colorc=im_colorc1|im_colorc2|im_colorc3; im_color1=immultiply(im_colorc,im_color(:,:,1)); im_color2=immultiply(im_colorc,im_color(:,:,2)); im_color3=immultiply(im_colorc,im_color(:,:,3)); im_color(:,:,1)=im_color1; im_color(:,:,2)=im_color2; im_color(:,:,3)=im_color3;

im_gris=rgb2gray(im_color); im_umb=graythresh(im_gris) im_bww=im2bw(im_gris,im_umb); im_fill0=imfill(im_bww,'holes');

se0=strel('disk',4,0); im_open0=imopen(im_fill0,se0); % imb=zeros(size(im_open0));

image(im_colorc,'Parent',handles.img_capture); axes(handles.img_capture); imshow(im_colorc); axis off; dimopen=size(im_open0) [L Ne]=bwlabel(im_open0); a=Ne prop=regionprops(L,'All');1 areas = [prop.Area]; perimetros=[prop.Perimeter]; FC=4*pi*areas./(perimetros).^2 circ=find(FC>0.85&FC<1.1) a_circ=areas(circ);

prop0=prop(circ); for idx = 1:length(prop0) h rectangle('Position',prop0(idx).BoundingBox,'Curvature',[0.8,0.4],'LineWidth',2,'LineStyle','--'); set(h,'EdgeColor',[0 0 1]); hold on =

end

im_circ=ismember(L,circ); image(im_circ,'Parent',handles.fotografia); axes(handles.fotografia); imshow(im_circ); axis off;

im_color1=immultiply(im_circ,im_color(:,:,1)); im_color2=immultiply(im_circ,im_color(:,:,2)); im_color3=immultiply(im_circ,im_color(:,:,3)); im_color(:,:,1)=im_color1; im_color(:,:,2)=im_color2; im_color(:,:,3)=im_color3; % image(im_color,'Parent',handles.img_capture); axes(handles.img_capture); imshow(im_color); axis off; % %definimos la tolerancia tol=5;%26 % %tomamos una muestra de color de la imagen que % %est siendo mostrada

pixn=impixel pixc=impixel pixl=impixel pixb=impixel % %definimos los colores de referencia para niquel crefRn=double(pixn(1,1)); crefGn=double(pixn(1,2)); crefBn=double(pixn(1,3)); % %%%color ref del cobre crefRc=double(pixc(1,1)); crefGc=double(pixc(1,2)); crefBc=double(pixc(1,3)); % %%%color ref del laton crefRl=double(pixl(1,1)); crefGl=double(pixl(1,2)); crefBl=double(pixl(1,3)); %%%color ref del bronce crefRb=double(pixb(1,1)); crefGb=double(pixb(1,2)); crefBb=double(pixb(1,3)); % % C5=[crefRn crefRc crefRl crefRb] C6=[crefGn crefGc crefGl crefGb]

C7=[crefBn crefBc crefBl crefBb]

C5media=mean(C5) C6media=mean(C6) C7media=mean(C7)

if((C5media==C6media)&(C5media==C7media)) coln=C5(1) colc=C5(2) coll=C5(3) colb=C5(4)

coln1=C5(1) colc1=C5(2) coll1=C5(3) colb1=C5(4)

k=1 k1=1 end if(C5media<=C6media&C6media<=C7media) coln=C5(1) colc=C5(2) coll=C5(3)

colb=C5(4)

coln1=C6(1) colc1=C6(2) coll1=C6(3) colb1=C6(4)

k=1 k1=2 end

if(C5media<=C7media&C7media<=C6media) coln=C5(1) colc=C5(2) coll=C5(3) colb=C5(4)

coln1=C7(1) colc1=C7(2) coll1=C7(3) colb1=C7(4)

k=1 k1=3

end

if(C6media<=C5media&C5media<=C7media) coln=C6(1) colc=C6(2) coll=C6(3) colb=C6(4)

coln1=C5(1) colc1=C5(2) coll1=C5(3) colb1=C5(4)

k=2 k1=1 end

if(C6media<=C7media&C7media<=C5media) coln=C6(1) colc=C6(2) coll=C6(3) colb=C6(4)

coln1=C7(1) colc1=C7(2) coll1=C7(3) colb1=C7(4) k=2 k1=3 end

if(C7media<=C6media&C6media<=C5media) coln=C7(1) colc=C7(2) coll=C7(3) colb=C7(4)

coln1=C6(1) colc1=C6(2) coll1=C6(3) colb1=C6(4) k=3 k1=2 end if(C7media<=C5media&C5media<=C6media) coln=C7(1) colc=C7(2)

coll=C7(3) colb=C7(4)

coln1=C5(1) colc1=C5(2) coll1=C5(3) colb1=C5(4) k=3 k1=1 end

tkn=im_color(:,:,k)>=(coln-tol)&im_color(:,:,k)<=(coln+tol); tkc=im_color(:,:,k)>=(colc-tol)&im_color(:,:,k)<=(colc+tol); tkl=im_color(:,:,k)>=(coll-tol)&im_color(:,:,k)<=(coll+tol); tkb=im_color(:,:,k)>=(colb-tol)&im_color(:,:,k)<=(colb+tol);

tkn1=im_color(:,:,k1)>=(coln1-tol)&im_color(:,:,k1)<=(coln1+tol); tkc1=im_color(:,:,k1)>=(colc1-tol)&im_color(:,:,k1)<=(colc1+tol); tkl1=im_color(:,:,k1)>=(coll1-tol)&im_color(:,:,k1)<=(coll1+tol); tkb1=im_color(:,:,k1)>=(colb1-tol)&im_color(:,:,k1)<=(colb1+tol);

segn(:,:,k)=tkn;

segc(:,:,k)=tkc; segl(:,:,k)=tkl; segb(:,:,k)=tkb;

segn1(:,:,k1)=tkn1; segc1(:,:,k1)=tkc1; segl1(:,:,k1)=tkl1; segb1(:,:,k1)=tkb1; dimsegn=size(segn)

im_in=segn(:,:,k)|segc(:,:,k)|segl(:,:,k)|segb(:,:,k); im_in1=segn1(:,:,k1)|segc1(:,:,k1)|segl1(:,:,k1)|segb1(:,:,k1);

A=im_in; B=im_in1; C=double(A).*double(B);

image(im_in,'Parent',handles.img_capture); axes(handles.img_capture); imshow(im_in); axis off;

im_cleanin= bwmorph(im_in,'clean');

im_fillin = bwmorph(im_cleanin,'fill',Inf); se=strel('disk',5) im_dilatein = imdilate(im_fillin,se); im_rop = bwareaopen(im_dilatein,500); se1=strel('line',15,0); se2=strel('line',15,90); im_erodein = imerode(im_rop,[se1, se2]); im_erodein = imerode(im_erodein,[se1, se2]); im_a= bwmorph(im_erodein,'clean'); im_d= imdilate(im_a,se); im_d= imdilate(im_d,se);

im_r=imsubtract(im_circ,im_d); [Lo Neo]=bwlabel(im_circ); ao=Neo propo=regionprops(Lo,'All'); areas_o = [propo.Area]

[Lr Ner]=bwlabel(im_r); ar=Ner propr=regionprops(Lr,'All'); areas_r = [propr.Area]

for i=1:Neo if(areas_r(i)==areas_o(i)) areas_r(i)=0; j=Neo+1; else j=i end ix(i)=j end a_seg=ismember(Lr,ix); im_aseg=imfill(a_seg,'holes');

im_t1=uint8(double(im_aseg).*double(im_color(:,:,1))); im_t2=uint8(double(im_aseg).*double(im_color(:,:,2))); im_t3=uint8(double(im_aseg).*double(im_color(:,:,3))); im_t(:,:,1)=im_t1; im_t(:,:,2)=im_t2; im_t(:,:,3)=im_t3;

image(im_t,'Parent',handles.fotografia); axes(handles.fotografia); imshow(im_t); axis off; %%%%%%%%%%%%%%%%%%

% % m100 = find(areas_s > 12000 & areas_s <13000) % prop100 = prop(m100) %cantidad=length(prop100)*1; % for idx = 1:length(prop100) % h = rectangle('Position',prop100(idx).BoundingBox,'Curvature',[0.8,0.4],'LineWidth',2,'LineStyle','--'); % % set(h,'EdgeColor',[0.75 0 0.75]); hold on

% end %% % m50 = find(areas_s >20000 & areas_s < 20500) % prop50 = prop(m50); %cantidad=cantidad+length(prop100)*0.5; % for idx = 1:length(prop50) % h rectangle('Position',prop50(idx).BoundingBox,'Curvature',[0.8,0.4],'LineWidth',2,'LineStyle','--'); % % set(h,'EdgeColor',[0.75 0.5 0]); hold on =

% end % % m25 = find(areas_s > 8000 & areas_s < 9000); % prop25 = prop(m25); % % for idx = 1:length(prop25)

% h rectangle('Position',prop25(idx).BoundingBox,'Curvature',[0.8,0.4],'LineWidth',2,'LineStyle','--'); % % set(h,'EdgeColor',[0.4 0.97 0.8]); hold on

% end %%%% % m10 = find(areas_s > 4000 & areas_s < 4100); % prop10 = prop(m10); % %% % for idx = 1:length(prop10) % h rectangle('Position',prop10(idx).BoundingBox,'Curvature',[0.8,0.4],'LineWidth',2,'LineStyle','--'); % % set(h,'EdgeColor',[0.25 .7 0]); hold on =

% end %%%% % m5 = find(areas_s > 6000 & areas_s < 7000); % prop5 = prop(m5); % hold on; % %% % for idx = 1:length(prop5) % h rectangle('Position',prop5(idx).BoundingBox,'Curvature',[0.8,0.4],'LineWidth',2,'LineStyle','--'); % % set(h,'EdgeColor',[0.1 0 .8]); hold on =

% end %%% % m1 = find(areas_s > 4700 & areas_s < 4900); % prop1 = prop(m1); % hold on; % % % for idx = 1:length(prop1) % h rectangle('Position',prop1(idx).BoundingBox,'Curvature',[0.8,0.4],'LineWidth',2,'LineStyle','--'); % % set(h,'EdgeColor',[0.75 0 0]); hold on =

% end %% % hold off %

% --- Executes on selection change in listbox1. function listbox1_Callback(hObject, eventdata, handles) % hObject handle to listbox1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox1 contents as cell array % contents{get(hObject,'Value')} returns selected item from listbox1

% --- Executes during object creation, after setting all properties. function listbox1_CreateFcn(hObject, eventdata, handles) % hObject handle to listbox1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows. % See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end

% --- Executes on button press in SALIR. function SALIR_Callback(hObject, eventdata, handles) % hObject handle to SALIR (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) clear,clc,close all

% --- Executes on selection change in border.

function border_Callback(hObject, eventdata, handles) % hObject handle to border (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns border contents as cell array % contents{get(hObject,'Value')} returns selected item from border

T=handles.threshold; I=handles.rgb;

ME = get(handles.border,'Value'); %// Obtener mtodos de filtrado EDGE

%//Condiciones para men de opciones:

if ME==1

im_g=rgb2gray(I); %pasar imgen a escala de grises umb=graythresh(im_g); %definir umbral bw=im2bw(im_g, umb); %Binarizar imgen handles.bw1=edge(bw,'sobel',T);

axes(handles.img_capture) imshow(handles.bw1)

axis off

elseif ME==2

im_g=rgb2gray(I); %pasar imgen a escala de grises umb=graythresh(im_g); %definir umbral bw=im2bw(im_g, umb); %Binarizar imgen handles.bw1=edge(bw,'prewitt',T);

axes(handles.img_capture) imshow(handles.bw1) axis off

elseif ME==3

im_g=rgb2gray(I); %pasar imgen a escala de grises umb=graythresh(im_g); %definir umbral bw=im2bw(im_g, umb); %Binarizar imgen handles.bw1=edge(bw,'roberts',T);

axes(handles.img_capture) imshow(handles.bw1) axis off

elseif ME==4

im_g=rgb2gray(I); %pasar imgen a escala de grises umb=graythresh(im_g); %definir umbral bw=im2bw(im_g, umb); %Binarizar imgen handles.bw1=edge(bw,'log',T)

axes(handles.img_capture) imshow(handles.bw1) axis off

elseif ME==5

im_g=rgb2gray(I); %pasar imgen a escala de grises umb=graythresh(im_g); %definir umbral bw=im2bw(im_g, umb); %Binarizar imgen handles.bw1=edge(bw,'zerocross');

axes(handles.img_capture) imshow(handles.bw1) axis off

elseif ME==6

im_g=rgb2gray(I); %pasar imgen a escala de grises umb=graythresh(im_g); %definir umbral bw=im2bw(im_g, umb); %Binarizar imgen handles.bw1=edge(bw,'canny',T);

axes(handles.img_capture) imshow(handles.bw1) axis off

end

function threshold_Callback(hObject, eventdata, handles) % hObject handle to threshold (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 threshold as text % str2double(get(hObject,'String')) returns contents of threshold as a double

Val=get(hObject,'String'); NewVal = str2double(Val); handles.threshold=NewVal; guidata(hObject,handles);

% --- Executes during object creation, after setting all properties. function border_CreateFcn(hObject, eventdata, handles) % hObject handle to border (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end

% --- Executes during object creation, after setting all properties. function threshold_CreateFcn(hObject, eventdata, handles) % hObject handle to threshold (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 ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

set(hObject,'BackgroundColor','white'); end