2

I use opencv in c++, and I have a binary image with one object(image 1).

I want add pixels in top, left, right and down of the image(image 3), because I get the skeleton of object with Zhang-Suen algorithm (image 2), and adding the pixels in top, left, right, and down I fix the error visible in image 2, how can i add the 5 px on the edges??.

imagen

i want convert image1 to image3.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • I don't use c++ but, in OpenCV under python, the image is just an array of numbers. Adding borders is the same thing as adding rows or columns to the array. It is done using normal array manipulations. In all likelihood, the behavior of OpenCV under c++ is similar. – John1024 Jul 02 '14 at 21:34

2 Answers2

2

Input image:

enter image description here

// 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);

output image:

enter image description here

This technique has been previously described here.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
2

The same output can be easily obtained using the following method.

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

An example implementation can be found in the opencv documentation here

tybandara
  • 279
  • 2
  • 10