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

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 commentaires

So you want to find the fraction of black within each yellow triangle? You will have to attach the image as well, otherwise we can't reproduce your results in the first place.
Actually I want to find out the border line function.For this I have to calculate the distance between the border of the lesion and the image edge for each part of the borderline(which are created by these triangles). Then I have to subtract the gap between the functions which result from the difference in distance to the edge.Here is my original picture and the funtion I want.
Is that second image really the original image? It doesn't really look like it. And if you just want the distance to the edge, why not use bwdist?
But the first step is to find all border pixels. So you have some cleaning to do before you can easily use native Matlab functions.
I am sorry for giving the wrong image. I have given here the right one.
But I have to find out distance according to the triangular area.How can I do this?
I can't understand what have you meant by cleaning procedure. Please explain this.
You should provide the binary image, not a screenshot of the binary image, nor the original color image. The color image is fine if you also provide the code to convert it to the binary image.
What I meant by cleaning up is exactly what you have done in the image you posted in your question: make sure you only have a single binary object, without any holes in it.
Once you have the binary image, you can crop it to the bounding box. Then you can use bwdist to find the distance to the nearest line of your bounding box. That means you also need to include the diagonals for this part.
But I'll show what I mean in an answer when you have posted appropriate input data. You can also attach a mat file if that is easier for you.
joynob ahmed
joynob ahmed le 30 Avr 2020
Modifié(e) : joynob ahmed le 30 Avr 2020
I didn't give the screenshot. The binary image was the second image. And in question I have attached the mat file of which code I have applied on the binary image and got the output shown in the question.
I am adding the binary image again.
When I run your code I don't get your image. Which release are you using? And how do these white borders around the black appear?
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 le 1 Mai 2020
Modifié(e) : joynob ahmed le 1 Mai 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.

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

Thanks for your answer. But I wanted the distance triangle wise. In my code, I have determined the bounding box so that I can find the vertices.Then I have connected the centre of mass of the lesion to the vertices which divided the lesion into four triangles not the triangles made by diagonals.And according to these part I wanted the distance from lesion border to the image edge.
You can see the image in the question that my lesion is not divided into four equal parts.
I have added the screenshot of what I wanted to do.
Can you help me with that?
My code found the edge for you, as well as the linear fit for those diagonals.
What you need to do is devide the edge into quadrants and find that distance you are looking for. Since that is the distance between the x or y position and a horizontal or vertical line that should not be very difficult.
The dificult thing would be to label your edge with indices. I did write some code that was guaranteed to terminate, but it would have taken ages, so that part you will have to do yourself.
Okay. Thank you.
What is the plan if the boundary criss crosses a diagonal several times?
I didn't need the diagonal. I added the centre of mass with the vertices of box and according to the picture
I added should be the direction at which I needed the distances from boundary to image edge.
Is it possible? If not,then I have to work with the diagonals and the the result won't be so good.
Hi Rik.Would you mind telling me what distance you have measured in the code? Is it between image edge and the lesion border?
As the variable names in distance=dist2xbox(edge); imply: it is the distance from the lesion edge to the bounding box (where the box also contains the crosshairs).
joynob ahmed
joynob ahmed le 30 Mai 2020
Modifié(e) : joynob ahmed le 30 Mai 2020
Your code worked very well when it is used alone. But when I assembled your code to my main code,it causes an error for which I can't found what to do.This error is showing for this line-
edge= mask & ~( mask & convn(mask,SE,'same')==sum(SE(:)) );
The logical indices contain a true value outside of the array bounds
I found that the problem is in the 'sum' command.But what should I use instead of this command?
Again when I used the shifted and rotated image for this in my code-
mask=rotatedImage(41:616,128:894,1)>128;
There is an error saying-
Index in position 1 exceeds array bounds (must not exceed 576)
I am trying as much as I can but if you understand the problem please help me with this.
The first error looks like it is due to a variable with the name sum. Otherwise I don't have an explanation for the error, so you would have to copy the entire error message.
About that second part: I hard-coded these values to remove the white border that insisted wasn't there. So I assumed in your actual code that line would not be necessary. For the example image this line is needed, whether you need it in your full code I don't know.
joynob ahmed
joynob ahmed le 12 Juin 2020
Modifié(e) : joynob ahmed le 16 Juin 2020
#Rik
Thanks you for your answer. Now I have another question. I have filtered and determined the derivative of the border irregularity function to get the local maximums.We know the local maximum is detected when the derivative of the function crosses the zero point and the slope changes sign from + to −. I want to divide the curve in 8 region and count the abrupt cut off in every region so that I can have the final decision.
I have coded upto this:
I=imgaussfilt(distance,20);
figure
plot(I)
j=diff(I);
figure
plot(j)
I found output upto this:
And what I wanted is to point out the local maximums like this and count the abrupt cut off in each region:
I know you helped me a lot and I am very much grateful to you for this.If you can please help me with this.
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?
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by