MATLAB: Dented battery surface defect detection and defect measurement

battery_system_managementImage Processing Toolboxpower_electronics_controlsurface defect

hello, i need a help on how to detect the defect that occur on the surface of the battery? and how can i measure the defected area.. here is the code found but got some error.
[filename,pathname]=uigetfile({'*.bmp;*.tif;*.tiff;*.jpg;*.jpeg;*.gif','IMAGE Files(*.bmp,*.tif,*.tiff,*.jpg,*.jpeg,*.gif)'},'Choose Image File');
myimage = imread(cat(2,pathname,filename));
i = imresize(myimage,[512 512]);
imshow(i);
I = rgb2gray(i);
% f=figure,imshow(I);
% g=figure,imshow(J);
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(I), hy, 'replicate');
Ix = imfilter(double(I), hx, 'replicate');
gradmag = sqrt(Ix.^2 + Iy.^2);
K=figure,imshow(gradmag,[]);
set(K, 'visible','on');
filename = 'temp_file.jpg'
saveas(K, filename)
i1 = imread(filename)
delete(filename)
[x, y, rgb] = ind2sub([size(i1,1) size(i1,2) size(i1,3)], find(i1 ~= 255));
A = i1(min(x):max(x)-1,min(y):max(y)-1,:);
A = rgb2gray(A)
I = edge(A,'sobel')
%934 742
I = imcrop(I,[10 7 914 728]);
I = imresize(I, [2000 2500])
f=figure,imshow(I);
set(f, 'visible','on');
imwrite(I, filename)
bw = imread(filename)
delete(filename)
bw = im2bw(bw, graythresh(bw))
bw = bwareaopen(bw,65);
set(f, 'visible','on');
f, imshow(bw)
some pic of defect battery.

Best Answer

Measure the solidity of the blob with regionprops. If it's less than 1, there are defects/bays in the surface.
By the way, be aware that Duracell (part of Berkshire Hathaway) has a patent on the use of CT for looking at batteries.