基於OpenCV與JVM實現矩陣處理圖像
submat(int rowStart, int rowEnd, int colStart, int colEnd) 函數的返回值是一個矩陣對象。內容是原圖的子矩陣或子區域。
首先我們用imread來讀取圖片,然後輸出矩陣對象本身的一些信息
import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Core; import org.opencv.core.MatOfInt; import org.opencv.imgcodecs.Imgcodecs; import origami.Origami; public class HelloCv { public static void main(String[] args) throws Exception { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat mat = Imgcodecs.imread("./images/test.jpg",Imgcodecs.IMREAD_GRAYSCALE); System.out.println(mat); } }
由於這個矩陣是原始圖片,所以它的isSubmat是false。
現在我們使用submat函數的第一種形式,輸入參數是每一行和每一列的起始和終止值。
圖片裁剪
import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Core; import org.opencv.core.MatOfInt; import org.opencv.imgcodecs.Imgcodecs; import origami.Origami; public class HelloCv { public static void main(String[] args) throws Exception { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat mat = Imgcodecs.imread("C:/HWKJ/ZRQ/OpenCv/matrixcv/images/test.jpg"); System.out.println(mat); Mat submat = mat.submat(200, 240, 300, 350); System.out.println(submat); } }
這裡註意submat裡的尺寸,尺寸根據原圖的尺寸,超出原圖的尺寸會報錯,報錯如下
然後我們輸出裁剪的圖片。
那麼如何確認你想要截取圖片的區域范圍呢?也就是說怎麼確定這四個參數的填寫?我們以下圖為例
截取後的圖片
另外兩種submat方式
Range(int row,int column)
row
:寬開始結束范圍
column
:高開始結束范圍
Mat submat2 = mat.submat(new Range(20,300),new Range(100,500)); Imgcodecs.imwrite("./images/output2.png",submat2);
Rect(int x, int y,int width, int height)
x:橫坐標
y:縱坐標
width :寬
height:高
Mat submat3 = mat.submat(new Rect(0,200,100,100)); //submat3.setTo(new Scalar(255,0,0));//將圖片繪制為藍色 Imgcodecs.imwrite("./images/output3.png",submat3);
打開setTo如下:
Imgcodecs.imwrite("./images/blurtest.png",mat);
完整代碼:
import org.opencv.core.CvType; import org.opencv.core.Scalar; import org.opencv.core.Mat; import org.opencv.core.Rect; import org.opencv.core.Range; import org.opencv.core.Core; import org.opencv.core.Size; import org.opencv.core.MatOfInt; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import origami.Origami; public class HelloCv { public static void main(String[] args) throws Exception { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat mat = Imgcodecs.imread("C:/HWKJ/ZRQ/OpenCv/matrixcv/images/test.jpg"); System.out.println(mat); Mat submat = mat.submat(200, 400, 200, 550); //System.out.println(submat); Imgcodecs.imwrite("./images/output.png",submat); Mat submat2 = mat.submat(new Range(20,300),new Range(100,500)); Imgcodecs.imwrite("./images/output2.png",submat2); Mat submat3 = mat.submat(new Rect(0,200,400,200)); submat3.setTo(new Scalar(255,0,0)); Imgcodecs.imwrite("./images/output3.png",submat3); //Imgproc.blur(submat,submat,new Size(25.0,25.0)); Imgcodecs.imwrite("./images/blurtest.png",mat); } }
圖片模糊處理
import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Core; import org.opencv.core.Size; import org.opencv.core.MatOfInt; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import origami.Origami; public class HelloCv { public static void main(String[] args) throws Exception { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat mat = Imgcodecs.imread("C:/HWKJ/ZRQ/OpenCv/matrixcv/images/test.jpg"); System.out.println(mat); Mat submat = mat.submat(200, 400, 200, 550); //System.out.println(submat); //Imgcodecs.imwrite("./images/output.png",submat); Imgproc.blur(submat,submat,new Size(25.0,25.0)); System.out.println("after:"+mat); Imgcodecs.imwrite("./images/blurtest.png",mat); } }
子矩陣生成矩陣
setTo和copyTo是OpenCv中兩個非常重要的函數。
setTo可以將一個矩陣中的所有像素設置為指定的顏色
copyTo可以將一個已有的矩陣復制到另一個矩陣之中。
第一個顏色值代表藍色的深度,第二個值代表綠色的深度,最後一個值代表紅色的深度。
//獲取紅綠藍 Scalar Red = new Scalar(0,0,255); Scalar Green = new Scalar(0,255,0); Scalar Blue = new Scalar(255,0,0);
我們把這些顏色當作RGB的補充色。因此把其他通道設置為最大值255,主通道設置為0。藍綠色是紅色的補充色,所以紅色值通道被設為0,而另外兩個通道為255;
定義藍綠色、品紅色和黃色
Scalar cyan = new Scalar(255,255,0); Scalar magena= new Scalar(255,0,255); Scalar yellow = new Scalar(0,255,255);
下面我們使用setTo將子矩陣設置為給定的Scalar顏色
private void setColors(Mat mat ,boolean comp,int row){ for (int i = 0; i <3 ; i++) { Mat sub = mat.submat(row*100,row*100+100,i*100,i*100+100); if(comp){ //RGB if (i==0){ sub.setTo(Red); }if (i==1){ sub.setTo(Green); }if (i==2){ sub.setTo(Blue); } }else { //cmy if (i==0){ sub.setTo(cyan); }if (i==1){ sub.setTo(magena); }if (i==2){ sub.setTo(yellow); } } } }
接下來,我們創建一個包含三個顏色通道矩陣,並且填充它的第一行和第二行
完整代碼:
import org.opencv.core.CvType; import org.opencv.core.Scalar; import org.opencv.core.Mat; import org.opencv.core.Rect; import org.opencv.core.Range; import org.opencv.core.Core; import org.opencv.core.Size; import org.opencv.core.MatOfInt; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import origami.Origami; public class HelloCv1 { public static Scalar Red = new Scalar(0,0,255); public static Scalar Green = new Scalar(0,255,0); public static Scalar Blue = new Scalar(255,0,0); public static Scalar cyan = new Scalar(255,255,0); public static Scalar magena= new Scalar(255,0,255); public static Scalar yellow = new Scalar(0,255,255); public static void main(String[] args) throws Exception { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat mat = new Mat(200,300,CvType.CV_8UC3); setColors(mat,false,1); setColors(mat,true,0); Imgcodecs.imwrite("./images/rgbcmy.png",mat); } static void setColors(Mat mat ,boolean comp,int row){ for (int i = 0; i <3 ; i++) { Mat submat = mat.submat(row*100,row*100+100,i*100,i*100+100); if(comp){ //RGB if (i==0){ submat.setTo(Red); }if (i==1){ submat.setTo(Green); }if (i==2){ submat.setTo(Blue); } }else { //cmy if (i==0){ submat.setTo(cyan); }if (i==1){ submat.setTo(magena); }if (i==2){ submat.setTo(yellow); } } } } }
從圖片子矩陣生成矩陣
首先創建一個大小為200×200的矩陣和子矩陣:一個是主矩陣的上部,一個是主矩陣的下部
int width = 200,height = 200; Mat mat1 = new Mat(height,width,CvType.CV_8UC3); Mat top = mat.submat(0,height/2,0,width); Mat bottom = mat.submat(height/2,height,0,width);
然後加載一個圖片以創建另一個小矩陣,並把它的大小調整為上部(或下部)的子矩陣大小。這裡會引入Imgproc類中的resize函數。
完整代碼:
import org.opencv.core.CvType; import org.opencv.core.Scalar; import org.opencv.core.Mat; import org.opencv.core.Rect; import org.opencv.core.Range; import org.opencv.core.Core; import org.opencv.core.Size; import org.opencv.core.MatOfInt; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import origami.Origami; public class HelloCv1 { public static Scalar Red = new Scalar(0,0,255); public static Scalar Green = new Scalar(0,255,0); public static Scalar Blue = new Scalar(255,0,0); public static Scalar cyan = new Scalar(255,255,0); public static Scalar magena= new Scalar(255,0,255); public static Scalar yellow = new Scalar(0,255,255); public static void main(String[] args) throws Exception { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); int width = 200,height = 300; Mat mat1 = new Mat(height,width,CvType.CV_8UC3); Mat top = mat1.submat(0,height/2,0,width); Mat bottom = mat1.submat(height/2,height,0,width); Mat small = Imgcodecs.imread("./images/test.jpg"); Imgproc.resize(small,small,top.size()); small.copyTo(top); small.copyTo(bottom); Imgcodecs.imwrite("./images/matofpictures.png",mat1); }
註意:設置大小的步驟很關鍵。復制能夠成功,是因為小矩陣和子矩陣的大小是完全相同的,因此復制的時候沒有出現任何問題
以上就是基於OpenCV與JVM實現矩陣處理圖像的詳細內容,更多關於OpenCV JVM矩陣處理圖像的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- 基於OpenCv與JVM實現加載保存圖像功能(JAVA 圖像處理)
- Java圖像處理之獲取用戶感興趣的區域
- Java OpenCV圖像處理之SIFT角點檢測詳解
- 利用Java+OpenCV實現拍照功能
- Java OpenCV圖像處理之自定義圖像濾波算子