1. opencv常用函数(copyMakeBorder、copyTo)
copyMakeBorder:在图片的四周添加指定的边;
copyTo:用与把图片或者特征合并起来;
2. 代码实现
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
int test() {
std::string img_path = "../test.png";
cv::Mat img = cv::imread(img_path);
cv::Mat ROI = img(cv::Rect(0,0,100,100));
// 测试cv::copyMakeBorder()函数
cv::Mat img_border;
cv::copyMakeBorder(img, img_border, 20, 20, 20, 20, cv::BORDER_CONSTANT, {0, 0, 0});
cv::imwrite("../border.jpg", img_border);
// 测试copyTo,将多个图片concat在一起,也可以concat特征
int height = img.rows;
int width = img.cols;
cv::Mat img_concat = cv::Mat::zeros(height, width*2, CV_32FC3);
img.copyTo(img_concat(cv::Rect(0,0, width, height)));
img.copyTo(img_concat(cv::Rect(width, 0, width, height)));
cv::imwrite("../concat.jpg", img_concat);
return 0;
}
int main()
{
test();
std::cout << "test success!" << std::endl;
return 0;
}
3. 效果
test.jpg
border.jpg
concat.jpg