Filtering with regionprops and ismember

7 visualizzazioni (ultimi 30 giorni)
Alice Lam
Alice Lam il 31 Mag 2022
Commentato: Image Analyst il 31 Mag 2022
I'm trying to remove blobs in a sequence of images if they are not close to the location of the largest blob in the first image. However, when I use ismember, it seems like no blobs have been removed, with the left being the original and the right after ismember():
This is my code, and I have checked that keeperIndexes is not all indexes. My current output for keeperIndexes is a list of length 2, but I have more than two blobs left after ismember().
measurements = regionprops(filledImage, 'Centroid', 'Area');
allCentroids = [measurements.Centroid];
if i == 1
[~,ind] = max(cat(1,measurements.Area));
centroid = measurements(ind).Centroid;
allowableCentroidMax = double(centroid(1) + 100);
allowableCentroidMin = double(centroid(1) - 100);
allowableCentroidIndexes = (allCentroids > allowableCentroidMin) & (allCentroids < allowableCentroidMax);
end
keeperIndexes = find(allowableCentroidIndexes);
keeperBlobsImage = ismember(filledImage, keeperIndexes);

Risposte (2)

DGM
DGM il 31 Mag 2022
Modificato: DGM il 31 Mag 2022
I cropped the image out of your screenshot, so the sizes are probably wrong, but this is just an example in concept.
You appear to only be considering the horizontal distance between the centroids, but you were evaluating all elements of the centroid coordinates. Do vertical concatenation of the centroids and pick the first column so that you're only dealing with x-components.
Using find() isn't necessary. Just use the logical indices directly. Using ismember() doesn't work, because nothing in the image is anything other than 0 or 1. You might be able to do something if filledImage were a label image, but I'm assuming it's not. You can get the lndex lists of the blobs from regionprops() and then use the selected lists to eliminate unwanted blobs.
filledImage = imread('image.png')>128;
i = 1;
measurements = regionprops(filledImage, 'Centroid', 'Area','pixelidxlist');
allCentroids = vertcat(measurements.Centroid);
if i == 1
[~,ind] = max(cat(1,measurements.Area));
mainblobcentroid = measurements(ind).Centroid;
allowableCentroidMax = double(mainblobcentroid(1) + 100);
allowableCentroidMin = double(mainblobcentroid(1) - 100);
allowableCentroidIndexes = (allCentroids(:,1) > allowableCentroidMin) & (allCentroids(:,1) < allowableCentroidMax);
end
% this is a list of all pixels which belong to bad blobs
idx = vertcat(measurements(~allowableCentroidIndexes).PixelIdxList);
cleanedImage = filledImage;
cleanedImage(idx) = false; % clear bad blobs
imshow([filledImage cleanedImage]) % i'm just going to concatenate the images for viewing
Evaluating centroid offsets might not be the only way to judge distances. Depending on what's important in-context, you might be able to evaluate the average (or minimum) distance of each blob from the perimeter of the main blob. You might start by isolating the main blob and calculating the distance map using bwdist(). The rest is a matter of using the distance map as a second input to regionprops(), retrieving properties like 'meanintensity' or 'minintensity'.
filledImage = imread('image.png')>128;
% get distance map
mainblob = bwareafilt(filledImage,1);
D = bwdist(mainblob);
% evaluate minimum distances
measurements = regionprops(filledImage,D,'pixelidxlist','minintensity');
badblobidx = [measurements.MinIntensity] > 40 % some threshold value
% this is a list of all pixels which belong to bad blobs
idx = vertcat(measurements(badblobidx).PixelIdxList);
cleanedImage = filledImage;
cleanedImage(idx) = false; % clear bad blobs
imshow([filledImage cleanedImage]) % i'm just going to concatenate the images for viewing

Image Analyst
Image Analyst il 31 Mag 2022
Please explain the context/use case. Why do you want to delete those? How is close determined and why? Centroid separation? Distance between closest boundary points? Hausdorf distance?
To use ismember you need to label the blobs
labeledImage = bwlabel(filledImage);
measurements = regionprops(labeledImage, 'Centroid', 'Area');
allCentroids = vertcat(measurements.Centroid);
% Get separations between all the centorids
centroidSeparations = pdist2(allCentroids, allCentroids);
% Find out which blobs are close to each other.
farAway = centroidSeparations < someDistance & centroidSeparations > 0;
[r, c] = find(farAway);
labelsToKeep = r;
% Then to remove some labeled blob...
remainingBlobs = ismember(labeledImage, [labelsToKeep]);
Please attach the original images (gray scale and binary) if you need more help.

Categorie

Scopri di più su Polynomials in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by