OpenCVで画像をマスキングすることとは何ですか?
マスク操作では、画像の各ピクセルの値は、指定されたマスクマトリックスに基づいて再計算されます。これは、カーネルと呼ばれます。マスキングは、フィルタリングとも呼ばれます。
filter2D() Imgprocのメソッド クラスは、ソース、宛先、およびカーネルマトリックスを受け入れ、ソースマトリックスをカーネルマトリックスと畳み込みます。この方法を使用すると、画像をマスクまたはフィルタリングできます。
例
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class MaskingExample {
public static void main( String[] args ) {
//Loading the OpenCV core library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
//Reading the input image
String file = "D://images//boy.jpg";
Mat src = Imgcodecs.imread(file);
//Creating an empty matrix to store the result
Mat dst = new Mat();
//Creating kernel1
Mat kernel1 = new Mat(3, 3, CvType.CV_8S);
int row = 0, col = 0;
kernel1.put(row, col, 0, -1, 0, -1, 5, -1, 0, -1, 0);
//Creating kernel2
Mat kernel2 = Mat.ones(2,2, CvType.CV_32F);
for(int i = 0; i<kernel2.rows(); i++) {
for(int j = 0; j<kernel2.cols(); j++) {
double[] m = kernel2.get(i, j);
for(int k = 1; k<m.length; k++) {
m[k] = m[k]/(2 * 2);
}
kernel2.put(i,j, m);
}
}
//Filtering the image using kernel1
Imgproc.filter2D(src, dst, -1, kernel1);
HighGui.imshow("Mask Example1", dst);
dst = new Mat();
//Filtering the image using kernel2
Imgproc.filter2D(src, dst, -1, kernel2);
HighGui.imshow("Mask Example2", dst);
HighGui.waitKey();
}
} 入力画像
出力
上記のプログラムを実行すると、次のウィンドウが生成されます-
Kernel1 −
Kernel2 −
-
OpenCVを使用して画像に楕円を描画します
このプログラムでは、OpenCVライブラリを使用して画像に楕円を描画します。同じようにOpenCV関数ellipse()を使用します。 元の画像 アルゴリズム Step 1: Import cv2. Step 2: Read the image using imread(). Step 3: Set the center coordinates. Step 4: Set the axes length. Step 5: Set the angle. Step 6: Set start and end angle. Step 6: Set the color. Step 7: Set th
-
OpenCVを使用して画像に線を引く
このプログラムでは、OpenCV関数line()を使用して画像に単純な線を描画します。 元の画像 アルゴリズム Step 1: Import cv2. Step 2: Read the image using imread(). Step 3: Get the dimensions of the image using the image.shape method. Step 4: Define starting point of the line. Step 5: Define the end point of the line. Step 6: Define the thicknes