example_cv2.py
import cv2
import numpy as np
import os
from pylsd1.lsd import *
fullName = 'xian3.jpg'
folder, imgName = os.path.split(fullName)
src = cv2.imread(fullName, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
lines = lsd(gray)
for i in range(lines.shape[0]):
pt1 = (int(lines[i, 0]), int(lines[i, 1]))
pt2 = (int(lines[i, 2]), int(lines[i, 3]))
width = lines[i, 4]
cv2.line(src, pt1, pt2, (0, 0, 255), int(np.ceil(width / 2)))
cv2.imwrite(os.path.join(folder, 'cv2_' + imgName.split('.')[0] + '.jpg'), src)
lsd.py
from .bindings.lsd_ctypes import *
def lsd(src):
rows, cols = src.shape
src = src.reshape(1, rows * cols).tolist()[0]
temp = os.path.abspath(str(np.random.randint(
1, 1000000)) + 'ntl.txt').replace('\\', '/').encode('utf-8')
lens = len(src)
src = (ctypes.c_double * lens)(*src)
lsdlib.lsdGet(src, ctypes.c_int(rows), ctypes.c_int(cols), ctypes.c_char_p(temp))
fp = open(temp, 'r')
cnt = fp.read().strip().split(' ')
fp.close()
os.remove(temp)
count = int(cnt[0])
dim = int(cnt[1])
lines = np.array([float(each) for each in cnt[2:]])
lines = lines.reshape(count, dim)
return lines
__init__.py
from . import lsd
lsd_ctypes.py
import ctypes
import os
import sys
import random
import numpy as np
def load_lsd_library():
root_dir = os.path.abspath(os.path.dirname(__file__))
libnames = ['linux/liblsd.so']
libdir = 'lib'
if sys.platform == 'win32':
if sys.maxsize > 2 ** 32:
libnames = ['win32/x64/lsd.dll', 'win32/x64/liblsd.dll']
else:
libnames = ['win32/x86/lsd.dll', 'win32/x86/liblsd.dll']
elif sys.platform == 'darwin':
libnames = ['darwin/liblsd.dylib']
while root_dir != None:
for libname in libnames:
try:
lsdlib = ctypes.cdll[os.path.join(root_dir, libdir, libname)]
return lsdlib
except Exception as e:
pass
tmp = os.path.dirname(root_dir)
if tmp == root_dir:
root_dir = None
else:
root_dir = tmp
# if we didn't find the library so far, try loading without
# a full path as a last resort
for libname in libnames:
try:
# print "Trying",libname
lsdlib = ctypes.cdll[libname]
return lsdlib
except:
pass
return None
lsdlib = load_lsd_library()
if lsdlib == None:
raise ImportError('Cannot load dynamic library. Did you compile LSD?')
__init__.py (bindings文件目录下)
from .lsd_ctypes import *