---
title: "ImageLines"
language: "en"
type: "Symbol"
summary: "ImageLines[image] finds line segments in image and returns the coordinates of their endpoints. ImageLines[image, t] uses the threshold t for selecting image lines. ImageLines[image, t, d] uses the parameter d to control the distinctness of the detected lines. ImageLines[video, ...] finds lines in frames of video."
keywords: 
- line detection
- hough
- edge detection
- ransac
- line segments
- houghlines
- houghpeaks
canonical_url: "https://reference.wolfram.com/language/ref/ImageLines.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Feature Detection"
    link: "https://reference.wolfram.com/language/guide/FeatureDetection.en.md"
  - 
    title: "Image Processing & Analysis"
    link: "https://reference.wolfram.com/language/guide/ImageProcessing.en.md"
  - 
    title: "Computer Vision"
    link: "https://reference.wolfram.com/language/guide/ComputerVision.en.md"
  - 
    title: "Video Analysis"
    link: "https://reference.wolfram.com/language/guide/VideoAnalysis.en.md"
  - 
    title: "Video Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md"
  - 
    title: "Image Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/ImageComputation-UpdateHistory.en.md"
  - 
    title: "Computational Photography"
    link: "https://reference.wolfram.com/language/guide/ComputationalPhotography.en.md"
related_functions: 
  - 
    title: "Radon"
    link: "https://reference.wolfram.com/language/ref/Radon.en.md"
  - 
    title: "FindImageShapes"
    link: "https://reference.wolfram.com/language/ref/FindImageShapes.en.md"
  - 
    title: "EdgeDetect"
    link: "https://reference.wolfram.com/language/ref/EdgeDetect.en.md"
  - 
    title: "GradientFilter"
    link: "https://reference.wolfram.com/language/ref/GradientFilter.en.md"
  - 
    title: "ImageKeypoints"
    link: "https://reference.wolfram.com/language/ref/ImageKeypoints.en.md"
  - 
    title: "ImageCorners"
    link: "https://reference.wolfram.com/language/ref/ImageCorners.en.md"
  - 
    title: "HighlightImage"
    link: "https://reference.wolfram.com/language/ref/HighlightImage.en.md"
  - 
    title: "HighlightVideo"
    link: "https://reference.wolfram.com/language/ref/HighlightVideo.en.md"
---
# ImageLines

ImageLines[image] finds line segments in image and returns the coordinates of their endpoints.

ImageLines[image, t] uses the threshold t for selecting image lines.

ImageLines[image, t, d] uses the parameter d to control the distinctness of the detected lines.

ImageLines[video, …] finds lines in frames of video.

## Details and Options

* ``ImageLines`` returns a list of line segments in the form ``Line[{p1, p2}]``, where each ``pi = {xi, yi}`` is expressed in the standard image coordinate system.

* ``ImageLines[image, t]`` finds lines in the ``image`` whose normalized strength is larger than the specified threshold ``t``.

* ``ImageLines`` sorts the result based on the normalized strength.

* In ``ImageLines[image, t, d]``, the parameter ``d`` controls how close lines are suppressed. If the value is set to zero, all detected lines are returned. With ``d`` set to ``1``, only the strongest line may be returned.

* The following options can be given:

|              |         |                                      |
| ------------ | ------- | ------------------------------------ |
| MaxFeatures  | All     | maximum number of features to return |
| Method       | "Hough" | method to detect lines               |

* With a setting ``MaxFeatures -> n``, at most ``n`` lines with largest normalized strength are returned.

* Possible line detection methods are:

|          |                                          |
| -------- | ---------------------------------------- |
| "Hough"  | lines based on Hough transform (default) |
| "RANSAC" | lines using the RANSAC algorithm         |

* With ``Method -> "Hough"``, lines are detected by iteratively selecting the strongest peaks in the Hough transform. Using the distinctness parameter, peaks that are within a rectangular range from the already selected peaks are excluded from the set of line candidates.

* With ``Method -> "RANSAC"``, lines are detected using random sampling. For each sampling, pixels that are within a distance specified by the distinctness parameter ``d`` are used for computing the strength of the line. The pixels on the selected line are not used in the following iterations.

