Making borders for extrapolation

As you'll see in this and upcoming chapters, one of the most important issues to handle when dealing with many computer vision algorithms is extrapolation, or to put it simply, the assumption of the non-existing pixels outside of an image. You might be wondering, why would I need to think about the non-existing pixels, and the simplest answer is that there are many computer vision algorithms that work with not just a single pixel, but also with their surrounding pixels. In such cases, when the pixel is in the middle of an image, there are no issues. But for the pixels at the borders of the image (for instance, in the topmost row), some of the surrounding pixels will fall outside of the image. That is exactly where you need to think about extrapolation and the assumption of non-existing pixels. Are you going to simply assume those pixels have zero values? Maybe it's better to assume they have the same value as the border pixels? These questions are all taken care of in an OpenCV function called copyMakeBorder.

copyMakeBorder allows us to form borders outside an image and provides enough customizations to deal with all possible scenarios. Let's see how copyMakeBorder is used with a couple of simple examples:

int top = 50; 
int bottom = 50; 
int left = 50; 
int right = 50; 
BorderTypes border = BORDER_REPLICATE; 
copyMakeBorder(image, 
               result, 
               top, 
               bottom, 
               left, 
               right, 
               border); 

As seen in the preceding example, copyMakeBorder accepts an input image and produces a result image, just like most of the OpenCV functions we've learned about so far. In addition, this function must be provided with four integer values that represent the number of pixels that are added to the top, bottom, left, and right side of the image. However, the most important parameter that must be provided here is the border type parameter, which must be an entry of the BorderTypes enum. Here are a few of the most commonly used BorderType values:

  • BORDER_CONSTANT
  • BORDER_REPLICATE
  • BORDER_REFLECT
  • BORDER_WRAP

Note that when BORDER_CONSTANT is used as the border type parameter, an additional scalar parameter must be provided to the copyMakeBorder function, which represents the constant color value of the created border. If this value is omitted, zero (or black) is assumed. The following image demonstrates the output of the copyMakeBorder function when it is executed on our example image:

copyMakeBorder, and many other OpenCV functions, use the borderInterpolate function, internally, to calculate the position of the donor pixel used for the extrapolation and creating the non-existing pixels. You won't need to call this function directly yourself, so we'll leave it for you to explore and discover on your own.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.98.207