MATLAB: How to get depth information from surface image

image analysisimage processingMATLABsurface geometry

I have written the following matlab code to do the following:-
  • load rgb image of surface
  • contrast stretch
  • convert rgb to gray scale
  • convert gray to binary image
  • select appropriate portion of the image
  • fill hole in the binary image
  • find and plot the centroid of image
  • estimate radius of circular part and plot circle
My aim is to develop the SIMPLEST matlab code for automatic detection of indentation and calculate the diameter and depth of the indentation (if possible other geometrical properties) from a sample image.
The code is shown below:
% read the image from file
rgbImage = imread('V1.nn.bmp');
subplot(2,3,1);
imshow(rgbImage,[]);
axis on;
title('original image');
% Image adjust
Istretch = imadjust(rgbImage,stretchlim(rgbImage));
subplot(2,3,2);
imshow(Istretch,[])
axis on;
title('Contrast stretched image')
% convert the original image into gray image
gry = rgb2gray(Istretch);
subplot(2,3,3);
imshow(gry,[]);
axis on;
title('original gray image');
% convert gray image to binary image
level = graythresh(Istretch);
binaryImage = im2bw(gry,level);
subplot(2,3,4);
imshow(binaryImage);
title('binary masked image');
% select appropriate portion of the image
areafilt = bwareafilt(binaryImage, 1, 'largest');
subplot(2,3,5);
imshow(areafilt);
title('main part of the image');
% fill hole in the binary image
fillHole=imfill(areafilt,'holes');
subplot(2,3,6);
imshow(fillHole);
title('filled hole of the image');
%%find and plot the centroid of image
stats = regionprops('table',fillHole,'Centroid','Area',...
'MajorAxisLength','MinorAxisLength','EquivDiameter')
figure;
imshow(fillHole);
centroids=cat(1,stats.Centroid);
areas=cat(1,stats.Area);
hold on;
plot(centroids(:,1), centroids(:,2), 'r*','MarkerSize', 10, 'LineWidth', 1);
hold off;
% estimate radius of circular part and plot circle
diameter = mean([stats.MajorAxisLength stats.MinorAxisLength],2)
radii = diameter/2
hold on
viscircles(centroids,radii);
hold off
title('circle with centroid of the image');
I = surf(gry);
I tried surf function for depth information and it seems to get messy. I need help from image specialist to improve the code from above to meet my aim. The image V1.nn.bmp is attached, please find it.
Thank you.

Best Answer

Mozilla/Firefox has banned that web site saying it's been improperly configured, so we can't see it, unless someone is running an old browser. Basically the original image should have depth information in it, times a scale factor. You need to know what that scale factor is, like 0 gray levels is 10 cm, and 255 gray levels is 124 cm or whatever. Then you can create a look up table where any graylevel can be translated into a depth. But certainly don't do a contrast stretch - that will just destroy your depth calibration.
Related Question