필터 지우기
필터 지우기

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

조회 수: 8 (최근 30일)
Jacob Kerr
Jacob Kerr 2021년 2월 7일
댓글: DGM 2023년 10월 21일
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 !

답변 (2개)

VINAYAK LUHA
VINAYAK LUHA 2023년 10월 13일
편집: VINAYAK LUHA 2023년 10월 21일
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
DGM
DGM 2023년 10월 21일
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.

댓글을 달려면 로그인하십시오.


DGM
DGM 2023년 10월 21일
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