目录
- 编写源码
- 编译
- 测试
平台:树莓派3B
版本: 2021-05-07-raspios-buster-armhf
编写源码
所用源码修改自【机器视觉学习笔记】最近邻插值实现图片任意角度旋转(C++)
在合适的地方编写源码
nano main.cpp
#include <opencv2/opencv.hpp>
#include <iostream>using namespace cv;
using namespace std;Mat RotateImage(Mat src, float angle)
{int x0, y0, x1, y1;angle = angle * 3.1415926535897932384626433832795 / 180;float sin_sita = sin(angle), cos_sita = cos(angle);Mat dst(abs((int)src.cols*sin_sita) + abs((int)src.rows*cos_sita), abs((int)src.cols*cos_sita) + abs((int)src.rows*sin_sita), CV_8UC3, Scalar(0)); //创建新图像for (x1 = 0; x1 < dst.cols; ++x1){for (y1 = 0; y1 < dst.rows; ++y1){float fx1, fy1;//将图片中点设为坐标原点fx1 = x1 - dst.cols / 2;fy1 = y1 - dst.rows / 2;//旧点直角坐标(坐标原点在角上)x0 = fx1*cos_sita - fy1*sin_sita + src.cols / 2 + 0.5;y0 = fx1*sin_sita + fy1*cos_sita + src.rows / 2 + 0.5;if (x0 >= 0 && x0 < src.cols && y0 >= 0 && y0 < src.rows){dst.at<Vec3b>(Point(x1, y1)) = src.at<Vec3b>(Point(x0, y0));}elsedst.at<Vec3b>(Point(x1, y1)) = 0;}}return dst;
}int main(int argc, char * argv[])
{Mat src;Mat Output;String Name = argv[1];src = imread(argv[1]);Output = RotateImage(src, (float)stoi(argv[2]));imshow("原图", src);imshow("输出", Output);imwrite(Name.substr(0, Name.find('.')) + "_Output." + Name.substr(Name.find('.') + 1, Name.length() - Name.find('.') - 1), Output);waitKey(0);return 0;
}
编译
相同的目录下编写CMakeLists.txt文件
nano CMakeLists.txt
project(main)
set(OpenCV_DIR "/usr/local/include/opencv4")
find_package( OpenCV REQUIRED )
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})
其中/usr/local/include/opencv4为安装opencv后库的路径
不同的版本可能不同
配置编译环境并编译
cmake .
make
测试
./main 4.jpg 45
如图可见输出图片已成功自动重命名另存为4_Output.jpg