* By default, ``ImageLines`` returns lines that span from border to border. With a setting ``Method -> {"Segmented" -> True}``, detected lines may be divided into smaller line segments.

---

## Examples (14)

### Basic Examples (1)

Detect and visualize straight lines in an image:

```wl
In[1]:=
i = [image];
lines = ImageLines[i, 1 / 3]

Out[1]= {Line[{{0., 172.014}, {360., 76.6467}}], Line[{{0., 106.159}, {360., 168.092}}], Line[{{0., 118.373}, {360., 180.306}}], Line[{{0., 123.596}, {320.562, 0.}}], Line[{{0., 90.4493}, {360., 157.446}}], Line[{{0., 49.5606}, {360., 126.765}}], Line[{{0., 72.8608}, {360., 142.399}}], Line[{{0., 130.942}, {360., 187.834}}], Line[{{0., 198.926}, {360., 126.839}}], Line[{{0., 24.3074}, {360., 106.659}}]}
```

Highlight detected lines:

```wl
In[2]:= HighlightImage[i, lines]

Out[2]= [image]
```

### Scope (2)

Specify the distinctness parameter:

```wl
In[1]:= ImageLines[[image], .2, .2]

Out[1]= {Line[{{0., 18.3048}, {201., 18.3048}}], Line[{{0., 118.107}, {118.82, 0.}}], Line[{{101.363, 120.}, {100.641, 0.}}]}
```

---

Detect the line going through the foreground pixels:

```wl
In[1]:= ImageLines[[image], .05]

Out[1]= {Line[{{0.492551, 40.}, {39.5074, 0.}}]}
```

### Options (3)

#### MaxFeatures (1)

Specify the maximum number of lines to be to be detected:

```wl
In[1]:=
img = [image];
lines = ImageLines[EdgeDetect[img], MaxFeatures -> 5]

Out[1]= {Line[{{71.4974, 225.}, {262.715, 0.}}], Line[{{232.978, 225.}, {13.8344, 0.}}], Line[{{51.8704, 225.}, {295.412, 0.}}], Line[{{0., 134.307}, {300., 138.827}}], Line[{{219.337, 225.}, {33.8854, 0.}}]}

In[2]:= HighlightImage[img, {Orange, lines}]

Out[2]= [image]
```

#### Method (2)

By default, lines are detected using ``Method -> "Hough"`` :

```wl
In[1]:=
img = [image];
lines = ImageLines[img, .23, Method -> "Hough"];
HighlightImage[img, lines]

Out[1]= [image]
```

Use a random sampling method:

```wl
In[2]:=
lines = ImageLines[img, .23, Method -> "RANSAC"];
HighlightImage[img, lines]

Out[2]= [image]
```

---

Detect segmented lines in a grayscale image:

```wl
In[1]:=
img = [image];
lines = ImageLines[img, .2, Method -> {"Hough", "Segmented" -> True}];
HighlightImage[img, lines, ImageSize -> 240]

Out[1]= [image]
```

Segmented lines using the random sampling method:

```wl
In[2]:= lines = ImageLines[img, .23, Method -> {"RANSAC", "Segmented" -> True}];HighlightImage[img, lines, ImageSize -> 240]

Out[2]= [image]
```

### Applications (6)

Detect and visualize straight trajectories in a bubble chamber image:

```wl
In[1]:=
img = [image];
lines = ImageLines[img];
HighlightImage[img, lines]

Out[1]= [image]
```

---

Detect segments on a color image:

```wl
In[1]:=
img = [image];
lines = ImageLines[img, .53, .03, Method -> {"Segmented" -> True}];
HighlightImage[img, {Black, lines}, ImageSize -> 320]

Out[1]= [image]
```

---

Visualize vanishing points:

```wl
In[1]:=
img = [image];
lines = ImageLines[EdgeDetect[img], .28, .06];
HighlightImage[img, lines]

Out[1]= [image]
```

---

Detect segments on a gradient magnitude map:

```wl
In[1]:=
img = [image];
lines = ImageLines[ImageAdjust@GradientFilter[img, 3, Method -> {"NonMaxSuppression" -> True}], .09, Method -> {"Segmented" -> True}];
HighlightImage[img, {Yellow, lines}]

Out[1]= [image]
```

