How can I delete certain unwanted user-defined regions/blobs in segmentation?

4 views (last 30 days)
Andrea Labudzki
Andrea Labudzki on 12 Jan 2022
Commented: Andrea Labudzki on 13 Jan 2022
Hello, I have a calibration pattern that I am trying to implement in my code in order to convert pixel distances into real world distances. I have converted the pattern to binary and have performed simple filtering on the image. Next, I performed segmentation on the regions of the image. The original image is:
(I am aware of the lens distortion. This is not important to me right now.) I find the centroids of the blobs and put them into an array. I would like to use this array to find the distances between the blobs in the image. However, due to some imperfections, some of the regions are not ideal. Here you can see the centroids of the regions labeled with stars *:
I would like to be able to "delete" them by having the user click on the unwanted regions, such as the region in the upper left corner that is divided in two due to the cross hairs in the system that were not deleted from filtering. Is this possible? If so, how could I do this and what would be the best way? The source code is below:
fileName = 'calibration.png';
img = imread(fileName);
img = rgb2gray(img);
% Binarize image + filter
thresValue = 130;
mask = img > thresValue;
se = strel('disk', 9);
mask = imopen(mask, se);
%mask = mat2gray(c);
% Crop image
rect = [500 1000 1400 1350];
mask = imcrop(mask, rect);
% Show image
subplot(1, 3, 1);
imshow(mask)
% Segmentation
[labeledImage, numOfBlobs] = bwlabel(mask, 4);
% Add pseudo-colors to regions
pseudoColoredImage = label2rgb (labeledImage, 'lines', 'k', 'shuffle');
% Display pseudo-colored image
subplot(1, 3, 2);
imshow(pseudoColoredImage);
impixelinfo;
axis('on', 'image');
title('Pseudo-colored Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Retrieve centers of circles
blobProps = regionprops(labeledImage, mask, 'Centroid');
numOfBlobs = size(blobProps, 1);
% Find x-distances between circles -> ??
centroids = cat(1,blobProps.Centroid);
%subplot(1, 3, 3);
figure
imshow(labeledImage)
hold on
plot(centroids(:,1),centroids(:,2),'b*')

Answers (2)

Image Analyst
Image Analyst on 12 Jan 2022
How about using BW2 = bwselect(BW,c,r)?
Something like (untested)
uiwait(helpdlg('Click on the bad blobs. Type Enter when done'));
[x, y] = ginput();
x = round(x);
y = round(y);
badBlobs = bwselect(bw, x, y);
bw = bw & ~badblobs;
  2 Comments
Andrea Labudzki
Andrea Labudzki on 13 Jan 2022
By the way, I wanted to find the distance between centroids by accessing their positions in the array and just subtracting one x value from the previous x value, but the order of labeling does not work this way. Do you know how I could reorder the labeling so that the 1st blob is the upper left one, the second is the one to its right, etc?

Sign in to comment.


yanqi liu
yanqi liu on 13 Jan 2022
yes,sir,may be use block distance to auto filter,such as
clc; clear all; close all;
fileName = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/860515/image.png';
img = imread(fileName);
img = rgb2gray(img);
% Binarize image + filter
thresValue = 130;
mask = img > thresValue;
se = strel('disk', 9);
mask = imopen(mask, se);
%mask = mat2gray(c);
% Crop image
rect = [500 1000 1400 1350];
mask = imcrop(mask, rect);
% Show image
subplot(1, 3, 1);
imshow(mask)
% Segmentation
[labeledImage, numOfBlobs] = bwlabel(mask, 4);
% Add pseudo-colors to regions
pseudoColoredImage = label2rgb (labeledImage, 'lines', 'k', 'shuffle');
% Display pseudo-colored image
subplot(1, 3, 2);
imshow(pseudoColoredImage);
%impixelinfo;
axis('on', 'image');
title('Pseudo-colored Image', 'FontSize', 15, 'Interpreter', 'None');
hold on
drawnow;
% Retrieve centers of circles
blobProps = regionprops(labeledImage, mask, 'Centroid');
numOfBlobs = size(blobProps, 1);
% Find x-distances between circles -> ??
centroids = cat(1,blobProps.Centroid);
%subplot(1, 3, 3);
figure
imshow(labeledImage)
hold on
plot(centroids(:,1),centroids(:,2),'b*')
% compute distance between them
bw = labeledImage;
a=pdist(centroids);
b = squareform(a);
b(b==0) = NaN;
[r,c] = find(b<100);
for i = 1 : length(r)
plot(centroids([r(i) c(i)],1),centroids([r(i) c(i)],2),'ro-');
% delete it
bw(labeledImage==r(i)) = 0;
bw(labeledImage==c(i)) = 0;
end
bw = logical(bw);
figure; imshow(bw);

Community Treasure Hunt

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

Start Hunting!