I have this image:
I need to identify various types of fruit, including pears, apples, oranges and bananas.
Initially I started by recognizing them through the form, with the help of this code:
Im = imread('frame147.jpg'); figure,imshow(Im);title('Imagem Original');
I1 = rgb2gray(Im);
level=graythresh(I1); bw=im2bw(I1,level);
[I2, Num]=bwlabel(bw);
objetos=regionprops(I2); figure,imshow(bw);title('Binario da Imagem com box');
for n=1:size(objetos,1) rectangle('Position',objetos(n).BoundingBox,'EdgeColor','y','LineWidth',1); end
for n=1:size(find([objetos.Area]<100),2) s=find([objetos.Area]<100); d=round(objetos(s(n)).BoundingBox); bw(d(2):d(2)+d(4),d(1):d(1)+d(3))=0; end figure,imshow(bw);title('Objetos Definidos');
[I2, Ne]=bwlabel(bw);
objetos2=regionprops(I2,'ALL');
for n=1:size(objetos2,1) X=rectangle('Position',objetos2(n).BoundingBox,'EdgeColor','y','LineWidth',1); end
for n=1:size(find([objetos2.Area]>100),2) X1=objetos2(n).Centroid(1); X2=objetos2(n).Centroid(2); text('Position',[X1,X2],'String',n,'color','b') end fprintf('\n\nForam encontradas %d objetos:\n\n',size(objetos2,1))
tol1=0.9; % tolerancia circulos – oranges and aples tol2=0.2; % tolerancia triangulos – pear tol3=0.9; % tolerancia retangulos – banana
for n=1:size(objetos2,1) for m=1:15 dif(m)=(objetos2(n).Extrema(m)/objetos2(n).Extrema(m+1)); end % Circulos: oranges or apples
if ((4*pi*(objetos2(n).Area))/((objetos2(n).Perimeter)^2)>tol1); fprintf('Peça nº %d: Círculo.\n',n); Area=(objetos2(n).Area) % Triângulos: pears
elseif ((sqrt((objetos2(n).Area/(sqrt(3)/4)))>=(objetos2(n).Perimeter/3)*(1-tol2))&&((sqrt((objetos2(n).Area/(sqrt(3)/4))))<=(objetos2(n).Perimeter/3)*(1+tol2))) fprintf('Peça nº %d: Pêra.\n',n); Area=(objetos2(n).Area) %Retangulos: banana
elseif (dif(3)==1 && dif(7)==1 && dif(9)==1 && dif(13)==1) if (objetos2(n).MinorAxisLength/objetos2(n).MajorAxisLength>=(tol3)) fprintf('Peça nº %d: banana.\n',n); Area=(objetos2(n).Area) % Not a fruit
else fprintf('Peça nº %d: Não é uma peça de fruta.\n',n); Area=(objetos2(n).Area) end end end
bananas have not yet been able to adapt a formula to recognize it
but in the case of apples and oranges the only way to distinguish them would be through color recognition, but I can not find a method of doing so.
Can someone help me?
Best Answer