In this chapter we learn to add different types of borders to an image.

We use OpenCV function copyMakeBorder. It can be found under Imgproc package. Its syntax is given below −

Imgproc.copyMakeBorder(source,destination,top,bottom,left,right,borderType);

The parameters are described below −

Sr.No.Parameter & Description
1sourceIt is source image.
2destinationIt is destination image.
3topIt is the length in pixels of the border at the top of the image.
4bottomLength in pixels of the border at the bottom of the image.
5leftIt is the length in pixels of the border at the left of the image.
6rightIt is the length in pixels of the border at the right of the image.
7borderTypeIt defines the type of border. The possible borders are BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_CONSTANT etc.

Apart from the copyMakeBorder() method, there are other methods provide by the Imgproc class. They are described briefly −

Sr.No.Method & Description
1cvtColor(Mat src, Mat dst, int code, int dstCn)It converts an image from one color space to another.
2dilate(Mat src, Mat dst, Mat kernel)It dilates an image by using a specific structuring element.
3equalizeHist(Mat src, Mat dst)It equalizes the histogram of a grayscale image.
4filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta)It convolves an image with the kernel.
5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)It blurs an image using a Gaussian filter.
6integral(Mat src, Mat sum)It calculates the integral of an image.

Example

The following example demonstrates the use of Imgproc class to add border to an image −

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class main {
   public static void main( String[] args ) {
   
      try {
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("digital_image_processing.jpg",
         
         Highgui.CV_LOAD_IMAGE_COLOR);
         Mat destination = new Mat(source.rows(),source.cols(),source.type());
         
         int top, bottom, left, right;
         int borderType;

         /// Initialize arguments for the filter
         top = (int) (0.05*source.rows()); 
         bottom = (int) (0.05*source.rows());
         left = (int) (0.05*source.cols()); 
         right = (int) (0.05*source.cols());

         destination = source;
         Imgproc.copyMakeBorder(source, destination, top, bottom, left, right, Imgproc.BORDER_WRAP);
         Highgui.imwrite("borderWrap.jpg", destination);
         
      } catch (Exception e) {
         System.out.println("error: " + e.getMessage());
      }
   }
}

Output

When you execute the given code, the following output is seen −

Original Image

Adding Border Tutorial

Isolated Border Image

Adding Border Tutorial

Wrapped Border Image

Adding Border Tutorial

Reflect Border Image

Adding Border Tutorial

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *