Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xmba15 committed Jun 6, 2024
0 parents commit 1ca2b07
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build

on:
push:
branches: ["master"]
pull_request:

jobs:
linting:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v3

- name: Setup python
uses: actions/setup-python@v4
with:
python-version: "3.8"

- name: Apply pre-commit
uses: pre-commit/action@v3.0.0
with:
extra_args: --all-files
88 changes: 88 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
*.pyc
.ipynb_checkpoints
*~
*#
build*

# Packages #
###################
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Images
######################
*.jpg
*.gif
*.png
*.svg
*.ico

# Video
######################
*.wmv
*.mpg
*.mpeg
*.mp4
*.mov
*.flv
*.avi
*.ogv
*.ogg
*.webm

# Audio
######################
*.wav
*.mp3
*.wma

# Fonts
######################
Fonts
*.eot
*.ttf
*.woff

# Format
######################
CPPLINT.cfg
.clang-format

# Gtags
######################
GPATH
GRTAGS
GSYMS
GTAGS
45 changes: 45 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
repos:
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
entry: pflake8
additional_dependencies: [pep8-naming, pyproject-flake8]

- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
language_version: python3

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.961
hooks:
- id: mypy

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier
types_or: [json, markdown, yaml]

- repo: https://github.com/lovesegfault/beautysh
rev: v6.2.1
hooks:
- id: beautysh

- repo: https://github.com/pylint-dev/pylint.git
rev: v3.1.1
hooks:
- id: pylint
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 📝 Simple Line Segment Matching

---

Despite the huge amount of resources about keypoint matching, lesser interest has been shown for line segment matching.
Hence, this repository shows some samples related to basic concepts of line segments:

