📅  最后修改于: 2020-12-14 05:38:11             🧑  作者: Mango
在本章中,我们学习向图像添加不同类型的边框。
我们使用OpenCV函数copyMakeBorder 。可以在Imgproc软件包下找到。其语法如下-
Imgproc.copyMakeBorder(source,destination,top,bottom,left,right,borderType);
参数说明如下-
Sr.No. | Parameter & Description |
---|---|
1 |
source It is source image. |
2 |
destination It is destination image. |
3 |
top It is the length in pixels of the border at the top of the image. |
4 |
bottom Length in pixels of the border at the bottom of the image. |
5 |
left It is the length in pixels of the border at the left of the image. |
6 |
right It is the length in pixels of the border at the right of the image. |
7 |
borderType It defines the type of border. The possible borders are BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_CONSTANT etc. |
除了copyMakeBorder()方法之外,Imgproc类还提供其他方法。他们简要描述-
Sr.No. | Method & Description |
---|---|
1 |
cvtColor(Mat src, Mat dst, int code, int dstCn) It converts an image from one color space to another. |
2 |
dilate(Mat src, Mat dst, Mat kernel) It dilates an image by using a specific structuring element. |
3 |
equalizeHist(Mat src, Mat dst) It equalizes the histogram of a grayscale image. |
4 |
filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta) It convolves an image with the kernel. |
5 |
GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX) It blurs an image using a Gaussian filter. |
6 |
integral(Mat src, Mat sum) It calculates the integral of an image. |
以下示例演示了如何使用Imgproc类向图像添加边框-
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());
}
}
}
当您执行给定的代码时,将看到以下输出-