1

I have some damaged line segments in a binary image and I need to fix them (make them straight and at their original thick). In order to do that I have to find the middle points of the segment, so when I check the neighborhood to find the thickness of the lines I'll be able to find where the pixel stops being 1 and becomes 0.

3 Answers3

2

Assuming your damaged line segments are straight, you can use regionprops in MATLAB to find the center of each bounding box. Because if a segment is straight, its is always the diagonal line of the bounding box, thus the center of the box is also the center of the semgent.

DXM
  • 1,249
  • 1
  • 14
  • 22
  • unfortunately the segments are not straight. You can see in this is image what i mean [link] (http://imageshack.us/a/img825/1173/45157442.png) – Aris Malidis Oct 09 '12 at 06:42
  • Here's my thoughts, 1. find the approximate position of the two endpoints of the segment, using regionprops. 2. do a Canny edge detection, so you get the outline of the segment. 3. Using Dijkstra's algorithm to find path from one endpoint to the other. 4. find the middle point of the path. – DXM Oct 17 '12 at 20:47
1

Let's call the points A and B to reduce ambiguity, A(Xa, Ya) and B(Xb, Yb)

Let C be the middle point.

C(Xc, Yc)
Xc = (Xa + Xb) / 2
Yc = (Ya + Yb) / 2

We have four interesting numbers, two for the X coordinates and two for the Y coordinates.

Xmin = floor(Xc)
Xmax = ceil(Xc)
Ymin = floor(Yc)
Ymax = ceil(Yc)

The X coordinate of your middle point is either Xmin or Xmax, the Y coordinate of your middle point is either Ymin or Ymax.

So we have four potential points: (Xmin, Ymin), (Xmin, Ymax), (Xmax, Ymin), (Xmax, Ymax).

So, finally, we must decide which point is nearest to C.

Distance from P(Xp, Yp) to C(Xc, Yc) is:

sqrt(sqr(Xp - Xc) + sqr(Yp - Yc))

Calculate the four distance from the four points to C, choose the minimum and that will be the best possible middle point.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
1

Suppose

A = [xa ya];
B = [xb yb];

then

C = round( mean([A;B]) );

Matlab's round rounds numbers towards their nearest integer, so this minimizes the (city-block) distance from the analytical center (mean([A;B])) to the nearest pixel.

If you want to keep sub-pixel precision (which is actually advisable for most calculations until an explicit map from a result to pixel indices is required), just drop the round and use only the mean part.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96