As Yuval pointed out it really depends on what you call regular or irregular. But just for a quick start I take a naive assumption that regular means circle-like. Now to check if a body of pixels is circle-like I use regionprops
function to calculate 'EquivDiameter'
property:
Returns a scalar that specifies the diameter of a circle with the same area as the region. Computed as sqrt(4*Area/pi).
Then I use IOU measure to calculate irregularity. To test the code I used image from here.
The code is like this:
clc; close all; clear variables;
%% read image
I = imresize(rgb2gray(imread('maxresdefault.jpg')), 0.5);
I = imfill(medfilt2(I, [3 3])<230,'holes');
% I = imread('1.gif')>0; % load your GIF image
imshow(I); title('Shapes');
%% check irregularity
[L, n] = bwlabel(I);
S = regionprops('table', L, 'Centroid', 'EquivDiameter');
[X, Y] = meshgrid(1:size(I, 2), 1:size(I, 1));
r = zeros(1, n);
for i=1:n
shape = L==i;
circle = ((X-S.Centroid(i, 1)).^2+(Y-S.Centroid(i, 2)).^2)<(S.EquivDiameter(i)/2)^2;
r(i) = 1-bwarea(shape&circle)/bwarea(shape|circle); % calculating ROI
end
%% showing intersection
figure;
circs = im2double(repmat(I, 1, 1, 3));
circs = insertShape(circs, 'filledcircle', [S.Centroid S.EquivDiameter/2], 'LineWidth', 5);
imshow(circs); title('Intersections');
%% showing results
figure;
map = jet(256);
O = zeros(size(I), 'uint8');
for i=1:n
ind = max(1,round(r(i)*256));
O(L==i) = ind;
end
O = label2rgb(O, map);
O = insertText(O, S.Centroid, r, 'AnchorPoint', 'center', 'FontSize', 18);
imshow(O); title('Irregularity');
colormap jet;
colorbar('ticks', [0 1], 'TickLabels',{'Regular','Irregular'})
Results:
