create mask (line) based on conditional (mask)

would like to create a mask that hightlights image pixels above or below a threshold
from each element in the mask, would like to draw a circle around it (anotehr mask) so as to make it stand out in the image
not sure how to implement the circular mask, seems to need XY coordinate (wont take maska)
function Findnoise(app, event)
highedit = str2num(app.highEditField.Value);
lowedit = str2num(app.lowEditField.Value);
maska= app.a >=highedit & app.a <=lowedit;
hcirc = drawcircle(app.UIAxes, 'Center',maska,'Radius',10,'StripeColor','red'); <- problematic line
% mask1 = hcirc.createMask;
% imagesc(app.a,'Parent',app.UIAxes);
end

 Accepted Answer

You could do it using ROI objects, but that would be terribly slow and prone to other problems. Here's an example:
% dummy inputs
app.a = 0.06*randn(500,500);
imshow(app.a) % we need to display it to use the ROI tools this way.
app.highEditField.Value = '0.2';
app.lowEditField.Value = '0.8';
app.UIAxes = gca;
% create a logical mask
highedit = str2num(app.highEditField.Value);
lowedit = str2num(app.lowEditField.Value);
maska = app.a >= highedit & app.a <= lowedit;
% convert the mask to row,col subs
[y,x] = find(maska);
% draw a bazillion circle objects
maskc = false(size(maska));
for k = 1:nnz(maska)
hcirc = drawcircle(app.UIAxes, 'Center',[x(k) y(k)],'Radius',10,'StripeColor','red');
maskc = maskc | hcirc.createMask;
end
% show it
imshow(maskc)
It would make more sense to simply dilate the mask.
% dummy inputs
app.a = 0.06*randn(500,500);
app.highEditField.Value = '0.2';
app.lowEditField.Value = '0.8';
app.UIAxes = gca;
% create a logical mask
highedit = str2num(app.highEditField.Value);
lowedit = str2num(app.lowEditField.Value);
maska = app.a >= highedit & app.a <= lowedit;
% dilate the mask
maskc = imdilate(maska,strel('disk',10,0));
% show it
imshow(maskc)
The question seems to suggest that you were trying to create open circles, but I don't know what purpose that would serve as a mask. Either way, you can still do that.
% ... same setup as before
linew = 2;
% dilate the mask (hollow)
maskc = imdilate(maska,strel('disk',10,0)) ...
& ~imdilate(maska,strel('disk',10-linew,0));
% show it
imshow(maskc)
You could obviously then combine them if you wanted. The single pixels don't show up here on the forum, but they're there. As a visualization, this might be useful, but as a mask, I don't see a clear technical purpose.
% incorporate both masks?
maskcomp = maska | maskc;
% show it
imshow(maskcomp)

5 Comments

ok, will give your code a shot.
yes, speed in the big issue
iterative loops are just taking forever in the application
ie
function Findnoise(app, event)
highedit = str2num(app.highEditField.Value);
lowedit = str2num(app.lowEditField.Value);
for n = 1:size(app.a,1)
for m = 1:size(app.a, 2)
if (app.a(n,m) > highedit) %| (app.a(n,m) > lowedit)
hcirc = drawcircle(app.UIAxes, 'Center',[n,m],'Radius',10,'StripeColor','red');
end
end
end
end
end
I would strongly suggest not doing it with ROI tools at all.
JM
JM on 20 Aug 2024
Moved: Walter Roberson on 20 Aug 2024
the goal is to make background artificats standout. Often its 1 pixel, sometimes a cluster of 10. Sometimes positive values from background, sometimes negative.
It does not follow noise statics that can be filtered. Really need the user to be able to select an appropiate level of threshold.
Im struggling to get anytihng to show on the image that pops up
any help with getting something to show and on the application
function Findnoise(app, event)
highedit = 3.5 ; %str2num(app.highEditField.Value);
lowedit = 2.4 %str2num(app.lowEditField.Value);
maska= app.a >=highedit & app.a <=lowedit;
maskc = imdilate(maska,strel('disk',1,0));
imagesc(maskc,'Parent',app.UIAxes)
figure, imagesc(maskc)
maskcomp = maska | maskc;
imagesc(maskcomp)
% for n = 1:size(app.a,1)
% for m = 1:size(app.a, 2)
% if (app.a(n,m) > highedit) %| (app.a(n,m) > lowedit)
% hcirc = drawcircle(app.UIAxes, 'Center',[n,m],'Radius',10,'StripeColor','red');
% end
% end
% end
%hcirc = drawcircle(app.UIAxes, 'Center',[cpx,cpy],'Radius',inch*590,'StripeColor','red');
% hcirc = drawcircle(app.UIAxes, 'Center',[cpx,cpy],'Radius',10,'StripeColor','red');
% mask1 = hcirc.createMask;
% imagesc(app.a,'Parent',app.UIAxes);
end
end
There's no reason the threshold values or other parameters can't be controlled via some GUI, but I'm going to write conceptual demos that don't need a GUI. It's simpler that way, and it can run on the forum.
I don't know what your input image is, so I can only assume that if maskcomp is empty, then it's because maska is empty (i.e. nnz(maska) is zero). The strel you're using also only has a 1px radius, so it's not going to dilate very much. If your image is very large, or you're zoomed way out, small features simply may not display.

Sign in to comment.

More Answers (1)

Based on your question and the answer, I don't understand. I don't know why you're messing with circles when regions in a threshold range will, in general, not be perfectly circular.
If you simply want to outline pixels above, or in the range of, a threshold, simply threshold and use bwboundaries followed by visboundaries.
If you want to make the pixels solid by showing an overlay above those pixels, use imoverlay.
If you want to tint different/separate regions with different colors and opacities, then use labeloverlay.
If you want a circle at the centroid of the blob just to "note" where the blob is in a visual sense, then you can use
hold on;
plot(x, y, 'r.', 'MarkerSize', 30);
to place a red dot at the centroid. To find the centroids you can use regionprops
props = regionprops(mask, 'Centroid');
xy = vertcat(props.Centroid);
x = xy(:, 1);
y = xy(:, 2);
You could also use the centroids to place a number at each centroid using the text function. This essentially counts/labels each blob with a unique number, rather than just doing a dot. Of course you could combine the things, like have the outline around each blob with a dot at the center and a number next to the dot.
If you want to place an ellipse over each blob (that has the same area as the blob and same orientation), see Steve's blog:

Products

Release

R2022a

Tags

Asked:

JM
on 20 Aug 2024

Commented:

DGM
on 22 Aug 2024

Community Treasure Hunt

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

Start Hunting!