function dirtDetection_Callback(hObject, eventdata, handles)
global RGB
global total_Area_Live
%invert the binary condition
%bw2 = imcomplement(b);
%imwrite(bw2, 'B3.png');
% Display image in axesImage1.
%axes(handles.axesrgb);
%imshow(bw2);
%clear non-dirt binary
%BWc1 = imclearborder(bw2,4)
%imwrite(BWc1, 'I3.png');
% Display image in axesImage2.
%axes(handles.axesbinary);
%imshow(BWc1), title 'Dirt';
%Measure area of image regions
%STATS = regionprops(BWc1,'PixelList');
%total = bwarea(BWc1);
%if total <= 0
%set(handles.resultdirt,'String','CLEAN EGG');
%else total >= 1
%set(handles.resultdirt,'String','DIRT EGG');
%end
%rgb2gray
gray = rgb2gray(RGB);
%filtering
filter = medfilt2(gray,[3 3])
% Take the red channel.
grayImage = filter
,:,1);
% Find the mask
maskImage = grayImage > 80;
% Shrink it a bit.
maskImage = imerode(maskImage, ones(5));
% Calculate the texture.
textureImage = stdfilt(grayImage);
% It shows the edge of the egg, so mask away the edge.
textureImage = textureImage .* double(maskImage);
% Threshold it and fill it
binaryTexture = textureImage > 10;
binaryTexture = imfill(binaryTexture, 'holes');
binaryTexture = imclose(binaryTexture, ones(9));
binaryTexture = imfill(binaryTexture, 'holes');
% Display the image.
axes(handles.axesrgb);
imshow(grayImage), title('Binary image');
%Measure area of image regions
%STATS = regionprops(binaryTexture);
%Measure dirt of egg regions
hold on;
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
% Plot the borders of all the coins on the original grayscale image using
% the coordinates returned by bwboundaries.
boundaries = bwboundaries(binaryTexture);
numberOfBoundaries = size(boundaries);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary
,2), thisBoundary
,1), 'g', 'LineWidth', 2);
end
hold off;
total_Dirt_Live = bwarea(binaryTexture)
%calculate the percentage of dirt
Percent_Live=(total_Dirt_Live/total_Area_Live)*100
if Percent_Live == 0
set(handles.resultdirt,'String','CLEAN EGG');
else set(handles.resultdirt,'String',round(Percent_Live))
end