- Java 數字影像處理
- 數字影像處理 - 首頁
- 數字影像處理 - 簡介
- 數字影像處理 - Java BufferedImage 類
- 數字影像處理 - 影像下載與上傳
- 數字影像處理 - 影像畫素
- 數字影像處理 - 灰度轉換
- 數字影像處理 - 增強影像對比度
- 數字影像處理 - 增強影像亮度
- 數字影像處理 - 增強影像銳度
- 數字影像處理 - 影像壓縮技術
- 數字影像處理 - 新增影像邊框
- 數字影像處理 - 影像金字塔
- 數字影像處理 - 基本閾值處理
- 數字影像處理 - 影像形狀轉換
- 數字影像處理 - 高斯濾波器
- 數字影像處理 - 方框濾波器
- 數字影像處理 - 腐蝕與膨脹
- 數字影像處理 - 水印
- 數字影像處理 - 卷積理解
- 數字影像處理 - Prewitt 運算元
- 數字影像處理 - Sobel 運算元
- 數字影像處理 - Kirsch 運算元
- 數字影像處理 - Robinson 運算元
- 數字影像處理 - Laplacian 運算元
- 數字影像處理 - 加權平均濾波器
- 數字影像處理 - 建立縮放效果
- 數字影像處理 - 開源庫
- 數字影像處理 - OpenCV 簡介
- 數字影像處理 - OpenCV 灰度轉換
- 數字影像處理 - 顏色空間轉換
- 數字影像處理 有用資源
- 數字影像處理 - 快速指南
- 數字影像處理 - 有用資源
- 數字影像處理 - 討論
Java 數字影像處理 - 影像形狀轉換
可以使用 OpenCV 輕鬆更改影像形狀。影像可以翻轉、縮放或向四個方向中的任何一個旋轉。
為了更改影像的形狀,我們讀取影像並將其轉換為 Mat 物件。其語法如下所示:
File input = new File("digital_image_processing.jpg");
BufferedImage image = ImageIO.read(input);
//convert Buffered Image to Mat.
翻轉影像
OpenCV 允許三種類型的翻轉程式碼,如下所述:
| 序號 | 翻轉程式碼和描述 |
|---|---|
| 1 |
0 0 表示,圍繞 x 軸翻轉。 |
| 2 |
1 1 表示,圍繞 y 軸翻轉。 |
| 3 |
-1 -1 表示,圍繞 x 軸和 y 軸翻轉。 |
我們將適當的翻轉程式碼傳遞給 **Core** 類中的 **flip()** 方法。其語法如下所示:
Core.flip(source mat, destination mat1, flip_code);
**flip()** 方法接受三個引數:源影像矩陣、目標影像矩陣和翻轉程式碼。
除了 flip 方法外,Core 類還提供了其他方法。簡要描述如下:
| 序號 | 方法和描述 |
|---|---|
| 1 |
add(Mat src1, Mat src2, Mat dst) 它計算兩個陣列或一個數組和一個標量的逐元素和。 |
| 2 |
bitwise_and(Mat src1, Mat src2, Mat dst) 它計算兩個陣列或一個數組和一個標量的逐元素按位與。 |
| 3 |
bitwise_not(Mat src, Mat dst) 它反轉陣列的每個位。 |
| 4 |
circle(Mat img, Point center, int radius, Scalar color) 它繪製一個圓。 |
| 5 |
sumElems(Mat src) 它使用高斯濾波器模糊影像。 |
| 6 |
subtract(Mat src1, Scalar src2, Mat dst, Mat mask) 它計算兩個陣列或陣列和標量之間的逐元素差。 |
示例
以下示例演示瞭如何使用 Core 類翻轉影像:
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
public class Main {
public static void main( String[] args ) {
try {
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
File input = new File("digital_image_processing.jpg");
BufferedImage image = ImageIO.read(input);
byte[] data = ((DataBufferByte) image.getRaster(). getDataBuffer()).getData();
Mat mat = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC3);
mat.put(0, 0, data);
Mat mat1 = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC3);
Core.flip(mat, mat1, -1);
byte[] data1 = new byte[mat1.rows()*mat1.cols()*(int)(mat1.elemSize())];
mat1.get(0, 0, data1);
BufferedImage image1 = new BufferedImage(mat1.cols(), mat1.rows(), 5);
image1.getRaster().setDataElements(0,0,mat1.cols(),mat1.rows(),data1);
File ouptut = new File("hsv.jpg");
ImageIO.write(image1, "jpg", ouptut);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
輸出
執行以上示例時,它會將名為 **digital_image_processing.jpg** 的影像翻轉到其等效的 HSV 顏色空間影像,並將其以 **flip.jpg** 的名稱寫入硬碟。
原始影像
翻轉後的影像
廣告