submat(int rowStart, int rowEnd, intcolStart, intcolEnd) 関数の戻り値は行列オブジェクトです。コンテンツは、元の画像のサブマトリックスまたはサブ領域です。
まず、imread を使用して画像を読み取り、行列オブジェクト自体に関する情報を出力します。
org.opencv.core.CvType をインポートします。
org.opencv.core.Mat をインポートします。
org.opencv.core.Core をインポートします。
org.opencv.core.MatOfInt をインポートします。
org.opencv.imgcodecs.Imgcodecs をインポートします。
折り紙をインポートします。折り紙;
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 関数の最初の形式を使用します。
画像のトリミング
org.opencv.core.CvType をインポートします。
org.opencv.core.Mat をインポートします。
org.opencv.core.Core をインポートします。
org.opencv.core.MatOfInt をインポートします。
org.opencv.imgcodecs.Imgcodecs をインポートします。
折り紙をインポートします。折り紙;
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(200, 240, 300, 350); System.out.println(サブマット); } }
ここでサブマットのサイズに注意してください。サイズは元の画像のサイズに基づいています。元の画像のサイズを超えるとエラーが報告されます。エラーは次のとおりです。
次に、トリミングされた画像を出力します。
では、撮影したい範囲の確認、つまりこの4つのパラメータの塗りつぶしをどうやって決めるのでしょうか?次の図を例として見てみましょう
キャプチャした画像
他の 2 つのサブマット メソッド
範囲 (int 行、int 列)
行: 広い開始範囲と終了範囲、列: 高い開始範囲と終了範囲
Mat submat2 = mat.submat(新しい範囲(20,300),新しい範囲(100,500));
Imgcodecs.imwrite("./images/output2.png",submat2);
Rect (int x, int y, int 幅, int 高さ)
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 "、サブマット3);
次のように setTo を開きます。
Imgcodecs.imwrite("./images/blurtest.png",mat);
完全なコード:
org.opencv.core.CvType をインポートします。
org.opencv.core.Scalar をインポートします。
org.opencv.core.Mat をインポートします。
org.opencv.core.Rect をインポートします。
org.opencv.core.Range をインポートします。
org.opencv.core.Core をインポートします。
org.opencv.core.Size をインポートします。
org.opencv.core.MatOfInt をインポートします。
org.opencv.imgcodecs.Imgcodecs をインポートします。
org.opencv.imgproc.Imgproc をインポートします。
折り紙をインポートします。折り紙;
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(200, 400, 200, 550); //System.out.println(submat); Imgcodecs.imwrite("./images/output.png",submat); Mat submat2 = mat.submat(新しい範囲(20,300),新しい範囲(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);
}
}
画像のぼやけ
org.opencv.core.CvType をインポートします。
org.opencv.core.Mat をインポートします。
org.opencv.core.Core をインポートします。
org.opencv.core.Size をインポートします。
org.opencv.core.MatOfInt をインポートします。
org.opencv.imgcodecs.Imgcodecs をインポートします。
org.opencv.imgproc.Imgproc をインポートします。
折り紙をインポートします。折り紙;
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(200, 400, 200, 550); //System.out.println(submat); //Imgcodecs.imwrite("./images/output.png",submat); Imgproc.blur(サブマット,サブマット,新しいサイズ(25.0,25.0)); System.out.println("after:"+mat); Imgcodecs.imwrite("./images/blurtest.png",mat); } }
部分行列生成行列
setTo と copyTo は、OpenCv の 2 つの非常に重要な関数です。
setTo は、行列内のすべてのピクセルを指定した色に設定できます
copyTo は、既存の行列を別の行列にコピーできます。
最初の色の値は青の深さを表し、2 番目の値は緑の深さを表し、最後の値は赤の深さを表します。
//赤、緑、青を取得します。スカラー
Red = new Scalar(0,0,255);
Scalar Green = new Scalar(0,255,0);
Scalar Blue = new Scalar(255,0,0);
これらの色は RGB の補色として扱われます。したがって、他のチャンネルを最大値の 255 に設定し、メイン チャンネルを 0 に設定します。シアンは赤の補色であるため、赤の値チャネルは 0 に設定され、他の 2 つのチャネルは 255 に設定されます。
シアン、マゼンタ、イエローを定義する
スカラーシアン = new Scalar(255,255,0);
スカラー magena= new Scalar(255,0,255);
スカラー黄色 = 新しいスカラー(0,255,255);
以下では、setTo を使用して部分行列を特定のスカラー色に設定します。
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(シアン); }if (i==1){ sub.setTo(magena);
}if (i==2){ sub.setTo( yellow); } } } }
次に、3 つのカラー チャネルのマトリックスを作成し、その 1 行目と 2 行目を次のように塗りつぶします。
完全なコード:
org.opencv.core.CvType をインポートします。
org.opencv.core.Scalar をインポートします。
org.opencv.core.Mat をインポートします。
org.opencv.core.Rect をインポートします。
org.opencv.core.Range をインポートします。
org.opencv.core.Core をインポートします。
org.opencv.core.Size をインポートします。
org.opencv.core.MatOfInt をインポートします。
org.opencv.imgcodecs.Imgcodecs をインポートします。
org.opencv.imgproc.Imgproc をインポートします。
折り紙をインポートします。折り紙;
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 シアン = new Scalar(255,255,0); public static Scalar magena= new Scalar(255,0,255); public static スカラー yellow = new Scalar(0,255,255); public static void main(String[] args) throws Exception { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); マット mat = 新しい 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(シアン); }if (i==1){ submat.setTo(magena);
}if (i==2){ submat.setTo( yellow); } } } } }
画像部分行列から行列を生成
まず、サイズ 200x200 の行列と部分行列を作成します。1 つはメイン行列の上部用、もう 1 つはメイン行列の下部用です。
int 幅 = 200、高さ = 200;
マット mat1 = 新しい Mat(高さ,幅,CvType.CV_8UC3);
マットトップ = mat.submat(0,高さ/2,0,幅);
マット底部 = mat.submat(高さ/2,高さ,0,幅);
次に、画像をロードして別の小さな行列を作成し、それを上部 (または下部) サブ行列のサイズにサイズ変更します。ここではImgprocクラスのリサイズ機能について紹介します。
完全なコード:
org.opencv.core.CvType をインポートします。
org.opencv.core.Scalar をインポートします。
org.opencv.core.Mat をインポートします。
org.opencv.core.Rect をインポートします。
org.opencv.core.Range をインポートします。
org.opencv.core.Core をインポートします。
org.opencv.core.Size をインポートします。
org.opencv.core.MatOfInt をインポートします。
org.opencv.imgcodecs.Imgcodecs をインポートします。
org.opencv.imgproc.Imgproc をインポートします。
折り紙をインポートします。折り紙;
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 シアン = new Scalar(255,255,0); public static Scalar magena= new Scalar(255,0,255); public static スカラー yellow = new Scalar(0,255,255); public static void main(String[] args) throws Exception { System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
int 幅 = 200、高さ = 300;
マット mat1 = 新しい Mat(高さ,幅,CvType.CV_8UC3);
マットトップ = mat1.submat(0,高さ/2,0,幅);
マット底部 = mat1.submat(高さ/2,高さ,0,幅);
Mat small = Imgcodecs.imread("./images/test.jpg");
Imgproc.resize(small,small,top.size());
small.copyTo(トップ);
small.copyTo(下);
Imgcodecs.imwrite("./images/matofpictures.png",mat1);
}
注: サイズを設定する手順は重要です。小さな行列と部分行列のサイズがまったく同じであるため、レプリケーションは成功します。したがってレプリケーションに問題はありません。