Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

Hough-Transformation

Hough transformation for circle and line detection

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

# Hough-Transformation

Hough transformation for **circle** and **line** detection in Delphi / Pascal (FMX demo included)

Looking for the VCL version: https://sourceforge.net/projects/houghtransforma/ [2]

---

## Overview

This project contains a Delphi/Pascal implementation of the Hough Image Transformation algorithm.

It supports:

- **Line detection** via classic Hough space (θ, r) voting

- **Circle detection** via a circle Hough transform (radius provided by the user/UI) [1]

The implementation produces good results on simple test images and includes post-processing capabilities with dynamic thresholding [3].

---

## Algorithm Details

### Line Detection Algorithm

The line Hough transform works by:

1. **Voting in parameter space**: For each edge point in the image, the algorithm votes in a 2D accumulator array indexed by angle (θ) and distance (r) [3]

2. **Peak detection**: After voting, peaks in the accumulator represent likely lines in the original image

3. **Thresholding**: The implementation includes both fixed and dynamic thresholding methods to filter results [3]

The key mathematical relationship is:

- r = X \\\* cos(θ) + Y \\\* sin(θ)

The algorithm provides two thresholding approaches:

- **Fixed threshold**: Using HoughResultToParameter with a set range value [3]

- **Dynamic threshold**: Using HoughResultToParameterDynamic for adaptive filtering based on local statistics [3]

### Circle Detection Algorithm

Circle detection is exposed as a function that takes:

- An input bitmap

- A Hough accumulator/result structure

- A **test radius** (chosen by the user) [1]

The implementation is designed to run for a specific radius. To detect circles of unknown sizes, you would need to repeat the process across multiple radii [1].

---

## Project Structure

- Unit\\\_HoughTransformation.pas

Core algorithm implementation for line and circle detection, including helper functions for finding maxima and thresholding [3].

- GUI.FMX.HoughTransform\\\_Demo.pas

FireMonkey (FMX) demo GUI that loads an image, runs line/circle detection, and displays results [1].

- README.md

This file.

---

## API / Key Types

The algorithm unit defines:

- THoughResult = array of array of integer; - 2D accumulator array [3]

- THoughFinal - Structure containing filtered results with X, Y, and Z (vote count) coordinates [3]

Main entry points:

- procedure Hough\\\_LineDetection(AnalysisBitmap: TBitMap; var aHoughResult: THoughResult); [1]

- procedure Hough\\\_CircleDetection(AnalysisBitmap: TBitMap; var aHoughResult: THoughResult; Radius: Integer); [1]

Helper functions:

- function MAX\\\_2D(aHoughResult: THoughResult): integer; - Finds maximum value in accumulator [3]

- function MEAN\\\_2D\\\_Local(aHoughResult: THoughResult; x\\\_pos, y\\\_pos, x\\\_size, y\\\_size: integer): integer; - Calculates local mean [3]

- procedure HoughResultToParameterDynamic(aHoughResult: THoughResult; Range: Real; var aHoughFinal: THoughFinal); - Applies dynamic thresholding [3]

---

## How to Use

### 1) Line Detection (Basic)

pascal

var

\  AnalysisBitmap: TBitmap;

\  HoughResult: THoughResult;

\  ResultBitmap: TBitmap;

begin

\  // Prepare your source bitmap

\  AnalysisBitmap := TBitmap.Create;

\  // ... load image into AnalysisBitmap ...

\  

\  // Run Hough line detection

\  Hough\\\_LineDetection(AnalysisBitmap, HoughResult);

\  

\  // Convert result to viewable bitmap

\  ResultBitmap := TBitmap.Create;

\  HoughresultToBitMap(ResultBitmap, HoughResult);

\  

\  // Display or save ResultBitmap

\  // ...

\  

\  ResultBitmap.Free;

\  AnalysisBitmap.Free;

end;

2\\) Line Detection (With Thresholding)

```pascal

var

\  AnalysisBitmap: TBitmap;

\  HoughResult: THoughResult;

\  HoughFinal: THoughFinal;

\  ResultBitmap: TBitmap;

\  Threshold: Integer;

begin

\  // Run detection

\  Hough\\\_LineDetection(AnalysisBitmap, HoughResult);

\  

\  // Apply dynamic thresholding

\  Threshold := 50; // Percentage (0-100)

\  HoughResultToParameterDynamic(HoughResult, (Threshold / 100), HoughFinal);

\  

\  // Convert filtered results to bitmap

\  ResultBitmap := TBitmap.Create;

\  HoughResultLineParameterToBitMap(ResultBitmap, HoughFinal);

\  

\  ResultBitmap.Free;

end;

3\\) Circle Detection

```pascal
var

\  AnalysisBitmap: TBitmap;

\  HoughResult: THoughResult;

\  ResultBitmap: TBitmap;

\  TestRadius: Integer;

begin

\  // Set the radius to search for

\  TestRadius := 25; // pixels

\  

\  // Run Hough circle detection

\  Hough\\\_CircleDetection(AnalysisBitmap, HoughResult, TestRadius);

\  

\  // Convert and display result

\  ResultBitmap := TBitmap.Create;

\  HoughresultToBitMap(ResultBitmap, HoughResult);

\  

\  ResultBitmap.Free;

end;

FMX Demo Usage

The included FireMonkey demo provides a GUI for testing the algorithms 

GUI.FMX.HoughTr...m\_Demo.pas

:

Loading an Image

Click the Load Image button

Select a test image from your file system 

GUI.FMX.HoughTr...m\_Demo.pas

Line Detection

Load an image

Optionally adjust the Hough Threshold slider (controls filtering sensitivity) 

GUI.FMX.HoughTr...m\_Demo.pas

Click Hough Line button to run line detection 

GUI.FMX.HoughTr...m\_Demo.pas

View results:

Hough Result: Raw accumulator visualization

Hough Accumulator: Filtered line parameters overlaid on image 

GUI.FMX.HoughTr...m\_Demo.pas

Circle Detection

Load an image

Set the radius value using the spinbox control 

GUI.FMX.HoughTr...m\_Demo.pas

Click Hough Circle button to run circle detection 

GUI.FMX.HoughTr...m\_Demo.pas

View the Hough Result image showing detected circles 

GUI.FMX.HoughTr...m\_Demo.pas

Implementation Notes

Dynamic Thresholding

The implementation includes dynamic thresholding functionality that adapts to the accumulator data 

Unit\_HoughTransformation.pas

:

Computes maximum values across the accumulator using MAX\_2D 

Unit\_HoughTransformation.pas

Filters votes based on a percentage of the maximum (Range \* max) 

Unit\_HoughTransformation.pas

Can incorporate local gradient information with configurable minimum gradient (Grad\_Min) 

Unit\_HoughTransformation.pas

Result Interpretation

Bright pixels in accumulator = High vote counts = Likely line/circle parameters

The HoughFinal structure contains filtered (X, Y, Z) coordinates where:

X, Y = Parameter space coordinates (??, r for lines; center coordinates for circles)

Z = Vote count (confidence)