图像扩充边界
cpp
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
// 读取图像
cv::Mat image = cv::imread("1.jpg", cv::IMREAD_COLOR);
if (image.empty()) {
std::cerr << "Could not open or find the image!" << std::endl;
return -1;
}
// 指定要添加的边界像素数
int top_border = 20;
int bottom_border = 20;
int left_border = 30;
int right_border = 30;
// 定义边界颜色
cv::Scalar border_color(0, 0, 255); // 在BGR颜色空间中,红色边界
// 使用copyMakeBorder函数扩充边界
cv::Mat bordered_image;
cv::copyMakeBorder(image, bordered_image, top_border, bottom_border, left_border, right_border, cv::BORDER_CONSTANT, border_color);
// 显示原始图像和扩充后的图像
cv::namedWindow("Original Image", cv::WINDOW_NORMAL);
cv::namedWindow("Bordered Image", cv::WINDOW_NORMAL);
cv::imshow("Original Image", image);
cv::imshow("Bordered Image", bordered_image);
cv::waitKey(0);
return 0;
}