- Detect line segments using opencv's cv2.ximgproc.createFastLineDetector. The algorithm behind this method is from [HERE](https://ieeexplore.ieee.org/document/6907675).
This method detects line segments specified by their two end points.
- Draw line segment's line support region.
- Extract [Line Band Descriptor](https://www.sciencedirect.com/science/article/abs/pii/S1047320313000874) and match based on the extracted descriptors.

## :running: How to Run

---

- Download sample data:

```bash
wget https://raw.githubusercontent.com/kailigo/LineMatchingBenchmark/master/benchmark/textureless_corridor/1.png -O ./data/textureless_corridor_1.png
wget https://raw.githubusercontent.com/kailigo/LineMatchingBenchmark/master/benchmark/textureless_corridor/2.png -O ./data/textureless_corridor_2.png
```

- Draw detected line segments and their line support regions

```bash
python3 scripts/test_line_segment_detection.py --input ./data/textureless_corridor_1.png
```

![detected lines](./docs/images/detected_lines.jpg)

- Detect and match line segments (The matching method is quite primitive so the matching accuracy is not high).

```bash
python3 scripts/test_line_segment_matching.py --query_path ./data/textureless_corridor_1.png --ref_path ./data/textureless_corridor_2.png
```

![matched lines](./docs/images/matched_lines.jpg)

## 🎛 Development environment

---

```bash
mamba env create --file environment.yml
mamba activate lsmh
```

## :gem: References

---

- [Line Segment Matching: A Benchmark](https://kailigo.github.io/projects/LineMatchingBenchmark)
- [Learning to Parse Wireframes in IMages of Man-Made Environments, CVPR 2018](https://github.com/huangkuns/wireframe)
- [Line Segment Detection: a Review of the 2022 State of the Art](https://www.ipol.im/pub/art/2024/481/article.pdf)
Empty file added data/.keep
Empty file.
Empty file added docs/.keep
Empty file.
Binary file added docs/images/detected_lines.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/matched_lines.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: lsmh
channels:
- defaults
dependencies:
- python=3.8
- pip
- pip:
- -r requirements.txt
21 changes: 21 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[tool.flake8]
max-line-length = 120
max-complexity = 20

[tool.black]
line-length = 120

[tool.isort]
profile = "black"
multi_line_output = 3

[tool.mypy]
ignore_missing_imports = true

[tool.pylint."MESSAGES CONTROL"]
disable = """
missing-docstring,
import-error,
wrong-import-position,
"""
max-line-length = 120
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
opencv-python==4.1.2.30
opencv-contrib-python==4.1.2.30
scikit-image==0.21.0
loguru==0.7.0
Empty file added scripts/.keep
Empty file.
147 changes: 147 additions & 0 deletions scripts/custom_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
from dataclasses import dataclass
from functools import cached_property

import cv2
import numpy as np
from skimage.draw import line

__all__ = [
"LineSegment",
"detect_line_segment",
"detect_and_compute_line_segment",
]


@dataclass
class LineSegment:
start: np.ndarray
end: np.ndarray
lsr_width: int = 10 # line support region width

def _validate(self):
assert self.start.shape[0] == 2
assert self.end.shape[0] == 2
assert self.lsr_width > 0

def __post_init__(self):
self._validate()

@cached_property
def middle(self):
return ((self.start + self.end) // 2).astype(np.uint32)

@cached_property
def length(self):
return np.linalg.norm(self.end - self.start)

@cached_property
def angle(self):
delta = self.end - self.start
return np.arctan2(delta[1], delta[0])

@cached_property
def line_support_region(self):
dx = self.lsr_width * np.sin(self.angle)
dy = self.lsr_width * np.cos(self.angle)

x1, y1 = self.start
x2, y2 = self.end
p1 = (int(x1 - dx), int(y1 + dy))
p2 = (int(x1 + dx), int(y1 - dy))
p3 = (int(x2 + dx), int(y2 - dy))
p4 = (int(x2 - dx), int(y2 + dy))

return np.array([p1, p2, p3, p4], dtype=np.int32)

def draw_line(
self,
image: np.ndarray,
color=(255, 0, 0),
thickness=1,
):
cv2.line(
image,
tuple(self.start),
tuple(self.end),
color=[int(e) for e in color],
thickness=thickness,
lineType=cv2.LINE_AA,
)

def draw_line_support_region(
self,
image: np.ndarray,
color=(0, 255, 0),
thickness=2,
):
cv2.polylines(
image,
[self.line_support_region.reshape((-1, 1, 2))],
isClosed=True,
color=color,
thickness=thickness,
)

def estimate_lbd_descriptor(
self,
gray: np.ndarray,
num_bands: int,
):
band_descriptors = []
for i in range(num_bands):
offset = (i - num_bands / 2) * (self.length / num_bands)
cx = self.start[0] + offset * np.cos(self.angle)
cy = self.start[1] + offset * np.sin(self.angle)

perp_angle = self.angle + np.pi / 2
band_start = (
int(cx - self.lsr_width / 2 * np.cos(perp_angle)),
int(cy - self.lsr_width / 2 * np.sin(perp_angle)),
)
band_end = (
int(cx + self.lsr_width / 2 * np.cos(perp_angle)),
int(cy + self.lsr_width / 2 * np.sin(perp_angle)),
)

rr, cc = line(band_start[1], band_start[0], band_end[1], band_end[0])
valid = (rr >= 0) & (rr < gray.shape[0]) & (cc >= 0) & (cc < gray.shape[1])
intensities = gray[rr[valid], cc[valid]]

if len(intensities) > 0:
band_descriptors.extend([np.mean(intensities), np.std(intensities)])
else:
band_descriptors.extend([0, 0])

band_descriptors = np.array(band_descriptors).reshape(1, -1)
return cv2.normalize(band_descriptors, None, norm_type=cv2.NORM_L2).flatten()


def detect_line_segment(
gray: np.ndarray,
detector=cv2.ximgproc.createFastLineDetector(),
):
cv2_lines = detector.detect(gray)

def _get_segment(cv2_line):
x0, y0, x1, y1 = cv2_line.flatten()
return LineSegment(
np.asarray((x0, y0)),
np.asarray((x1, y1)),
)

return [_get_segment(_line) for _line in cv2_lines]


def detect_and_compute_line_segment(
gray: np.ndarray,
detector=cv2.ximgproc.createFastLineDetector(),
num_bands=16,
):
line_segments = detect_line_segment(
gray,
detector,
)
descriptors = [
line_segment.estimate_lbd_descriptor(gray, num_bands).reshape(1, -1) for line_segment in line_segments
]
return line_segments, np.concatenate(descriptors).astype(np.float32)
Loading

0 comments on commit 1ca2b07

Please sign in to comment.