Path: blob/master/samples/java/tutorial_code/ImgProc/HitMiss/HitMiss.java
16354 views
import org.opencv.core.*;1import org.opencv.highgui.HighGui;2import org.opencv.imgproc.Imgproc;34class HitMissRun{56public void run() {7Mat input_image = new Mat( 8, 8, CvType.CV_8UC1 );8int row = 0, col = 0;9input_image.put(row ,col,100, 0, 0, 0, 0, 0, 0, 0,110, 255, 255, 255, 0, 0, 0, 255,120, 255, 255, 255, 0, 0, 0, 0,130, 255, 255, 255, 0, 255, 0, 0,140, 0, 255, 0, 0, 0, 0, 0,150, 0, 255, 0, 0, 255, 255, 0,160, 255, 0, 255, 0, 0, 255, 0,170, 255, 255, 255, 0, 0, 0, 0);1819Mat kernel = new Mat( 3, 3, CvType.CV_16S );20kernel.put(row ,col,210, 1, 0,221, -1, 1,230, 1, 0 );2425Mat output_image = new Mat();26Imgproc.morphologyEx(input_image, output_image, Imgproc.MORPH_HITMISS, kernel);2728int rate = 50;29Core.add(kernel, new Scalar(1), kernel);30Core.multiply(kernel, new Scalar(127), kernel);31kernel.convertTo(kernel, CvType.CV_8U);3233Imgproc.resize(kernel, kernel, new Size(), rate, rate, Imgproc.INTER_NEAREST);34HighGui.imshow("kernel", kernel);35HighGui.moveWindow("kernel", 0, 0);3637Imgproc.resize(input_image, input_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);38HighGui.imshow("Original", input_image);39HighGui.moveWindow("Original", 0, 200);4041Imgproc.resize(output_image, output_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);42HighGui.imshow("Hit or Miss", output_image);43HighGui.moveWindow("Hit or Miss", 500, 200);4445HighGui.waitKey(0);46System.exit(0);47}48}4950public class HitMiss51{52public static void main(String[] args) {53// load the native OpenCV library54System.loadLibrary(Core.NATIVE_LIBRARY_NAME);55new HitMissRun().run();56}57}585960