如何使用opencv在图像上添加边? 我使用C++中的OpenCV,我有一个对象(图像1)的二进制图像。

如何使用opencv在图像上添加边? 我使用C++中的OpenCV,我有一个对象(图像1)的二进制图像。,c++,opencv,image-processing,C++,Opencv,Image Processing,我想在图像的顶部、左侧、右侧和下方添加像素(图3),因为我使用Zhang Suen算法(图2)获得对象的骨架,并在顶部、左侧、右侧和下方添加像素。我修复了图像2中可见的错误,如何在边上添加5像素 我想将图像1转换为图像3。输入图像: // Load input image cv::Mat input = cv::imread("zero.png"); if (input.empty()) { std::cout << "!!! Failed imread\n"; r

我想在图像的顶部、左侧、右侧和下方添加像素(图3),因为我使用Zhang Suen算法(图2)获得对象的骨架,并在顶部、左侧、右侧和下方添加像素。我修复了图像2中可见的错误,如何在边上添加5像素


我想将图像1转换为图像3。

输入图像:

// Load input image
cv::Mat input = cv::imread("zero.png");
if (input.empty())
{
    std::cout << "!!! Failed imread\n";
    return -1;
}

// Create a larger output image to store the end result
cv::Mat output(input.rows+10, input.cols+10, input.type(), cv::Scalar(0));

// Specify the size of the copy and its offset 
cv::Rect offset_rect = cv::Rect(5, 5, input.cols, input.rows);

// Copy to the output Mat
input.copyTo(output(offset_rect));

//cv::imwrite("output.png", output);

//加载输入图像
cv::Mat input=cv::imread(“zero.png”);
if(input.empty())
{

std::cout输入图像:

// Load input image
cv::Mat input = cv::imread("zero.png");
if (input.empty())
{
    std::cout << "!!! Failed imread\n";
    return -1;
}

// Create a larger output image to store the end result
cv::Mat output(input.rows+10, input.cols+10, input.type(), cv::Scalar(0));

// Specify the size of the copy and its offset 
cv::Rect offset_rect = cv::Rect(5, 5, input.cols, input.rows);

// Copy to the output Mat
input.copyTo(output(offset_rect));

//cv::imwrite("output.png", output);

//加载输入图像
cv::Mat input=cv::imread(“zero.png”);
if(input.empty())
{

std::cout使用以下方法可以轻松获得相同的输出

void copyMakeBorder(InputArray src, OutputArray dst, int top, int bottom, int left, int     right, int borderType, const Scalar& value=Scalar() )

在opencv文档中可以找到一个示例实现

使用以下方法可以轻松获得相同的输出

void copyMakeBorder(InputArray src, OutputArray dst, int top, int bottom, int left, int     right, int borderType, const Scalar& value=Scalar() )

一个例子,可以在OpenCV文档

中找到,我不使用C++,但是在Python下的OpenCV中,图像只是一个数字数组。添加边框是与数组中添加行或列相同的事情。它是用普通数组操作来完成的。C++中OpenCV的行为很可能是相似的。我不这样做。使用C++,但是在Python下的OpenCV中,图像只是一个数字数组。添加边框是将行或列添加到数组中的相同的事情。它是使用普通数组操作来完成的。在任何情况下,C++下的OpenCV的行为都是相似的。只是完美的解决方案!简单的完美解决方案!!