Java DIP - 添加边框
在本章中,我们将学习如何为图像添加不同类型的边框。
我们使用 OpenCV 函数 copyMakeBorder。它可以在 Imgproc 包下找到。它的语法如下 −
Imgproc.copyMakeBorder(source,destination,top,bottom,left,right,borderType);
参数如下 −
Sr.No. | 参数 &描述 |
---|---|
1 |
source 源图像。 |
2 |
destination 目标图像。 |
3 |
top 图像顶部边框的长度(以像素为单位)。 |
4 |
bottom 图像底部边框的长度(以像素为单位)图像。 |
5 |
left 图像左侧边框的长度(以像素为单位)。 |
6 |
right 图像右侧边框的长度(以像素为单位)。 |
7 |
borderType 定义边框的类型。可能的边框有 BORDER_REPLICATE、BORDER_REFLECT、BORDER_WRAP、BORDER_CONSTANT 等。 |
除了 copyMakeBorder() 方法之外,Imgproc 类还提供了其他方法。下面简要介绍一下 −
Sr.No. | 方法 &描述 |
---|---|
1 |
cvtColor(Mat src, Mat dst, int code, int dstCn) 它将图像从一个颜色空间转换为另一个颜色空间。 |
2 |
dilate(Mat src, Mat dst, Mat kernel) 它使用特定的结构元素来扩大图像。 |
3 |
equalizeHist(Mat src, Mat dst) 它均衡直方图灰度图像。 |
4 |
filter2D(Mat src, Mat dst, intdepth, Mat kernel, Point anchor, double delta) 它将图像与内核进行卷积。 |
5 |
GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX) 它使用高斯滤波器模糊图像。 |
6 |
integral(Mat src, Mat sum) 计算图像的积分。 |
示例
以下示例演示了如何使用 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; /// 初始化过滤器的参数 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()); } } }
输出
执行给定的代码时,将看到以下输出 −
原始图像

孤立边框图像

包裹边框图像

反射边框图像
