您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關如何使用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); } } } } }
首先創建一個大小為200x200的矩陣和子矩陣:一個是主矩陣的上部,一個是主矩陣的下部
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實現矩陣處理圖像”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。