Clear Filters
Clear Filters

How can I get the difference between two region triangle wise?

1 view (last 30 days)
I have found the boundary of the blob and the bounding box.Then I have added the centroid to the vertices of the box.Now I am trying to get the differences between the boundary and the box for each triangle.How can I do that?
I have added my code with the output:
  9 Comments
Image Analyst
Image Analyst on 30 Apr 2020
Edited: Image Analyst on 30 Apr 2020
I think you did give a screenshot. Just click and drag the above image in your browser and you'll see there is a white frame around it for some reason. Is that in your program? If so use imclearborder. Also fill holes.
mask = imclearborder(mask);
mask = imfill(mask, 'holes');
Each point on your yellow bounding box will have a distance to every point on your blob boundary. Do you want the closest? So that for each point on the bounding box, find the distance to the nearest boundary point of the blob? So now you'll have a distance for every point on the boundary. So then do you want the average of all the closest distances in the triangle? So you'll have one average distance for each side of the bounding box?
joynob ahmed
joynob ahmed on 1 May 2020
Edited: joynob ahmed on 1 May 2020
#Rik
I am using matlab2019a. I don't see any white border.
#Image analyst
I didn't. It was an image which I got after performing segmentation process on actual image.
Not the distance from yellow bounding box but from the image edge to blob boundary is needed.I need the the nearest boundary point to the image edge in the direction which are directed by the triangle.
Please see this image.

Sign in to comment.

Accepted Answer

Rik
Rik on 30 Apr 2020
The code below is most of the way there. It only needs to have an encoding for the distance along the edge, so the line doesn't make a lot of sense yet.
%read binary mask
mask = imread('image.bmp');
mask=mask(41:616,128:894,1)>128;
%fill all holes:
%flip mask and select everything that is not the outer area
mask= bwlabel(~mask,4) ~= 1;
%find the edge pixels with something like imerode
SE=true(3*ones(1,ndims(mask)));%structuring element
edge= mask & ~( mask & convn(mask,SE,'same')==sum(SE(:)) );
%find the bounding box with 1 px margin
colind_first=find(sum(mask,1),1,'first')-1;
colind_last =find(sum(mask,1),1,'last')+1;
rowind_first=find(sum(mask,2),1,'first')-1;
rowind_last =find(sum(mask,2),1,'last')+1;
box=false(size(mask));
box([rowind_first rowind_last], colind_first:colind_last )=true;
box( rowind_first:rowind_last ,[colind_first colind_last])=true;
%add the diagonal lines to the box
x=false(size(mask));
p=polyfit([rowind_first rowind_last],[colind_first colind_last],1);
row=rowind_first:rowind_last;
col=round(polyval(p,row));
x(sub2ind(size(x),row,col))=true;
%add other diagonal to x
p=polyfit([rowind_first rowind_last],[colind_last colind_first],1);
col=round(polyval(p,row));
x(sub2ind(size(x),row,col))=true;
dist2xbox=bwdist(box | x);
distance=dist2xbox(edge);
figure(1),clf(1)
subplot(1,2,1)
imshow(edge | box | x)
subplot(1,2,2)
plot(distance)
  14 Comments
joynob ahmed
joynob ahmed on 18 Jun 2020
Rik
The problem was in this line:
colind_last =find(sum(mask,1),1,'last')+1;
This +1 made this value 768 which is greater than size(x) in my main code. Will it be a problem if I remove the +1 and -1 portion from the code?
Rik
Rik on 18 Jun 2020
No, but you will lose the margin, so if you remove it, the box will overlap with the outer margin of your object. If your image doesn't have a margin, the method to fill all holes might have failed as well.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!