赞
踩
根据之前的博客进行
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
git clone https://github.com/opencv/opencv.git
cd opencv
nano toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
# 指定交叉编译器位置
set(CMAKE_C_COMPILER /opt/nanopi-toolchain/bin/arm-cortexa9-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER /opt/nanopi-toolchain/bin/arm-cortexa9-linux-gnueabihf-g++)
# 指定系统根目录(sysroot),这是必需的以便编译器找到正确的库和头文件
set(CMAKE_FIND_ROOT_PATH /opt/nanopi-toolchain/arm-cortexa9-linux-gnueabihf/sys-root)
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=../output \
-D BUILD_SHARED_LIBS=ON \
-D WITH_JPEG=ON \
-D CMAKE_TOOLCHAIN_FILE=../toolchain.cmake \
-D BUILD_EXAMPLES=OFF \
-D WITH_IPP=OFF \
-D WITH_TBB=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D CMAKE_C_FLAGS="-std=gnu99" \
-D CMAKE_CXX_FLAGS="-std=c++14" \
-D BUILfD_opencv_python2=OFF \
-D BUILD_opencv_python3=OFF \
-D BUILD_opencv_java=OFF \
-D WITH_OPENCL=OFF \
-D WITH_CUDA=OFF \
-D WITH_GTK=OFF \
-D WITH_VTK=OFF \
-D BUILD_opencv_gapi=OFF .. # 禁用G-API模块
make -j12
# cd build
rm -rf *
-D CMAKE_INSTALL_PREFIX=../output
,因此编译安装的文件输出到了output文件夹中sudo make install
cd ../output
scp -r lib/* pi@192.168.10.197:/usr/local/lib/
scp -r include/* pi@192.168.10.197:/usr/local/include/
test_opencv.cpp
文件#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
// 替换下面的路径为一个实际的图片文件路径
cv::Mat img = cv::imread("/home/pi/head.png", cv::IMREAD_COLOR);
if (img.empty()) {
std::cerr << "Could not open or find the image." << std::endl;
return -1;
}
// 如果在具有图形界面的系统上运行,使用以下代码显示图片
cv::imwrite("/home/pi/output.jpg", img);
return 0;
}
g++ test_opencv.cpp -o test_opencv `pkg-config --cflags --libs opencv4`
./test_opencv # 可以看到生成一个output.jpg文件
cmake_minimum_required(VERSION 3.10)
project(t_cv)
# 设置C++标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 添加include目录
include_directories(include)
# 指定交叉编译工具链
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
# 设置交叉编译器路径
set(CMAKE_C_COMPILER /opt/nanopi-toolchain/bin/arm-linux-gcc)
set(CMAKE_CXX_COMPILER /opt/nanopi-toolchain/bin/arm-linux-g++)
# 查找OpenCV包
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# 添加源代码文件
file(GLOB SOURCES "src/*.cpp")
add_executable(t_cv ${SOURCES})
# 链接OpenCV库
target_link_libraries(t_cv ${OpenCV_LIBS})
# 设置输出文件夹为 'output',没有用
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output)
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/output)
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
// 打开默认相机
cv::VideoCapture cap(1); // 0是默认相机的设备ID。如果不起作用,尝试更换ID。
if (!cap.isOpened()) {
std::cerr << "Error: Couldn't open the camera.\n";
return -1;
}
cv::Mat frame;
std::cout << "Starting camera...\n";
// 从相机捕获一帧
cap >> frame; // 或者使用 cap.read(frame);
if (frame.empty()) {
std::cerr << "Error: Couldn't capture an image.\n";
return -1;
}
// 保存图像
if (!cv::imwrite("/home/pi/captured_image.png", frame)) {
std::cerr << "Error: Couldn't save the image.\n";
return -1;
}
std::cout << "Image saved as /home/pi/captured_image.png\n";
// 释放相机
cap.release();
return 0;
}
// #include <opencv2/opencv.hpp>
// #include <iostream>
// using namespace std;
// int main() {
// // 替换下面的路径为一个实际的图片文件路径
// cv::Mat img = cv::imread("/home/pi/head.png", cv::IMREAD_COLOR);
// if (img.empty()) {
// std::cerr << "Could not open or find the image." << std::endl;
// return -1;
// }
// cout<<"starting"<<endl;
// // 如果在具有图形界面的系统上运行,使用以下代码显示图片
// bool isWritten = cv::imwrite("/home/pi/output.jpg", img);
// if (!isWritten) {
// std::cerr << "Failed to write the image." << std::endl;
// return -1;
// } else {
// cout << "Image written successfully." << endl;
// }
// cout<<"ok"<<endl;
// return 0;
// }
sudo apt-get update
sudo apt-get install libjpeg62
前面的解决思路:上述通过在cmake中添加了如下对jpeg的支持,应该就解决了,上面已经添加了
-D BUILD_SHARED_LIBS=ON \
-D WITH_JPEG=ON \
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。