Eliminating Groups of Connected Pixels.

I would like to be able to look at the matrices of an image and convert certain connected pixel groups from 1’s to 0’s. I would like to be able look at each individual rows and columns and convert groups or individual pixel that are smaller than a defined amount of pixels. I have used the bwareaopen function to eliminate pixel groups that are smaller than a defined amount. But I cannot determine how to use this concept for individual rows or columns.
For example, I would like to eliminate the square in the first image below. . .
. .

 Accepted Answer

First of all, you shouldn't use highly compressed jepg images for image analysis or else you get bad artifacts like you're seeing.
The "square" is the outermost object and will have label 1. So label everything and remove it. Something like this (untested off the top of my head);
[labeledImage, numberOfRegions] = bwlabel(binaryImage);
% Binarize the labeled image to get back to a binary image again.
binaryImage = labeledImage > 1; % Keep all except region #1.

5 Comments

Thank you for your help. And thank you for pointing out the fact I was using JPEGs! Initially I was using .jpg, but I went back and changed everything to .bmp. Apparently I missed a few. I appreciate you pointing that out for me. I am using only bitmaps. after rerunning my code I see how this makes a difference. Also, I am VERY new to the MATLAB world. Basically I began this summer. For this particular image I would like to remove the square. But there are other applications where i would only want to remove parts that are smaller than a certain width. I have removed the entire square using altered example code: (But that not really my goal)
%% Eliminate the largest connected pixel group
BW = imread('Thresh.bmp');
BW= bwareaopen(BW,2000, 8); % eliminate noise
figure, imshow(BW), title('Absolute Difference')
CC = bwconncomp(BW);
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
BW(CC.PixelIdxList{idx}) = 0;
figure, imshow(BW), title('Larges Pixel Group Eliminated')
I would like to be able to only delete certain portions of the square that are thinner than a defined number of pixels rather than the entire square.
Thank you for for your help and any additional guidance. Amanda
I have a function to remove the largest or smallest N regions from an image. See attached m-file.
To "delete certain portions of the square that are thinner than a defined number of pixels rather than the entire square." you need to call bwdist() and get the thickness of the regions. Then you need to skeletonize the binary image to get the centerline. Then multiply the skeleton times the distance transform image to get the radii just at the centerline.
radii = skelImage .* edtImage;
Now threshold to find radii less than some number, say 3:
thinPortions = radii < 3;
Now call imdilate() to enlarge those by 3 pixels
mask = imdilate(thinPortions, true(3)); % or something like that.
and then use that to erase your original binary image.
binaryImage(mask) = false; % Erase pixels in mask of thin things.
Thank you again!! I tried this way but was getting an image filled with all zeros. But you answer did direct me to another solution. I created a square template, the used imdilate() to enlarge it. I subtracted the dilated template from the original binary image. It worked out GREAT! Thank you very much for your help.
Here is the code I used:
TemplateBW = tmpltB;
se = strel('square',17);
dilatedBW = imdilate(TemplateBW,se);
figure, imshow(TemplateBW),title('Edge Template')
figure, imshow(dilatedBW), title('Dialated Edge Template')
BW= imread('Thresh.bmp'); BW= bwareaopen(BW, 2000); figure, imshow(BW), title('Binary Image')
Dif = BW - dilatedBW;
figure, imshow(Dif)
Dif = im2bw(Dif);
subplot(2,2,1), imshow(DS),
title('Data Set Image 04')
subplot(2,2,2), imshow(sampleIm),
title('Template for Ideal Part')
subplot(2,2,3), imshow(K),
title('Absoulute Difference Before Thresholding')
subplot(2,2,4), imshow(Dif),
title('Subtraction of Dialated Edge Template'),
[L ,Num] = bwlabel(Dif);
s = regionprops(Dif, 'basic');
s(1)
centroids = cat(2, s.Centroid);
hold on plot(centroids(:,1), centroids(:,2), 'r*') for n=1: length(s) rectangle('Position', s(n).BoundingBox, 'EdgeColor', 'g', 'LineWidth',2)
end
hold off
.
sorry some of my variables are unclear or not defined in this portion of the code.
Well if you already knew where the things were so that you could draw a template, you can just erase them a lot easier than creating a template, then dilating and subtracting fro original. You can simply erase:
BW(row1:row2, col1:col2) = false;
Just do that 4 times, once for each side of the square. I'm pretty sure it will be faster also.
Hello Image Analyst,
Can you please tell me how to calculate the the distance transform image knowing that i want to delete edges from a skeleton that are >12 pixels
thak you!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!