Selecting specific region around the known indices of a matrix.

Hi,
I have a path around which I want to select 3 points above and 3 points below to acheive something similar to the picture. Basically I want to select the points that lie within 3 radius points. I have attached the MAT file which includes the indices locations around which I want to select points. It also has the matrix from which the figure can be ploted. Orginal matrix has the same dimensions. untitled.png

 Accepted Answer

You can dilate the image with a disk kernel and then take the difference to get the indexes:
A = load('matlab.mat');
h = fspecial('disk',3 ); % Disk element for dilatation
ADilatated = imdilate(A.crack,h); %
PointsAround = ADilatated-A.crack; % Difference to get the new points
IndexAroud = find(PointsAround~=0); % This is the array with the indexes around it
figure
subplot(1,3,1)
imshow(A.crack)
title('Original Image')
subplot(1,3,2)
imshow(ADilatated)
title('Dilated Image')
subplot(1,3,3)
imshow(PointsAround)
title('Points in a 3-pixel radius')
In case you don't want a disk (radius) but rather a horizontal/vertical line you can just change the kernel, some examples can be founded here https://de.mathworks.com/help/images/ref/imdilate.html

7 Comments

My query is related to how to get something similar to what I shared in the image using the indices that I shared in other variable in .mat file. Eventually, I would need to use the shape around the region (removing this shape from rest of the matrix) that I shared in the next part of the code.
Right now I get this region (figure below) that I want to remove and I do not want to change the radius more than 3 points because that effects my end results. I have the exact location of this curved path in the indices (mat file) but am unable to come up with a way to tackle this.
Annotation 2019-11-03 181809.png
No, it solves the problem.
Thank you.
You mean to get something similar to the white line you showed based in the index you got? If so, it is basically the same thing:
A = load('matlab.mat');
NewImage = zeros(size(A.crack));
NewImage(A.indices) = 1; % Transfer your indicies to the image
h = fspecial('disk',3 ); % Disk element for dilatation
NewImageDilated = imdilate(NewImage,h); % Dilate your indicies
figure
subplot(1,2,1)
imshow(NewImage)
title('Line Image')
subplot(1,2,2)
imshow(NewImageDilated)
title('Dilated Image')
Untitled.png
Is this what you wanted?
Yeah this is what I was looking for and implemented that too after your first comment. I am facing another issue now. I am getting extra area above. Highlighted in red in the attached picture. Any recommendations?
Annotation 2019-11-03 202811.png
You can either reduce the overal radius of your kernel or modify it in a way that helps you. If you look at h, it is just a numerical matrix:
h = fspecial('disk',3 )
h =
0 0.0003 0.0110 0.0172 0.0110 0.0003 0
0.0003 0.0245 0.0354 0.0354 0.0354 0.0245 0.0003
0.0110 0.0354 0.0354 0.0354 0.0354 0.0354 0.0110
0.0172 0.0354 0.0354 0.0354 0.0354 0.0354 0.0172
0.0110 0.0354 0.0354 0.0354 0.0354 0.0354 0.0110
0.0003 0.0245 0.0354 0.0354 0.0354 0.0245 0.0003
0 0.0003 0.0110 0.0172 0.0110 0.0003 0
And this you can change, for example, as to remove only some of the pixels in the upside:
h(1:2,:) = 0;
Using this modified h you will get less pixels from above and by fine tuning the parameters and modifications you may be able to reduce your problem.
Is it possible to give different h value from upper and lower region for disk? I mean taking a different value like [4,3] in fspecial ?
Not in fspecial as far as I know, but you can achieve a similar effect using a radius 4 and then setting the first row from h to zero, like the example I gave you in the last comment. If I were to convert my example with your notation what I did would be a [3,1].

Sign in to comment.

More Answers (0)

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!