user1853871
user1853871

Reputation: 249

Matlab, using straight line equation to remove rough edges

I am using the following image which I have segemented :

Before

I need to smooth the line where the image is segmeneted so that it looks like this:

After

I have read that the best way to to do this is to use the straight line equation:

y=mx+c

and find four points along the segmented area. However I am unsure of how I go about finding four points.

Can anyone please guide me?

Thanks

I have code which I have used to decide which side of the image needs to be segmented:

  s= regionprops(BW2, 'BoundingBox');
  BW3 = imcrop(BW2,s.BoundingBox);%crop the image 


  total1=sum(BW3(:,[1:5]));%get the total of the first 5 columns in the image
  totalFirst=sum(total1);
  total2=sum(BW3(:,[end-4:end])); %get the total of the last 5 columns in the image
  totalLast=sum(total2);
  %breast profile classified such that if the sum of the first 5 rows is
  %greater than the last 5 rows then the breast is right orientated else it
  %is left orientated. 
  if totalFirst>totalLast 
    O=1; % o for right
  else 
    O=0; % 1 for left 
  end 

Upvotes: 1

Views: 927

Answers (1)

kol
kol

Reputation: 28708

Assume that the image is right-oriented, as in the example above - left-oriented images can be flipped using fliplr. Assume that pixels of value 0 are black, and pixels of value 1 are white. The simplest algorithm I can imagine is the following:

1. Starting from the upper left corner, find the column index of the pixel in the first row, which is white. Let's denote it by j0.

j0 = 1;
while BW3(1, j0) ~= 1 
  j0 = j0 + 1;
end 

2. Starting again from the upper left corner, find the row index of the pixel in the first column, which is white. Denote it by i0.

i0 = 1;
while BW3(i0, 1) ~= 1 
  i0 = i0 + 1;
end 

3. The coordinates of these two white pixels are (1, j0) and (i0, 1), respectively. The points on the upper left side of the line connecting these two points (equation: j = a * i + b) have to be colored black:

a = (1 - j0) / (i0 - 1);
b = j0 - a;
for i = 1:i0
   for j = 1:round(a * i + b)
      BW3(i, j) = 0;
   end
end

4. Some pixels on the lower right side of this line remain black. We have to color these white. The easiest way is to set the color of a strip of pixels on the lower right side of the line to white. Let's denote the width of this strip by w.

w = 20; % This depends on the resolution and the noise level of the image
for i = 1:round(i0 - w / a)
   for j = max(1, round(a * i + b + 1)):round(a * i + b + w)
      BW3(i, j) = 1; 
   end
end

Notes

In step 1., instead of checking the value of a single pixel on the first row, it's better to compare the mean value of its neighborhood (of size n) to a threshold (t):

n = 10; t = 0.9; % These depend on the resolution and the noise level of the image
c = 0:(n - 1);
j0 = 1;
while mean(mean(BW3(1 + c, j0 + c))) < t 
  j0 = j0 + 1;
end 

Similarly, in step 2.:

i0 = 1;
while mean(mean(BW3(i0 + c, 1 + c))) < t 
  i0 = i0 + 1;
end 

Result

enter image description here

I uploaded the code here.

Upvotes: 1

Related Questions