├── .devcontainer
└── devcontainer.json
├── .github
├── gradio_gui.png
├── hawps.jpg
├── line_1.webm
├── line_2.webm
├── lsdnets.jpg
├── lsds.jpg
├── origs.jpg
├── table1.png
├── tinys.jpg
└── visual_comp_2.png
├── .gitignore
├── LICENSE
├── README.md
├── devops
├── Dockerfile
├── build_cpp.sh
├── build_image.sh
├── detect_lines.sh
├── launch_web_vis.sh
└── requirements.txt
├── line_extracter
├── CMakeLists.txt
├── build_project.sh
├── cmake-build-release
│ └── Line_extracter
├── compute_eigen_coords.cpp
├── compute_eigen_coords.h
├── img_processor.cpp
├── img_processor.h
├── kde.cpp
├── kde.h
├── lsd_methods.cpp
├── lsd_methods.h
├── main.cpp
├── neural_network.cpp
├── neural_network.h
├── region_growing.cpp
├── region_growing.h
├── region_splitter.cpp
├── region_splitter.h
├── roi_extraction.cpp
├── roi_extraction.h
├── segment_extractor.cpp
├── segment_extractor.h
├── speed_test_results
├── to_original_scale.cpp
└── to_original_scale.h
├── lsd
├── __init__.py
├── detect_lines.py
└── models_onnx
│ └── model.onnx
├── visualization
├── context.py
├── display_gradio.py
├── draw_pred.py
└── test_images
│ ├── 00031810.jpg
│ ├── 00037254.jpg
│ └── 00055631.jpg
└── wireframe_tiny
├── images
├── 112.jpg
├── 114.jpg
├── 129.jpg
├── 35.jpg
├── 40.jpg
├── 48.jpg
├── 49.jpg
├── 51.jpg
├── 54.jpg
├── 55.jpg
├── 63.jpg
├── 67.jpg
├── 71.jpg
├── 75.jpg
├── 77.jpg
├── 84.jpg
├── 89.jpg
├── 93.jpg
├── 95.jpg
└── 97.jpg
└── markup
├── 112.jpg.json
├── 114.jpg.json
├── 129.jpg.json
├── 35.jpg.json
├── 40.jpg.json
├── 48.jpg.json
├── 49.jpg.json
├── 51.jpg.json
├── 54.jpg.json
├── 55.jpg.json
├── 63.jpg.json
├── 67.jpg.json
├── 71.jpg.json
├── 75.jpg.json
├── 77.jpg.json
├── 84.jpg.json
├── 89.jpg.json
├── 93.jpg.json
├── 95.jpg.json
└── 97.jpg.json
/.devcontainer/devcontainer.json:
--------------------------------------------------------------------------------
1 | // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.231.3/containers/python-3
3 | {
4 | "image": "lsdnet",
5 |
6 | // Set *default* container specific settings.json values on container create.
7 | "settings": {
8 | "python.defaultInterpreterPath": "/usr/local/bin/python",
9 | "python.linting.enabled": true,
10 | "python.linting.pylintEnabled": true,
11 | "python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
12 | "python.formatting.blackPath": "/usr/local/py-utils/bin/black",
13 | "python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
14 | "python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
15 | "python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
16 | "python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
17 | "python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
18 | "python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
19 | "python.linting.pylintPath": "/usr/local/py-utils/bin/pylint"
20 | },
21 |
22 | // Add the IDs of extensions you want installed when the container is created.
23 | "extensions": [
24 | "ms-python.python",
25 | "ms-python.vscode-pylance"
26 | ],
27 | }
28 |
--------------------------------------------------------------------------------
/.github/gradio_gui.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/.github/gradio_gui.png
--------------------------------------------------------------------------------
/.github/hawps.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/.github/hawps.jpg
--------------------------------------------------------------------------------
/.github/line_1.webm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/.github/line_1.webm
--------------------------------------------------------------------------------
/.github/line_2.webm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/.github/line_2.webm
--------------------------------------------------------------------------------
/.github/lsdnets.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/.github/lsdnets.jpg
--------------------------------------------------------------------------------
/.github/lsds.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/.github/lsds.jpg
--------------------------------------------------------------------------------
/.github/origs.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/.github/origs.jpg
--------------------------------------------------------------------------------
/.github/table1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/.github/table1.png
--------------------------------------------------------------------------------
/.github/tinys.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/.github/tinys.jpg
--------------------------------------------------------------------------------
/.github/visual_comp_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/.github/visual_comp_2.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Project exclude paths
2 |
3 | predictions
4 | videos
5 | line_videos
6 | .vscode
7 |
8 | # Byte-compiled / optimized / DLL files
9 | __pycache__/
10 | *.py[cod]
11 | *$py.class
12 |
13 | # Distribution / packaging
14 | .Python
15 | build/
16 | develop-eggs/
17 | dist/
18 | downloads/
19 | eggs/
20 | .eggs/
21 | parts/
22 | sdist/
23 | var/
24 | wheels/
25 | share/python-wheels/
26 | *.egg-info/
27 | .installed.cfg
28 | *.egg
29 | MANIFEST
30 |
31 | # PyInstaller
32 | # Usually these files are written by a python script from a template
33 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
34 | *.manifest
35 | *.spec
36 |
37 | # Installer logs
38 | pip-log.txt
39 | pip-delete-this-directory.txt
40 |
41 | # Unit test / coverage reports
42 | htmlcov/
43 | .tox/
44 | .nox/
45 | .coverage
46 | .coverage.*
47 | .cache
48 | nosetests.xml
49 | coverage.xml
50 | *.cover
51 | *.py,cover
52 | .hypothesis/
53 | .pytest_cache/
54 | cover/
55 |
56 | # Translations
57 | *.mo
58 | *.pot
59 |
60 | # Django stuff:
61 | *.log
62 | local_settings.py
63 | db.sqlite3
64 | db.sqlite3-journal
65 |
66 | # Flask stuff:
67 | instance/
68 | .webassets-cache
69 |
70 | # Scrapy stuff:
71 | .scrapy
72 |
73 | # Sphinx documentation
74 | docs/_build/
75 |
76 | # PyBuilder
77 | .pybuilder/
78 | target/
79 |
80 | # Jupyter Notebook
81 | .ipynb_checkpoints
82 |
83 | # IPython
84 | profile_default/
85 | ipython_config.py
86 |
87 | # pyenv
88 | # For a library or package, you might want to ignore these files since the code is
89 | # intended to run in multiple environments; otherwise, check them in:
90 | # .python-version
91 |
92 | # pipenv
93 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
94 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
95 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
96 | # install all needed dependencies.
97 | #Pipfile.lock
98 |
99 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
100 | __pypackages__/
101 |
102 | # Celery stuff
103 | celerybeat-schedule
104 | celerybeat.pid
105 |
106 | # SageMath parsed files
107 | *.sage.py
108 |
109 | # Environments
110 | .env
111 | .venv
112 | env/
113 | venv/
114 | ENV/
115 | env.bak/
116 | venv.bak/
117 |
118 | # Spyder project settings
119 | .spyderproject
120 | .spyproject
121 |
122 | # Rope project settings
123 | .ropeproject
124 |
125 | # mkdocs documentation
126 | /site
127 |
128 | # mypy
129 | .mypy_cache/
130 | .dmypy.json
131 | dmypy.json
132 |
133 | # Pyre type checker
134 | .pyre/
135 |
136 | # pytype static type analyzer
137 | .pytype/
138 |
139 | # Cython debug symbols
140 | cython_debug/
141 | .vscode/settings.json
142 |
143 |
144 | line_extracter/cmake-build-release/CMakeFiles
145 | line_extracter/cmake-build-release/cmake_install.cmake
146 | line_extracter/cmake-build-release/CMakeCache.txt
147 | line_extracter/cmake-build-release/Makefile
148 | images_to_test
149 | sandbox/intermediate_predictions
150 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 3-Clause License
2 |
3 | Copyright (c) 2022, iitpvisionlab
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | 1. Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | 2. Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | 3. Neither the name of the copyright holder nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LSDNet: Trainable Modification of LSD Algorithm for Real-Time Line Segment Detection
2 | Official implementation of "LSDNet: Trainable Modification of LSD Algorithm for Real-Time Line Segment Detection." by Teplyakov, Lev, Leonid Erlygin, and Evgeny Shvets. IEEE Access 10 (2022): 45256-45265.
3 |
4 | links to the paper: [IEEE Access](https://ieeexplore.ieee.org/abstract/document/9761231), [arXiv.org](https://arxiv.org/abs/2209.04642)
5 |
6 | ## Quantitative Results
7 |
8 |
9 |
10 |
11 | ## Qualitative Results
12 | Video demo
13 | 
14 |
15 | Images from Wireframe dataset of varying number of salient line segments
16 |
17 |
18 |
19 | LSD detector
20 |
21 |
22 |
23 | HAWP detector
24 |
25 |
26 |
27 | M-LSD-tidy detector
28 |
29 |
30 |
31 | LSDNet, the proposed detector
32 |
33 |
34 |
35 |
36 | ## Setting up LSDNet Project
37 | WARNING: all the scripts run in docker container without GPU support.
38 | We preferred plug-and-play demo with low FPS to top performance with hardcore NVIDIA-docker installation.
39 |
40 | ### Installation
41 | ```bash
42 | cd devops
43 | sh build_image.sh
44 | ```
45 | it takes about 10 min, it will download all the requirements and compile OpenCV the way we need it.
46 |
47 | ### Launch Interactive Demo
48 |
49 | The Interactive Demo allows you to play with LSDNet on single images.
50 |
51 | To launch the demo server (based on [Gradio](https://gradio.app/)) run
52 | ```bash
53 | cd devops
54 | sh launch_web_vis.sh
55 | ```
56 | This commad will launch a web server and print its address. The interface should look like this.
57 |
58 |
59 |
60 | ### Running on Your Images
61 | To make predictions on your images, specify in `devops/detect_lines.sh` the following variables:
62 | - `input_path` - path to an images folder or a single image (images must be `png` or `jpg`)
63 | - `out_path` - path where predicted segments will be saved in `.txt` format (`x1`,`y1`, `x2`,`y2`,`conf`) - the coordinates of segments' endpoints and segment confidence
64 |
65 | As a container mounts to the repo's root directory, paths above must be:
66 | - relative to `lsd/detect_lines.py` file
67 | - inside the repo's directory
68 |
69 | Then make predictions on your images with
70 | ```bash
71 | cd devops
72 | sh detect_lines.sh
73 | ```
74 |
--------------------------------------------------------------------------------
/devops/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM nvidia/cuda:11.3.0-cudnn8-devel-ubuntu20.04
2 |
3 | # install system
4 | RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --fix-missing -y \
5 | build-essential \
6 | libatlas-base-dev \
7 | libavcodec-dev \
8 | libavformat-dev \
9 | libavdevice-dev \
10 | libfontconfig1 \
11 | libgtk-3-dev \
12 | libjpeg-dev \
13 | libpng-dev \
14 | libsm6 \
15 | libswscale-dev \
16 | libtiff-dev \
17 | libv4l-dev \
18 | libx264-dev \
19 | libxext6 \
20 | libxrender1 \
21 | libxvidcore-dev \
22 | yasm \
23 | python3-dev \
24 | locales \
25 | sudo \
26 | ninja-build \
27 | ffmpeg \
28 | wget \
29 | unzip \
30 | python3-pip \
31 | && \
32 | rm -rf /var/lib/apt/lists/* \
33 | && \
34 | locale-gen "en_US.UTF-8"
35 |
36 | # cmake
37 | RUN wget https://github.com/Kitware/CMake/releases/download/v3.19.7/cmake-3.19.7-Linux-x86_64.sh && \
38 | sh cmake-3.19.7-Linux-x86_64.sh --skip-license --prefix=/usr/local
39 |
40 | # install opencv
41 | ARG opencv_ver=4.5.2
42 | RUN wget -O opencv-${opencv_ver}.zip https://github.com/opencv/opencv/archive/${opencv_ver}.zip && \
43 | wget -O opencv_contrib-${opencv_ver}.zip https://github.com/opencv/opencv_contrib/archive/${opencv_ver}.zip && \
44 | unzip opencv-${opencv_ver}.zip && \
45 | unzip opencv_contrib-${opencv_ver}.zip && \
46 | mkdir opencv-${opencv_ver}/build && cd opencv-${opencv_ver}/build && \
47 | cmake -D CMAKE_BUILD_TYPE=RELEASE \
48 | -D CMAKE_INSTALL_PREFIX=/usr/local \
49 | -D INSTALL_PYTHON_EXAMPLES=ON \
50 | -D INSTALL_C_EXAMPLES=OFF \
51 | -D OPENCV_ENABLE_NONFREE=ON \
52 | -D WITH_OPENCL=OFF \
53 | -D WITH_OPENJPEG=OFF \
54 | -D WITH_JASPER=OFF \
55 | -D WITH_OPENEXR=OFF \
56 | -D WITH_V4L=OFF \
57 | -D WITH_FFMPEG=OFF \
58 | -D WITH_GSTREAMER=OFF \
59 | -D BUILD_JAVA=OFF \
60 | -D WITH_CUDA=OFF \
61 | -D BUILD_TESTS=OFF \
62 | -D WITH_CUDNN=OFF \
63 | -D OPENCV_DNN_CUDA=OFF \
64 | -D OPENCV_EXTRA_MODULES_PATH=/opencv_contrib-${opencv_ver}/modules \
65 | -D HAVE_opencv_python3=ON \
66 | -D BUILD_EXAMPLES=OFF .. && \
67 | make install -j12 && \
68 | ldconfig && \
69 | rm -rf opencv-${opencv_ver}.zip opencv_contrib-${opencv_ver}.zip \
70 | opencv-${opencv_ver} opencv_contrib-${opencv_ver}
71 |
72 | # python
73 | RUN ln -s /usr/bin/python3 /usr/local/bin/python
74 | # RUN python -m pip --upgrade pip --no-cache-dir
75 | COPY requirements.txt .
76 | RUN pip3 install -r requirements.txt --no-cache-dir
77 |
--------------------------------------------------------------------------------
/devops/build_cpp.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | docker run\
4 | --rm \
5 | -v "$(realpath ..)":/home/devel/lsdnet\
6 | --runtime nvidia\
7 | --workdir /home/devel/lsdnet/line_extracter/\
8 | -t lsdnet\
9 | bash build_project.sh
10 |
--------------------------------------------------------------------------------
/devops/build_image.sh:
--------------------------------------------------------------------------------
1 | docker build -f Dockerfile -t lsdnet .
2 |
--------------------------------------------------------------------------------
/devops/detect_lines.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # set these variables
4 | input_path="../visualization/test_images/"
5 | out_path="../predictions"
6 |
7 | model_path="models_onnx/model.onnx"
8 | docker run\
9 | --rm \
10 | -v "$(realpath ..)":/home/devel/lsdnet\
11 | --workdir /home/devel/lsdnet/lsd/\
12 | -t lsdnet\
13 | python detect_lines.py $input_path $out_path $model_path
14 |
--------------------------------------------------------------------------------
/devops/launch_web_vis.sh:
--------------------------------------------------------------------------------
1 | docker run\
2 | --rm \
3 | -v "$(realpath ..)":/home/devel/lsdnet\
4 | --name lsdnet_web_vis\
5 | --runtime nvidia\
6 | --workdir /home/devel/lsdnet/visualization/\
7 | -p 7860:7860\
8 | -t lsdnet\
9 | python display_gradio.py
10 |
--------------------------------------------------------------------------------
/devops/requirements.txt:
--------------------------------------------------------------------------------
1 | opencv-python==3.4.8.29
2 | opencv-contrib-python==3.4.8.29
3 | Pillow==8.3.2
4 | scipy==1.4.1
5 | matplotlib==3.2.1
6 | tqdm==4.46.1
7 | cffi==1.14.0
8 | tensorboard==2.3.0
9 | pandas==1.0.4
10 | plotly==4.8.1
11 | scikit-image==0.17.2
12 | ftpretty==0.3.2
13 | scikit-learn==0.23.1
14 | mlflow==1.8.0
15 | tf2onnx==1.8.5
16 | onnxruntime==1.8.0
17 | gradio==1.7.0
18 |
--------------------------------------------------------------------------------
/line_extracter/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.17)
2 | project(Line_extracter)
3 |
4 | set(CMAKE_CXX_STANDARD 14)
5 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
6 |
7 | add_executable(Line_extracter main.cpp compute_eigen_coords.cpp compute_eigen_coords.h
8 | img_processor.cpp img_processor.h neural_network.cpp neural_network.h region_growing.cpp
9 | region_growing.h region_splitter.cpp region_splitter.h roi_extraction.cpp roi_extraction.h
10 | segment_extractor.cpp segment_extractor.h to_original_scale.cpp to_original_scale.h kde.h kde.cpp
11 | lsd_methods.cpp lsd_methods.h)
12 | find_package(OpenCV REQUIRED)
13 | include_directories(${OpenCV_INCLUDE_DIRS})
14 | set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
15 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
--------------------------------------------------------------------------------
/line_extracter/build_project.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | cmake -Bcmake-build-release -H. -DCMAKE_BUILD_TYPE=Release
4 | cd cmake-build-release
5 | make
--------------------------------------------------------------------------------
/line_extracter/cmake-build-release/Line_extracter:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iitpvisionlab/LSDNet/aea2bf7c5853aa1c3cfe04d42e032034188a600f/line_extracter/cmake-build-release/Line_extracter
--------------------------------------------------------------------------------
/line_extracter/compute_eigen_coords.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by leonid on 14.02.2021.
3 | //
4 |
5 | #include "compute_eigen_coords.h"
6 |
7 | compute_eigen_coords::compute_eigen_coords() = default;
8 |
9 | std::vector> compute_eigen_coords::apply_trans(const std::vector> &transformation,
10 | const std::vector> ®ion,
11 | const cv::Mat &cls_map) {
12 | std::vector> new_coords(2, std::vector(region[0].size()));
13 | float cos_alpha = transformation[0][0];
14 | float sin_alpha = transformation[1][0];
15 | for (int i = 0; i < region[0].size(); ++i) {
16 | float x = (region[0][i] - x_mean);
17 | float y = (region[1][i] - y_mean);
18 | new_coords[0][i] = cos_alpha * x + sin_alpha * y;
19 | new_coords[1][i] = -sin_alpha * x + cos_alpha * y;
20 | // float weight;
21 | // if (cls_map.data!=NULL) {
22 | // weight = cls_map.at(region[0][i], region[1][i]);
23 | // }
24 | }
25 | return new_coords;
26 | }
27 |
28 | std::vector> compute_eigen_coords::apply_inverse_trans(
29 | const std::vector> &transformation,
30 | const std::vector> &points_new) const {
31 | std::vector> points(2, std::vector(points_new[0].size()));
32 | float cos_alpha = transformation[0][0];
33 | float sin_alpha = transformation[1][0];
34 | for (int i = 0; i < points_new[0].size(); ++i) {
35 | float x = points_new[0][i];
36 | float y = points_new[1][i];
37 | points[0][i] = cos_alpha * x - sin_alpha * y + x_mean;
38 | points[1][i] = sin_alpha * x + cos_alpha * y + y_mean;
39 |
40 | points[0][i] = ( 0 < points[0][i]) ? points[0][i] : 0;
41 | points[1][i] = ( 0 < points[1][i]) ? points[1][i] : 0;
42 | }
43 | return points;
44 | }
45 |
46 | std::vector> compute_eigen_coords::compute_transformation_matrix(
47 | const std::vector> ®ion, const cv::Mat &cls_map) {
48 |
49 | std::vector x = region[0];
50 | std::vector y = region[1];
51 |
52 | float mxx = 0;
53 | float myy = 0;
54 | float mxy = 0;
55 |
56 | int n = x.size();
57 |
58 | bool x_all_eq = true;
59 | bool y_all_eq = true;
60 | if (cls_map.data == NULL) {
61 | exit(1);
62 | // x_mean = std::accumulate(x.begin(), x.end(), 0.0) / n;
63 | // y_mean = std::accumulate(y.begin(), y.end(), 0.0) / n;
64 | //
65 | //
66 | // for (int i = 0; i < n; ++i) {
67 | // float x_i = x[i] - x_mean;
68 | // float y_i = y[i] - y_mean;
69 | // mxx += (x_i * x_i);
70 | // myy += (y_i * y_i);
71 | // mxy += (x_i * y_i);
72 | // mxx += (y_i * y_i);
73 | // myy += (x_i * x_i);
74 | // mxy += -(x_i * y_i);
75 | // }
76 | // mxx = mxx / n;
77 | // myy = myy / n;
78 | // mxy = mxy / n;
79 |
80 | } else {
81 | float S = 0;
82 | float curr_x_sum = 0;
83 | float curr_y_sum = 0;
84 | int x_first = x[0];
85 | int y_first = y[0];
86 |
87 | for (int i = 0; i < n; ++i) {
88 | if (x[i] != x_first) x_all_eq = false;
89 | if (y[i] != y_first) y_all_eq = false;
90 |
91 | float weight = cls_map.at(x[i], y[i]);
92 | curr_x_sum += (x[i] * weight);
93 | curr_y_sum += (y[i] * weight);
94 | S += weight;
95 | }
96 | x_mean = curr_x_sum / S;
97 | y_mean = curr_y_sum / S;
98 |
99 | for (int i = 0; i < n; ++i) {
100 | float weight = cls_map.at(x[i], y[i]);
101 | float x_i = x[i] - x_mean;
102 | float y_i = y[i] - y_mean;
103 | mxx += (x_i * x_i * weight);
104 | myy += (y_i * y_i * weight);
105 | mxy += (x_i * y_i * weight);
106 | // mxx += (y_i * y_i * weight);
107 | // myy += (x_i * x_i * weight);
108 | // mxy += -(x_i * y_i * weight);
109 | }
110 | mxx = mxx / S;
111 | myy = myy / S;
112 | mxy = mxy / S;
113 | }
114 | std::vector> A = {
115 | {0, 0},
116 | {0, 0}
117 | };
118 | if (not x_all_eq and not y_all_eq) {
119 | float M[2][2] = {
120 | {mxx, mxy},
121 | {mxy, myy}
122 | };
123 | float lamb = (M[1][1] + M[0][0] + sqrt(4 * M[0][1] * M[0][1] + (M[0][0] - M[1][1]) * (M[0][0] - M[1][1]))) / 2;
124 | float direction[2] = {M[0][1], (lamb - M[0][0])};
125 |
126 | float f = (direction[0] >= 0) ? 1 : -1;
127 | float theta = acos(f * (direction[1]) / pow(pow(direction[1], 2) + pow(direction[0], 2), 0.5));
128 | float alpha = M_PI / 2 - theta;
129 |
130 | float sin_alpha = std::sin(alpha);
131 | float cos_alpha = std::cos(alpha);
132 |
133 | A = {
134 | {cos_alpha, -sin_alpha},
135 | {sin_alpha, cos_alpha}
136 | };
137 | } else {
138 | float direction[2] = {0, 0};
139 | if (x_all_eq){
140 | A = {
141 | {1, 0},
142 | {0, 1}
143 | };
144 | }else {
145 | A = {
146 | {0, -1},
147 | {1, 0}
148 | };
149 | }
150 |
151 |
152 |
153 |
154 |
155 | }
156 | return A;
157 | }
--------------------------------------------------------------------------------
/line_extracter/compute_eigen_coords.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by leonid on 14.02.2021.
3 | //
4 |
5 | #ifndef LINE_EXTRACTER_COMPUTE_EIGEN_COORDS_H
6 | #define LINE_EXTRACTER_COMPUTE_EIGEN_COORDS_H
7 |
8 | #include "opencv2/core/core.hpp"
9 | #include
10 | #include
11 |
12 | class compute_eigen_coords {
13 | public:
14 | compute_eigen_coords();
15 |
16 | std::vector>
17 | compute_transformation_matrix(const std::vector> ®ion, const cv::Mat &cls_map = cv::Mat());
18 |
19 | std::vector>
20 | apply_trans(const std::vector> &transformation, const std::vector> ®ion,
21 | const cv::Mat &cls_map = cv::Mat());
22 |
23 | std::vector>
24 | apply_inverse_trans(const std::vector> &transformation,
25 | const std::vector> &points_new) const;
26 | private:
27 |
28 | float x_mean = 0;
29 | float y_mean = 0;
30 | };
31 |
32 |
33 | #endif //LINE_EXTRACTER_COMPUTE_EIGEN_COORDS_H
34 |
--------------------------------------------------------------------------------
/line_extracter/img_processor.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by leonid on 14.02.2021.
3 | //
4 |
5 | #include "img_processor.h"
6 |
--------------------------------------------------------------------------------
/line_extracter/img_processor.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by leonid on 14.02.2021.
3 | //
4 |
5 | #ifndef LINE_EXTRACTER_IMG_PROCESSOR_H
6 | #define LINE_EXTRACTER_IMG_PROCESSOR_H
7 |
8 |
9 | class img_processor {
10 |
11 | };
12 |
13 |
14 | #endif //LINE_EXTRACTER_IMG_PROCESSOR_H
15 |
--------------------------------------------------------------------------------
/line_extracter/kde.cpp:
--------------------------------------------------------------------------------
1 | // Kernel density estimation by Tim Nugent (c) 2014
2 | // Based on Philipp K. Janert's Perl module:
3 | // http://search.cpan.org/~janert/Statistics-KernelEstimation-0.05
4 | // Multivariate stuff from here:
5 | // http://sfb649.wiwi.hu-berlin.de/fedc_homepage/xplore/ebooks/html/spm/spmhtmlnode18.html
6 |
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include