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/2hold onviscircles(centroids,radii);hold offtitle('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