How to obtain specific located objects in binary image ?

6 vues (au cours des 30 derniers jours)
Lütfi Kadir Çelebi
Lütfi Kadir Çelebi le 29 Avr 2021
Modifié(e) : DGM le 8 Jan 2023
Hello everyone. I have binary mask image, and also I have some coordinates information as nx2 vector that include x and y locations. I want to obtain regions on binary mask where only include specific coordinates and make black all other regions. How can I achieve this ?

Réponses (2)

Matt J
Matt J le 29 Avr 2021
With bwselect().
  2 commentaires
Lütfi Kadir Çelebi
Lütfi Kadir Çelebi le 30 Avr 2021
This function doesnt work for my aim, I didnt understand this function how works exactly
Matt J
Matt J le 30 Avr 2021
Even in spite of the examples in the documentation?

Connectez-vous pour commenter.


DGM
DGM le 8 Jan 2023
There are multiple ways this can be done, but bwselect() is a convenient way, especially when the desired output is the union of all intersected blobs.
% you have some binarized image
inpict = imread('blobs.png');
inmask = rgb2gray(inpict)>100;
% and you have some query points
points = [76 185; 411 280; 310 121; 181 140; 92 297; ...
206 128; 232 396; -58 492; 283 351; 109 166];
imshow(inmask); hold on
plot(points(:,1),points(:,2),'*')
We can see graphically that three blobs are selected. Let's say we don't use bwselect(). How could you find the selected blobs? One could imagine a few ways. Here's one.
%% do it the hard way (one way)
% clean up point list
sz = size(inmask);
npoints = size(points,1);
badpts = any(points<1,2) | points(:,2)>sz(1) | points(:,1)>sz(2);
goodpoints = round(points(~badpts,:));
% get labels of selected blobs
idx = sub2ind(sz(1:2),goodpoints(:,2),goodpoints(:,1));
L = bwlabel(inmask);
selectedlabels = nonzeros(unique(L(idx)));
% find all blobs as a multiframe logical mask
outmask = bsxfun(@eq,L,permute(selectedlabels,[2 3 4 1]));
% collapse frames
outmask = any(outmask,4);
imshow(outmask)
Of course, using bwselect() is a lot easier than that. Just give it the binarized image and X,Y vectors.
%% using IPT bwselect()
outmask1 = bwselect(inmask,points(:,1),points(:,2));
imshow(outmask1)
If the reason to avoid bwselect() is the absence of Image Processing Toolbox, MIMT pickblob() also works, though the syntax differs.
%% using MIMT pickblob()
outmask2 = pickblob(inmask,points,'union');
imshow(outmask2)
Note that pickblob() and bwselect() have overlapping abilites in this particular case, but one is not a replacement for the other. IPT bwselect() allows for interactive point selection and can do spatial coordinate transformation, while MIMT pickblob() is not interactive and strictly works in image coordinates. Though both can generate union masks, pickblob() can also collect individual blob masks and indexing information for the query points.
%% using pickblob() to get individual masks and indexing info for all points
[maskstack lidx fidx] = pickblob(inmask,points);
imshow2(maskstack,'tools') % view multiframe mask (MIMT)
% list of query points, the blob label and mask frame associated with each
[points lidx fidx]
ans =
38 93 0 0
206 140 1 1
155 61 0 0
91 70 0 0
46 149 0 0
103 64 4 2
116 198 0 0
-29 246 0 0
142 176 5 3
55 83 0 0
See also: bwselect(), bwlabel()
  2 commentaires
Image Analyst
Image Analyst le 8 Jan 2023
Rather than using the cryptic bsxfun() and any() you can do
outmask = ismember(L, selectedlabels);
DGM
DGM le 8 Jan 2023
Modifié(e) : DGM le 8 Jan 2023
I suppose that's more succinct for this example. The reason it's that way is simply that it's pasted from the internals of pickblob(). In that context, it's necessary that the mask not be collapsed. It's also necessary that the operations are compatible with older versions, hence the use of bsxfun(). I don't really consider that anyone would seriously use that example directly. The main purpose was to provide emphasis to the simplicity of using tools that are built for the given task.
Beyond the intent inherited from the example's source material, would there be any utility to maintaining the isolated masks? I did give some thought to that possibility. It's part of why I chose this question to even mention pickblob(). The wording of the question is vague, but I don't think it clearly implies that the union is desired.
Similarly, one might ask why I did the expansion on dim 4 instead of dim 3. The answer is simply that the prevailing convention within MIMT is that dim 3 is for color channels, regardless of whether we're dealing with images or masks.
Would there have been a cost advantage to conditionally using ismember() in pickblob() when the union is requested? In newer versions, yes. In older versions no, or at least not with the versions I have and my hardware/environment. Still, the performance of ismember() seems to have improved over the years.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by