目录
一. 上传opencv 预编译包
二. 自己手动写一个测试包并上传
三. 自己写一个app, 引用包
一. 上传opencv 预编译包
1. 下载Opencv, 并用cmake 打开
打开工程之后,编译,install, 目录如下
2. 准备conan 包
把Debug 和 Release 分开放
3. 然后执行如下命令创建本地的包
conan create . --user=moz --channel=stable -s build_type=Debug
conan create . --user=moz --channel=stable -s build_type=Release
4. 上传包到remote
conan upload opencv -r=moz_stable
二. 自己手动写一个测试包并上传
1).用如下命令生成一个包
conan new cmake_lib -d name=lib_example -d version=1.0
2). 修改代码
然后在test_package中调用
代码写完之后,修改conanfile.py
然后执行
conan create . --user=moz --channel=stable -s build_type=Debug
执行上述命令的时候,报如下错误:
-- Configuring done (2.1s)
CMake Error in CMakeLists.txt:
IMPORTED_IMPLIB not set for imported target "opencv_calib3d" configuration
"MinSizeRel".
CMake Error in CMakeLists.txt:
IMPORTED_IMPLIB not set for imported target "opencv_core" configuration
"MinSizeRel".
CMake Error in CMakeLists.txt:
IMPORTED_IMPLIB not set for imported target "opencv_dnn" configuration
"MinSizeRel".
原因: OpenCV 自己的bug
解决办法: 需要在example 的CMakeLists find_package(opencv) 之前,添加如下代码:
set(OPENCV_MAP_IMPORTED_CONFIG "")
然后再执行上述命令,就顺利完成了
conan create . --user=moz --channel=stable -s build_type=Release
然后上传
conan upload lib_example -r=moz_stable
三. 自己写一个app, 引用包
1. 写conanfile.py
import os
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps
class TestRecipe(ConanFile):
settings = "os", "compiler", "build_type", "arch"
name = "opencv_test"
version = "1.0"
package_type = "application"
# generators = "CMakeToolchain", "CMakeDeps"
def requirements(self):
self.requires("opencv/4.10.0@moz/stable")
def generate(self):
deps = CMakeDeps(self)
deps.generate()
tc = CMakeToolchain(self)
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
2. 准备 CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(opencv_test LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#list(APPEND CMAKE_PREFIX_PATH "C:\\Qt5.15\\5.15.2\\msvc2019_64")
message("-----CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
set(OPENCV_MAP_IMPORTED_CONFIG "")
find_package(OpenCV REQUIRED)
message(${OpenCV_INCLUDE_DIRS})
#message(${OpenCV_LIBRARY_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIBRARY_DIRS})
add_executable(opencv_test main.cpp)
target_link_libraries(opencv_test ${OpenCV_LIBRARIES})
install(TARGETS opencv_test
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
3. 写一个demo app
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
Mat img;
Mat lab_img;
int l_value = 100, a_value = 128, b_value = 128;
int max_lvalue = 200;
int max_avalue = 255;
int max_bvalue = 255;
void onTrackbarChange(int, void*) {
Mat disp_img;
lab_img.copyTo(disp_img);
// forEach
// 指针
for (int i = 0; i < lab_img.rows; ++i) {
for (int j = 0; j < lab_img.cols; ++j) {
Vec3b& color = disp_img.at<Vec3b>(i, j);
int L = color[0];
int a = color[1] - 128;
int b = color[2] - 128;
L = L + l_value - 100;
a = a + a_value - 128 ;
b = b + b_value - 128;
L = std::max(0.0, std::min(255.0, L*1.0));
a = std::max(-128.0, std::min(127.0, a*1.0));
b = std::max(-128.0, std::min(127.0, b*1.0));
color[0] = L ;
color[1] = a + 128 ;
color[2] = b + 128;
}
}
cvtColor(disp_img, disp_img, COLOR_Lab2BGR);
imshow("Lab Color", disp_img);
}
void onMouse(int event, int x, int y, int flags, void* param) {
if (event == EVENT_LBUTTONUP) {
Vec3b color = lab_img.at<Vec3b>(y, x);
double l = color[0];
double a = color[1];
double b = color[2];
std::cout << "L=" << l << ", a=" << a << ", b=" << b << std::endl;
}
}
int main() {
img = imread("test.png");
if (img.empty()) {
std::cout << "Please input correct image path." << std::endl;
return -1;
}
namedWindow("Lab Color", WINDOW_NORMAL);
setMouseCallback("Lab Color", onMouse);
cvtColor(img, lab_img, COLOR_BGR2Lab);
createTrackbar("L Value", "Lab Color", &l_value, max_lvalue, onTrackbarChange);
createTrackbar("a Value", "Lab Color", &a_value, max_avalue, onTrackbarChange);
createTrackbar("b Value", "Lab Color", &b_value, max_bvalue, onTrackbarChange);
while (true) {
char key = waitKey(10);
if (key == 27) { // Press Esc to exit
break;
}
}
destroyAllWindows();
return 0;
}
4. 用conan 构建
1. 下载缺失的库
conan install . --output-folder=build --build=missing
2. build
conan build .
两步之后,就可以正常生成了,只是没有生成到build目录下,然后release 下面就有一个生成的exe