---

Find wide lines using edge detection:

```wl
In[1]:=
img = [image];
lines = ImageLines[EdgeDetect[img, 13], .18];
HighlightImage[img, lines]

Out[1]= [image]
```

---

Straighten the tower of Pisa:

```wl
In[1]:= image = [image];
```

Compute the gradient of the image to highlight edges:

```wl
In[2]:= (g = GradientFilter[image, 2])//ImageAdjust

Out[2]= [image]
```

Find the most significant straight lines in the gradient image:

```wl
In[3]:= HighlightImage[image, lines = ImageLines[g, MaxFeatures -> 10]]

Out[3]= [image]
```

FInd the angles corresponding to each line:

```wl
In[4]:= angles = Mod[ArcTan@@@Flatten[Differences@@@lines, 1], π]

Out[4]= {9.473903143468003`*^-17, 0.0866103, 0.0157473, 3.07073, 1.55899, 0.070863, 3.02349, 3.09435, 1.50387, 1.51174}
```

Compute the average angle of the vertical lines:

```wl
In[5]:= θ = Mean@Select[angles, Abs[# - π / 2] < π / 4&]

Out[5]= 1.52487
```

Rotate the image by $\frac{\pi }{2}-\theta$ to make almost-vertical lines vertical:

```wl
In[6]:= ImageRotate[image, π / 2 - θ]

Out[6]= [image]
```

### Possible Issues (2)

Use the distinctness parameter to prevent detecting duplicated lines:

```wl
In[1]:= Length@ImageLines[[image], .1, #]& /@ {0, .1}

Out[1]= {72, 3}
```

---

Thin lines in binary images might not be correctly detected:

```wl
In[1]:= i = [image];

In[2]:=
lines = ImageLines[i, MaxFeatures -> 3, Method -> {"Segmented" -> True}];
HighlightImage[i, lines]

Out[2]= [image]
```

Blurring the image typically improves line detection:

```wl
In[3]:=
lines = ImageLines[Blur[i], MaxFeatures -> 3, Method -> {"Segmented" -> True}];
HighlightImage[i, lines]

Out[3]= [image]
```

Rescaling the image to a larger size might also help in some cases:

```wl
In[4]:= i2 = ImageResize[i, Scaled[2]];

In[5]:=
lines = ImageLines[i2, MaxFeatures -> 3, Method -> {"Segmented" -> True}];
HighlightImage[i2, lines]

Out[5]= [image]
```

## See Also

* [`Radon`](https://reference.wolfram.com/language/ref/Radon.en.md)
* [`FindImageShapes`](https://reference.wolfram.com/language/ref/FindImageShapes.en.md)
* [`EdgeDetect`](https://reference.wolfram.com/language/ref/EdgeDetect.en.md)
* [`GradientFilter`](https://reference.wolfram.com/language/ref/GradientFilter.en.md)
* [`ImageKeypoints`](https://reference.wolfram.com/language/ref/ImageKeypoints.en.md)
* [`ImageCorners`](https://reference.wolfram.com/language/ref/ImageCorners.en.md)
* [`HighlightImage`](https://reference.wolfram.com/language/ref/HighlightImage.en.md)
* [`HighlightVideo`](https://reference.wolfram.com/language/ref/HighlightVideo.en.md)

## Related Guides

* [Feature Detection](https://reference.wolfram.com/language/guide/FeatureDetection.en.md)
* [Image Processing & Analysis](https://reference.wolfram.com/language/guide/ImageProcessing.en.md)
* [Computer Vision](https://reference.wolfram.com/language/guide/ComputerVision.en.md)
* [Video Analysis](https://reference.wolfram.com/language/guide/VideoAnalysis.en.md)
* [Video Computation: Update History](https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md)
* [Image Computation: Update History](https://reference.wolfram.com/language/guide/ImageComputation-UpdateHistory.en.md)
* [Computational Photography](https://reference.wolfram.com/language/guide/ComputationalPhotography.en.md)

## History

* [Introduced in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) \| [Updated in 2012 (9.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn90.en.md) ▪ [2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md) ▪ [2018 (11.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn113.en.md) ▪ [2025 (14.2)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn142.en.md)