How can I get the difference between two region triangle wise?
1 view (last 30 days)
Show older comments
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288490/image.bmp)
9 Comments
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?
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Image Processing and Computer Vision in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!