- Java 數字影像處理
- DIP - 首頁
- DIP - 簡介
- DIP - Java BufferedImage 類
- DIP - 影像下載與上傳
- DIP - 影像畫素
- DIP - 灰度轉換
- DIP - 增強影像對比度
- DIP - 增強影像亮度
- DIP - 增強影像銳度
- DIP - 影像壓縮技術
- DIP - 新增影像邊框
- DIP - 影像金字塔
- DIP - 基本閾值化
- DIP - 影像形狀轉換
- DIP - 高斯濾波器
- DIP - 方框濾波器
- DIP - 腐蝕與膨脹
- DIP - 水印
- DIP - 理解卷積
- DIP - Prewitt 運算元
- DIP - Sobel 運算元
- DIP - Kirsch 運算元
- DIP - Robinson 運算元
- DIP - Laplacian 運算元
- DIP - 加權平均濾波器
- DIP - 建立縮放效果
- DIP - 開源庫
- DIP - OpenCV 簡介
- DIP - OpenCV 灰度轉換
- DIP - 顏色空間轉換
- DIP 有用資源
- DIP - 快速指南
- DIP - 有用資源
- DIP - 討論
Java 數字影像處理 - 新增邊框
在本節中,我們將學習如何向影像新增不同型別的邊框。
我們使用 **OpenCV** 函式 **copyMakeBorder**。它可以在 **Imgproc** 包中找到。其語法如下所示:
Imgproc.copyMakeBorder(source,destination,top,bottom,left,right,borderType);
引數說明如下:
| 序號 | 引數及描述 |
|---|---|
| 1 |
source 源影像。 |
| 2 |
destination 目標影像。 |
| 3 |
top 影像頂部邊框的畫素長度。 |
| 4 |
bottom 影像底部邊框的畫素長度。 |
| 5 |
left 影像左側邊框的畫素長度。 |
| 6 |
right 影像右側邊框的畫素長度。 |
| 7 |
borderType 定義邊框型別。可能的邊框型別包括 BORDER_REPLICATE、BORDER_REFLECT、BORDER_WRAP、BORDER_CONSTANT 等。 |
除了 copyMakeBorder() 方法外,Imgproc 類還提供了其他方法。它們簡要描述如下:
| 序號 | 方法及描述 |
|---|---|
| 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, int depth, 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;
/// 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());
}
}
}
輸出
執行給定程式碼後,將看到以下輸出:
原始影像
隔離邊框影像
包裹邊框影像
反射邊框影像
廣告