MATLAB: Removing an area from an image

image analysisskeletonisation

Hi – I'm looking to segment, calculate area and skeletonise a microscope image. I've gotten my segmentation working pretty ok (thanks to some threads I read by Image Analyst), and now want to remove particles in the image smaller that a certain size. I know the pixel dimensions of the image, and I have a scalebar, but how do I start referencing the image size in terms of micrometres instead of pixels? The image is 4140×3096 pixels, equalling 11 pixels per micrometer, and I wish to remove objects smaller than 20 micrometer squared.
Thanks in advance for any assistance or pointers.
I attach the image, and my current code below.
R7URFS-Segment.jpg
%Call HSVMask function, and assign result to img_segmented
Inimage=imread('R7URFS.jpg');
figure, imshow(Inimage);title('Original Image');
img_segment=HSVMask(Inimage); % call function HSVMask()
imwrite(img_segment,'R7URFS-Segment.jpg');
%figure, imshow(img_segment);title('Segmented Image Returned From Function (HSVMask)');
img_segment=imread('R7URFS-Segment.jpg');
img_gray=rgb2gray(img_segment);
figure, imshow(img_gray);title('Grayscale Segmented Image');
% Shrink and dilate the image
shrinkxtimes=28;
dilatextimes=15;
img_thin_x = bwmorph(img_gray, 'shrink', shrinkxtimes);
%figure,imshow(img_thin_x);title('Shrunk Image');
img_thin_dilated=bwmorph(img_thin_x, 'dilate', 1);% dilate the lines to connect broken parts.
%figure,imshow(img_thin_dilated);title('Dilated Image x1');
%[img_small_removed]=remove_small_objects(img_thin_dilated,4148.916); % call function remove_small_objects
%figure,imshow(img_small_removed);title('Small Parts Removed - Returned from Function (remove_small_objects)');
img_thin_dilated_2=bwmorph(img_thin_dilated, 'dilate', dilatextimes);
%figure,imshow(img_thin_dilated_2);title('Dilated Again x15');
se=strel('disk',10);
img_thin_dilated_2=imfill(img_thin_dilated_2,'holes');
img_thin_dilated_2=imopen(img_thin_dilated_2,se);
Ls=Zheng_Skeletonise(img_thin_dilated_2);
img_skeleton=bwmorph(Ls, 'dilate', 2);
figure,imshow(Ls);title('Complete Skeletonised Image');
imwrite(Ls,'R7URFS-Skeleton.jpg');
return

Best Answer

Please define "20 micrometer squared".
Is it 20 microns by 20 microns = 400 microns^2 ?
Or is if 20 microns^2 ?
I find your description ambiguous.
Try this:
pixelsPerMicron = 11;
minAcceptableAreaInSqMicrons = 20 * 20; % 20 microns by 20 microns, or just 20???
minAcceptableAreaInPixels = minAcceptableAreaInSqMicrons * pixelsPerMicron ^ 2;
binaryImage = bwareafilt(binaryImage, [minAcceptableAreaInPixels, inf]);
Replace binaryImage with whatever variable you want to operate on. You might want to have the min acceptable width of a particle be an edit field or slider on your GUI so the user can adjust it. For example
minWidth = str2double(handles.edtMinWidth.String);
minAcceptableAreaInSqMicrons = minWidth * minWidth; % Apply user's setting.