复现的代码
本博客旨在复现论文《An Efficient High-quality Ellipse Detection》,该文章本来只有Matlab的代码实现,后来被islands翻译成了c++ 库,大家可以参考islands发在知乎上的文章高质量椭圆检测库,C++的代码链接。
使用环境
Ubuntu 22.04
bash
lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 22.04.4 LTS
Release: 22.04
Codename: jammy
Cmake 3.22.1
bash
cmake -version
cmake version 3.22.1
C++ 11.4.0
bash
g++ -v
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
Opencv 3.4.9
bash
pkg-config --modversion opencv
3.4.9
安装C++编译器
查看自己的C++编译器版本
bash
cmake --version
cmake version 3.22.1
如果没有安装cmake,那么可以使用指令自行安装
bash
sudo apt-get install cmake
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev
安装opencv
通过官网,使用代码安装opencv https://github.com/opencv/opencv/releases
我选择了opencv3.4.9,选择最后的Source.code(tar.gz)进行下载。
下载完毕后放入合适的路径,进行解压。
之后创建release文件夹,并进行编译
bash
mkdir release
cd release
cmake ..
sudo make
sudo make install
验证opencv是否安装成功
cpp
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
std::cout << "OpenCV version: " << CV_VERSION << std::endl;
return 0;
}
卸载opencv
bash
sudo make uninstall
cd ..
sudo rm -r build
安装Lapack
确定已安装gfortran
bash
sudo apt-get install gfortran
源码安装Lapack,下载并解压
进行编译
bash
cd lapack-3.9.1
mkdir build
cd build
cmake ..
make -j7
sudo make install
sudo ldconfig
cd ..
sudo cp LAPACKE/include/*.h /usr/local/include/
Ellipse detectieon C++库
完成基础环境的配置之后我们就可以去编译Ellipse detection了。参考库中的Readme进行安装。
bash
git clone https://github.com/memory-overflow/standard-ellipse-detection.git
cd standard-ellipse-detection
mkdir build && cd build
cmake ..
make
sudo make install
出错'cv::Mat' to non-scalar type
!亲测:使用opencv3.3.0不会报错,因此使用opencv3.3.0时不需要修改代码。
如果你和我一样,那么可能会在camek时出现如下错误。
from 'cv::Mat' to non-scalar type 'CvMat' requested
根据错误的位置定位到,源代码位置: standard-ellipse-detection/src/cvcannyapi.cpp的307行,我们将之前的代码进行修改,修改后代码如下。参考博文
cpp
CvMat c_src = cvMat(src), c_dst = cvMat(_edges.getMat());
CvMat c_dx = cvMat(_sobel_x.getMat());
CvMat c_dy = cvMat(_sobel_y.getMat());
配置.vscode
c_cpp_properties.json
Ctrl+Shift+P -> C/C++:Edit Configurations(UI),生成c_cpp_properties.json文件
IntelliSense mode选择 linux-gcc-x64,
在Include path中添加opencv的path,具体c_cpp_properties.json文件如下。
python
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${default}",
"/usr/local/include/opencv",
"/usr/local/include/opencv2"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
tasks.json
Ctrl+Shift+P -> Tasks: Configure Tasks,选择编译器,生成tasks.json文件。
在tasks.json中加入opencv库,Lapack库,以及刚刚安装的ellipse_detection库,tasks.json的示例如下,主要逐一args中的参数。"-lellipse_detection","`pkg-config","--libs","--cflags","opencv`","-llapacke","-llapack","-lblas","-lgfortran"。
"-lellipse_detection",
python
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lellipse_detection",
"`pkg-config","--libs","--cflags","opencv`",
"-llapacke",
"-llapack",
"-lblas",
"-lgfortran"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
测试
我们使用作者提供的测试代码进行测试,代码位置在standard-ellipse-detection/test,我们对testdetect.cpp进行编译。如果顺利的话,那么就可以编译成功了。之后调用testdetect应用程序就可以看到椭圆检测以及之后的图了。
cpp
./testdetect "/path/to/image"