标签:exe dir void inf down tor cat i++ ack
macOS Catalina 10.15.7
Homebrew 2.5.11
CMake 3.19.0
打开终端,输入brew install cmake
安装cmake的命令行工具。
输入brew install opencv
进行安装,Homebrew会自动将OpenCV的依赖部署到系统目录中。
选择一个空目录,用文本编辑器新建一个CMakeLists.txt,复制以下内容并保存
cmake_minimum_required(VERSION 3.10)
project(Test)
#CMake通过此命令自动寻找OpenCV的依赖
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_STANDARD 17)
include_directories(/usr/local/include)
add_executable(Test make.cpp)
target_link_libraries(Test ${OpenCV_LIBRARIES})
在其同级目录下新建main.cpp,并复制、保存以下测试代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <eigen3/Eigen/Eigen>
#include <opencv2/opencv.hpp>
#define DEBUG
#define CALCU_TIME(start, end) std::cout << "Spend Time::" <<(double)(end - start) / CLOCKS_PER_SEC << "s" << std::endl;
#define SHOW_POINT(point) std::cout << "(" << point.x() << ", " << point.y() << ")" << std::endl;
static const int WIDTH = 700;
static const int HEIGHT = 700;
std::vector<Eigen::Vector3f> frameBuffer;
void putPixel(const Eigen::Vector2i& point) {
int index = (HEIGHT - point.y())*WIDTH + point.x();
frameBuffer[index] = Eigen::Vector3f(255.0f, 255.0f, 255.0f);
}
void draw() {
int key = -1;
while(key != 27) {
cv::Mat image(WIDTH, HEIGHT, CV_32FC3, frameBuffer.data());
image.convertTo(image, CV_8UC3, 1.0f);
cv::imshow("Image", image);
key = cv::waitKey(10);
}
}
void Bresenham(const Eigen::Vector2f& begin, const Eigen::Vector2f& end)
{
float x0 = begin.x();
float y0 = begin.y();
float x1 = end.x();
float y1 = end.y();
// 确保计算方向为正向
if(x0 > x1) {
std::swap(x0, x1);
std::swap(y0, y1);
}
int dX = (int)fabs(x1 - x0);
int dY = (int)fabs(y1 - y0);
int delta = dX - 2 * dY;
int dStepUp = 2 * (dX - dY);
int dStepDown = -2 * dY;
int x = (int)x0, y = (int)y0;
for(int i = x; i != (int)x1; i++) {
putPixel(Eigen::Vector2i(i, y));
SHOW_POINT(Eigen::Vector2i(i, y));
if(delta < 0) {
y += 1;
delta += dStepUp;
} else {
delta += dStepDown;
}
}
}
void DDA(Eigen::Vector2f begin, Eigen::Vector2f end)
{
float x0 = begin.x();
float y0 = begin.y();
float x1 = end.x();
float y1 = end.y();
float dx = fabs(x1 - x0);
float dy = fabs(y1 - y0);
// 选择最大截距方向为步长方向
float step = std::max(dx, dy);
// 若step选择的移动方向为x轴,则deltaX = dx / dx = 1,即默认在x方向进行步长为1的移动
// 同理deltaY = 1
float deltaX = dx / step;
float deltaY = dy / step;
float x = x0, y = y0;
putPixel(Eigen::Vector2i(x, y));
SHOW_POINT(Eigen::Vector2i(x, y));
for(int i = 0 ; i < (int)step ; i++) {
x += deltaX;
y += deltaY;
putPixel(Eigen::Vector2i(x, y));
SHOW_POINT(Eigen::Vector2i(x, y));
}
}
int main() {
frameBuffer.resize(WIDTH * HEIGHT);
std::fill(frameBuffer.begin(), frameBuffer.end(), Eigen::Vector3f{0, 0, 0});
auto point = Eigen::Vector2f(700, 700);
clock_t startTime, endTime;
startTime = clock();
//Bresenham(Eigen::Vector2f(0, 0), point);
DDA(Eigen::Vector2f(0, 0), point);
endTime = clock();
CALCU_TIME(startTime, endTime);
draw();
return 0;
}
终端CD此目录:
mkdir build
命令建立一个目标生成文件夹
cd build
进入目标生成文件夹
cmake ..
cmake检测上级目录的CMakeLists.txt并在build目录中生成makefile
make
开始编译并生成二进制文件
./Test
运行
成功后会看到这个窗口,键盘按下ESC退出。
之后每次修改代码后就再在build目录下使用一次make命令重新编译。
值得一提的是,Jetbrains的CLion本身是通过CMake管理项目的,如果你有CLion可以直接打开这个目录(build的上级),一键运行,很方便。
[环境配置]macOS Homebrew安装OpenCV以及使用
标签:exe dir void inf down tor cat i++ ack
原文地址:https://www.cnblogs.com/1Kasshole/p/14057831.html