pylsd-nova is a python binding for LSD - Line Segment Detector
Project description
pylsd-nova
1. Introduction
pylsd-nova is a python binding for LSD - Line Segment Detector.
This is a fork from original pylsd binding. This fork works for Python 3 and adds the ability to change lsd parameters from python call.
2. Install
This package uses distutils, which is the default way of installing python modules. For installing by cloning the repository you can run the following commands:
git clone https://github.com/AndranikSargsyan/pylsd-nova.git
cd pylsd-nova
pip install .
Or install directly through pip
:
pip install pylsd-nova
3. Usage
You can use the package by importing like from pylsd import lsd
, and calling segments = lsd(img)
by optionally passing other lsd parameters mentioned below. img
is a Grayscale Image (H x W
numpy.ndarray), and the return value segments
contains detected line segments.
segments
is an N x 5
numpy.ndarray. Each row represents a straight line. The 5-dimensional row format is:
[point1.x, point1.y, point2.x, point2.y, width]
These are the parameters of lsd, which can be changed through keyword arguments of lsd call:
-
scale (float)
: Scale the image by Gaussian filter to 'scale'. -
sigma_scale (float)
: Sigma for Gaussian filter is computed assigma = sigma_scale / scale
. -
quant (float)
: Bound to the quantization error on the gradient norm. -
ang_th (float)
: Gradient angle tolerance in degrees. -
eps (float)
: Detection threshold,-log10(NFA)
. -
density_th (float)
: Minimal density of region points in rectangle. -
n_bins (int)
: Number of bins in pseudo-ordering of gradient modulus. -
max_grad (float)
: Gradient modulus in the highest bin. The default value corresponds to the highest gradient modulus on images with gray levels in [0,255].
You can use it just like the following code (here is the link to examples):
- by using cv2 module
import cv2
import numpy as np
import os
from pylsd import lsd
full_name = 'car.jpg'
folder, img_name = os.path.split(full_name)
img = cv2.imread(full_name, cv2.IMREAD_COLOR)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
segments = lsd(img_gray, scale=0.5)
for i in range(segments.shape[0]):
pt1 = (int(segments[i, 0]), int(segments[i, 1]))
pt2 = (int(segments[i, 2]), int(segments[i, 3]))
width = segments[i, 4]
cv2.line(img, pt1, pt2, (0, 0, 255), int(np.ceil(width / 2)))
cv2.imwrite(os.path.join(folder, 'cv2_' + img_name.split('.')[0] + '.jpg'), img)
- by using PIL(Image) module
import numpy as np
import os
from PIL import Image, ImageDraw
from pylsd import lsd
full_name = 'house.png'
folder, img_name = os.path.split(full_name)
img = Image.open(full_name)
img_gray = np.asarray(img.convert('L'))
segments = lsd(img_gray, scale=0.5)
draw = ImageDraw.Draw(img)
for i in range(segments.shape[0]):
pt1 = (int(segments[i, 0]), int(segments[i, 1]))
pt2 = (int(segments[i, 2]), int(segments[i, 3]))
width = segments[i, 4]
draw.line((pt1, pt2), fill=(0, 0, 255), width=int(np.ceil(width / 2)))
img.save(os.path.join(folder, 'PIL_' + img_name.split('.')[0] + '.jpg'))
The following is the result:
- car.jpg by using cv2 module
- house.png by using PIL(Image) module
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pylsd-nova-1.2.0.tar.gz
.
File metadata
- Download URL: pylsd-nova-1.2.0.tar.gz
- Upload date:
- Size: 158.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.8.0 tqdm/4.48.0 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fab151b0ea3320f39266f9a8ce5df10cdf34cecfebea4dbe0b22aa3d98e4e4f1 |
|
MD5 | 7b03e3325c1d092450da70ccbe5b2cc3 |
|
BLAKE2b-256 | c8733f647cf3e8774f3e746f93016c9fc705e4647831df577a2035561abf7e97 |