How can I dilate different parts of an image with different structuring elements?

10 vues (au cours des 30 derniers jours)
Jacob Kerr
Jacob Kerr le 7 Fév 2021
Commenté : DGM le 21 Oct 2023
I have got a binary image which is almost all empty but has a few pixles semi-randomly spaced through out the image. I want to then dilate these pixles so that they are trapiziums at different angles.
I have formed a trapizium structuring element and I am able to rotate this structuring element using imrotate() so that the pixles are dilated in a different direction as it were. However I am stuck when it comes to trying to dilate pixles in different angles to one and other. Is there a way to do this with imdilate() or would there be a better way round this issue... potentially not using the dilate function?
these are the input
this is the output which I am getting. Here all the trapiziums are at angle of 30 but I would ideally like them to be at different angles to one and other.
Later down the line I may also try to make the trapiziums diffent sizes to one and other. My intuition is the image dilation may not be the way to go but I am confused how to approach it otherwise... any guidance would be appreciated !

Réponses (2)

VINAYAK LUHA
VINAYAK LUHA le 13 Oct 2023
Modifié(e) : VINAYAK LUHA le 21 Oct 2023
Hi Jacob,
I understand that you want to dilate different parts of an image with trapezoid structuring elements of different size and orientation in MATLAB.
Here is a solution to achieve the above use case which involves overlaying a different trapezoid mask on each of the ROI defined by centroid of the white pixeled region:
  1. Use the "regionprops" function to identify the centroids of the connected white pixel regions in the image.
  2. For each centroid coordinate,
  • Define a region of interest (ROI) of suitable size around it.
  • Generate a mask of the same size as the ROI, containing a trapezium of random size and orientation.
  • Overlay the generated mask on the ROI.
Further, you can refer to the following articles for more details:
I hope you find the provided solution useful and this helps you to dilate different parts of an image with different structuring elements, as desired.
Regards,
Vinayak Luha
  1 commentaire
DGM
DGM le 21 Oct 2023
Vague text-only descriptions of a proposed process aren't very helpful when they gloss over the most complicated parts of the process. Using regionprops() and combining masks are the most basic parts. Creating a mask based on an arbitrarily-transformed shape is not nearly as obvious.
Here is one simple example, though this could be approached using either set of IPT ROI tools. This example uses the same source image as I used in my answer.
% a binary image (already logical class)
inmask = imread('scattered.png');
% define some polygon vertices
% this is a triangle with its acute corner at the origin
px = [0 40 40];
py = [0 0 -30];
sz = size(inmask,1:2);
S = regionprops(inmask,'centroid');
outmask = false(sz);
theta = linspace(0,-90,numel(S)); % used for rotating the strel
for k = 1:numel(S)
% rotate poly coordinates about its own origin
R = [cosd(theta(k)) -sind(theta(k)); sind(theta(k)) cosd(theta(k))];
polyxy = (R*[px(:) py(:)].').';
% offset poly coordinates so that the polygon's origin
% coincides with the centroid of this blob
polyxy = polyxy + S(k).Centroid;
% create a mask containing only this object
thismask = poly2mask(polyxy(:,1),polyxy(:,2),sz(1),sz(2));
% merge the result into the output image
outmask = outmask | thismask;
end
imshow(outmask,'border','tight')
In this example, as each blob index increases, the polygon is rotated from 0 to 90 degrees. Note that the angles are negative, since the image coordinates begin in the NW corner (the y-axis is flipped). Note that unlike the dilation example, the resulting blob shapes are independent of the original blob shapes. The blob origin (the acute corner of the triangle) is simply placed coincident with the centroid of each blob in the original image.

Connectez-vous pour commenter.


DGM
DGM le 21 Oct 2023
You can do it by dilation if you want, but you'd have to process each object independently. Consider the following image.
This image contains a number of single-pixel objects, and one larger circle object.
% a binary image (already logical class)
inmask = imread('scattered.png');
% i decided to use bwconncomp()
% you could also just use bwlabel()
CC = bwconncomp(inmask);
outmask = false(CC.ImageSize);
thismask = false(CC.ImageSize);
theta = linspace(0,90,CC.NumObjects); % used for rotating the strel
for k = 1:CC.NumObjects
% create a mask containing only this object
thismask(CC.PixelIdxList{k}) = true;
% create whatever custom strel you want for this object
se = strel('line',100,theta(k));
% use the strel in whatever way you want for this object
thismask = imdilate(thismask,se);
% merge the result into the output image
outmask = outmask | thismask;
% blank the temporary mask
thismask(:) = false;
end
imshow(outmask,'border','tight')
In this example, as each blob index increases, the strel is rotated from 0 to 90 degrees. Since the operation is dilation, the resulting blob shapes are dependent on the shape of the original blobs.

Community Treasure Hunt

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

Start Hunting!

Translated by