feat: 切换后端至PaddleOCR-NCNN,切换工程为CMake
1.项目后端整体迁移至PaddleOCR-NCNN算法,已通过基本的兼容性测试 2.工程改为使用CMake组织,后续为了更好地兼容第三方库,不再提供QMake工程 3.重整权利声明文件,重整代码工程,确保最小化侵权风险 Log: 切换后端至PaddleOCR-NCNN,切换工程为CMake Change-Id: I4d5d2c5d37505a4a24b389b1a4c5d12f17bfa38c
This commit is contained in:
@ -0,0 +1,186 @@
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
class GeometricDrawingRun{
|
||||
|
||||
private static final int W = 400;
|
||||
|
||||
public void run(){
|
||||
//! [create_images]
|
||||
/// Windows names
|
||||
String atom_window = "Drawing 1: Atom";
|
||||
String rook_window = "Drawing 2: Rook";
|
||||
|
||||
/// Create black empty images
|
||||
Mat atom_image = Mat.zeros( W, W, CvType.CV_8UC3 );
|
||||
Mat rook_image = Mat.zeros( W, W, CvType.CV_8UC3 );
|
||||
//! [create_images]
|
||||
|
||||
//! [draw_atom]
|
||||
/// 1. Draw a simple atom:
|
||||
/// -----------------------
|
||||
MyEllipse( atom_image, 90.0 );
|
||||
MyEllipse( atom_image, 0.0 );
|
||||
MyEllipse( atom_image, 45.0 );
|
||||
MyEllipse( atom_image, -45.0 );
|
||||
|
||||
/// 1.b. Creating circles
|
||||
MyFilledCircle( atom_image, new Point( W/2, W/2) );
|
||||
//! [draw_atom]
|
||||
|
||||
//! [draw_rook]
|
||||
/// 2. Draw a rook
|
||||
/// ------------------
|
||||
/// 2.a. Create a convex polygon
|
||||
MyPolygon( rook_image );
|
||||
|
||||
//! [rectangle]
|
||||
/// 2.b. Creating rectangles
|
||||
Imgproc.rectangle( rook_image,
|
||||
new Point( 0, 7*W/8 ),
|
||||
new Point( W, W),
|
||||
new Scalar( 0, 255, 255 ),
|
||||
-1,
|
||||
8,
|
||||
0 );
|
||||
//! [rectangle]
|
||||
|
||||
/// 2.c. Create a few lines
|
||||
MyLine( rook_image, new Point( 0, 15*W/16 ), new Point( W, 15*W/16 ) );
|
||||
MyLine( rook_image, new Point( W/4, 7*W/8 ), new Point( W/4, W ) );
|
||||
MyLine( rook_image, new Point( W/2, 7*W/8 ), new Point( W/2, W ) );
|
||||
MyLine( rook_image, new Point( 3*W/4, 7*W/8 ), new Point( 3*W/4, W ) );
|
||||
//! [draw_rook]
|
||||
|
||||
/// 3. Display your stuff!
|
||||
HighGui.imshow( atom_window, atom_image );
|
||||
HighGui.moveWindow( atom_window, 0, 200 );
|
||||
HighGui.imshow( rook_window, rook_image );
|
||||
HighGui.moveWindow( rook_window, W, 200 );
|
||||
|
||||
HighGui.waitKey( 0 );
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
/// Function Declaration
|
||||
|
||||
/**
|
||||
* @function MyEllipse
|
||||
* @brief Draw a fixed-size ellipse with different angles
|
||||
*/
|
||||
//! [my_ellipse]
|
||||
private void MyEllipse( Mat img, double angle ) {
|
||||
int thickness = 2;
|
||||
int lineType = 8;
|
||||
int shift = 0;
|
||||
|
||||
Imgproc.ellipse( img,
|
||||
new Point( W/2, W/2 ),
|
||||
new Size( W/4, W/16 ),
|
||||
angle,
|
||||
0.0,
|
||||
360.0,
|
||||
new Scalar( 255, 0, 0 ),
|
||||
thickness,
|
||||
lineType,
|
||||
shift );
|
||||
}
|
||||
//! [my_ellipse]
|
||||
/**
|
||||
* @function MyFilledCircle
|
||||
* @brief Draw a fixed-size filled circle
|
||||
*/
|
||||
//! [my_filled_circle]
|
||||
private void MyFilledCircle( Mat img, Point center ) {
|
||||
int thickness = -1;
|
||||
int lineType = 8;
|
||||
int shift = 0;
|
||||
|
||||
Imgproc.circle( img,
|
||||
center,
|
||||
W/32,
|
||||
new Scalar( 0, 0, 255 ),
|
||||
thickness,
|
||||
lineType,
|
||||
shift );
|
||||
}
|
||||
//! [my_filled_circle]
|
||||
/**
|
||||
* @function MyPolygon
|
||||
* @function Draw a simple concave polygon (rook)
|
||||
*/
|
||||
//! [my_polygon]
|
||||
private void MyPolygon( Mat img ) {
|
||||
int lineType = 8;
|
||||
int shift = 0;
|
||||
|
||||
/** Create some points */
|
||||
Point[] rook_points = new Point[20];
|
||||
rook_points[0] = new Point( W/4, 7*W/8 );
|
||||
rook_points[1] = new Point( 3*W/4, 7*W/8 );
|
||||
rook_points[2] = new Point( 3*W/4, 13*W/16 );
|
||||
rook_points[3] = new Point( 11*W/16, 13*W/16 );
|
||||
rook_points[4] = new Point( 19*W/32, 3*W/8 );
|
||||
rook_points[5] = new Point( 3*W/4, 3*W/8 );
|
||||
rook_points[6] = new Point( 3*W/4, W/8 );
|
||||
rook_points[7] = new Point( 26*W/40, W/8 );
|
||||
rook_points[8] = new Point( 26*W/40, W/4 );
|
||||
rook_points[9] = new Point( 22*W/40, W/4 );
|
||||
rook_points[10] = new Point( 22*W/40, W/8 );
|
||||
rook_points[11] = new Point( 18*W/40, W/8 );
|
||||
rook_points[12] = new Point( 18*W/40, W/4 );
|
||||
rook_points[13] = new Point( 14*W/40, W/4 );
|
||||
rook_points[14] = new Point( 14*W/40, W/8 );
|
||||
rook_points[15] = new Point( W/4, W/8 );
|
||||
rook_points[16] = new Point( W/4, 3*W/8 );
|
||||
rook_points[17] = new Point( 13*W/32, 3*W/8 );
|
||||
rook_points[18] = new Point( 5*W/16, 13*W/16 );
|
||||
rook_points[19] = new Point( W/4, 13*W/16 );
|
||||
|
||||
MatOfPoint matPt = new MatOfPoint();
|
||||
matPt.fromArray(rook_points);
|
||||
|
||||
List<MatOfPoint> ppt = new ArrayList<MatOfPoint>();
|
||||
ppt.add(matPt);
|
||||
|
||||
Imgproc.fillPoly(img,
|
||||
ppt,
|
||||
new Scalar( 255, 255, 255 ),
|
||||
lineType,
|
||||
shift,
|
||||
new Point(0,0) );
|
||||
}
|
||||
//! [my_polygon]
|
||||
/**
|
||||
* @function MyLine
|
||||
* @brief Draw a simple line
|
||||
*/
|
||||
//! [my_line]
|
||||
private void MyLine( Mat img, Point start, Point end ) {
|
||||
int thickness = 2;
|
||||
int lineType = 8;
|
||||
int shift = 0;
|
||||
|
||||
Imgproc.line( img,
|
||||
start,
|
||||
end,
|
||||
new Scalar( 0, 0, 0 ),
|
||||
thickness,
|
||||
lineType,
|
||||
shift );
|
||||
}
|
||||
//! [my_line]
|
||||
}
|
||||
|
||||
public class BasicGeometricDrawing {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new GeometricDrawingRun().run();
|
||||
}
|
||||
}
|
58
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/HitMiss/HitMiss.java
vendored
Normal file
58
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/HitMiss/HitMiss.java
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class HitMissRun{
|
||||
|
||||
public void run() {
|
||||
Mat input_image = new Mat( 8, 8, CvType.CV_8UC1 );
|
||||
int row = 0, col = 0;
|
||||
input_image.put(row ,col,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 255, 255, 255, 0, 0, 0, 255,
|
||||
0, 255, 255, 255, 0, 0, 0, 0,
|
||||
0, 255, 255, 255, 0, 255, 0, 0,
|
||||
0, 0, 255, 0, 0, 0, 0, 0,
|
||||
0, 0, 255, 0, 0, 255, 255, 0,
|
||||
0, 255, 0, 255, 0, 0, 255, 0,
|
||||
0, 255, 255, 255, 0, 0, 0, 0);
|
||||
|
||||
Mat kernel = new Mat( 3, 3, CvType.CV_16S );
|
||||
kernel.put(row ,col,
|
||||
0, 1, 0,
|
||||
1, -1, 1,
|
||||
0, 1, 0 );
|
||||
|
||||
Mat output_image = new Mat();
|
||||
Imgproc.morphologyEx(input_image, output_image, Imgproc.MORPH_HITMISS, kernel);
|
||||
|
||||
int rate = 50;
|
||||
Core.add(kernel, new Scalar(1), kernel);
|
||||
Core.multiply(kernel, new Scalar(127), kernel);
|
||||
kernel.convertTo(kernel, CvType.CV_8U);
|
||||
|
||||
Imgproc.resize(kernel, kernel, new Size(), rate, rate, Imgproc.INTER_NEAREST);
|
||||
HighGui.imshow("kernel", kernel);
|
||||
HighGui.moveWindow("kernel", 0, 0);
|
||||
|
||||
Imgproc.resize(input_image, input_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);
|
||||
HighGui.imshow("Original", input_image);
|
||||
HighGui.moveWindow("Original", 0, 200);
|
||||
|
||||
Imgproc.resize(output_image, output_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);
|
||||
HighGui.imshow("Hit or Miss", output_image);
|
||||
HighGui.moveWindow("Hit or Miss", 500, 200);
|
||||
|
||||
HighGui.waitKey(0);
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class HitMiss
|
||||
{
|
||||
public static void main(String[] args) {
|
||||
// load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new HitMissRun().run();
|
||||
}
|
||||
}
|
67
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/Pyramids/Pyramids.java
vendored
Normal file
67
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/Pyramids/Pyramids.java
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class PyramidsRun {
|
||||
|
||||
String window_name = "Pyramids Demo";
|
||||
|
||||
public void run(String[] args) {
|
||||
/// General instructions
|
||||
System.out.println("\n" +
|
||||
" Zoom In-Out demo \n" +
|
||||
"------------------ \n" +
|
||||
" * [i] -> Zoom [i]n \n" +
|
||||
" * [o] -> Zoom [o]ut \n" +
|
||||
" * [ESC] -> Close program \n");
|
||||
|
||||
//! [load]
|
||||
String filename = ((args.length > 0) ? args[0] : "../data/chicky_512.png");
|
||||
|
||||
// Load the image
|
||||
Mat src = Imgcodecs.imread(filename);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image!");
|
||||
System.out.println("Program Arguments: [image_name -- default ../data/chicky_512.png] \n");
|
||||
System.exit(-1);
|
||||
}
|
||||
//! [load]
|
||||
|
||||
//! [loop]
|
||||
while (true){
|
||||
//! [show_image]
|
||||
HighGui.imshow( window_name, src );
|
||||
//! [show_image]
|
||||
char c = (char) HighGui.waitKey(0);
|
||||
c = Character.toLowerCase(c);
|
||||
|
||||
if( c == 27 ){
|
||||
break;
|
||||
//![pyrup]
|
||||
}else if( c == 'i'){
|
||||
Imgproc.pyrUp( src, src, new Size( src.cols()*2, src.rows()*2 ) );
|
||||
System.out.println( "** Zoom In: Image x 2" );
|
||||
//![pyrup]
|
||||
//![pyrdown]
|
||||
}else if( c == 'o'){
|
||||
Imgproc.pyrDown( src, src, new Size( src.cols()/2, src.rows()/2 ) );
|
||||
System.out.println( "** Zoom Out: Image / 2" );
|
||||
//![pyrdown]
|
||||
}
|
||||
}
|
||||
//! [loop]
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class Pyramids {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new PyramidsRun().run(args);
|
||||
}
|
||||
}
|
101
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/Smoothing/Smoothing.java
vendored
Normal file
101
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/Smoothing/Smoothing.java
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class SmoothingRun {
|
||||
|
||||
/// Global Variables
|
||||
int DELAY_CAPTION = 1500;
|
||||
int DELAY_BLUR = 100;
|
||||
int MAX_KERNEL_LENGTH = 31;
|
||||
|
||||
Mat src = new Mat(), dst = new Mat();
|
||||
String windowName = "Filter Demo 1";
|
||||
|
||||
public void run(String[] args) {
|
||||
|
||||
String filename = ((args.length > 0) ? args[0] : "../data/lena.jpg");
|
||||
|
||||
src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image");
|
||||
System.out.println("Usage: ./Smoothing [image_name -- default ../data/lena.jpg] \n");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
if( displayCaption( "Original Image" ) != 0 ) { System.exit(0); }
|
||||
|
||||
dst = src.clone();
|
||||
if( displayDst( DELAY_CAPTION ) != 0 ) { System.exit(0); }
|
||||
|
||||
/// Applying Homogeneous blur
|
||||
if( displayCaption( "Homogeneous Blur" ) != 0 ) { System.exit(0); }
|
||||
|
||||
//! [blur]
|
||||
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
|
||||
Imgproc.blur(src, dst, new Size(i, i), new Point(-1, -1));
|
||||
displayDst(DELAY_BLUR);
|
||||
}
|
||||
//! [blur]
|
||||
|
||||
/// Applying Gaussian blur
|
||||
if( displayCaption( "Gaussian Blur" ) != 0 ) { System.exit(0); }
|
||||
|
||||
//! [gaussianblur]
|
||||
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
|
||||
Imgproc.GaussianBlur(src, dst, new Size(i, i), 0, 0);
|
||||
displayDst(DELAY_BLUR);
|
||||
}
|
||||
//! [gaussianblur]
|
||||
|
||||
/// Applying Median blur
|
||||
if( displayCaption( "Median Blur" ) != 0 ) { System.exit(0); }
|
||||
|
||||
//! [medianblur]
|
||||
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
|
||||
Imgproc.medianBlur(src, dst, i);
|
||||
displayDst(DELAY_BLUR);
|
||||
}
|
||||
//! [medianblur]
|
||||
|
||||
/// Applying Bilateral Filter
|
||||
if( displayCaption( "Bilateral Blur" ) != 0 ) { System.exit(0); }
|
||||
|
||||
//![bilateralfilter]
|
||||
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
|
||||
Imgproc.bilateralFilter(src, dst, i, i * 2, i / 2);
|
||||
displayDst(DELAY_BLUR);
|
||||
}
|
||||
//![bilateralfilter]
|
||||
|
||||
/// Done
|
||||
displayCaption( "Done!" );
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
int displayCaption(String caption) {
|
||||
dst = Mat.zeros(src.size(), src.type());
|
||||
Imgproc.putText(dst, caption,
|
||||
new Point(src.cols() / 4, src.rows() / 2),
|
||||
Imgproc.FONT_HERSHEY_COMPLEX, 1, new Scalar(255, 255, 255));
|
||||
|
||||
return displayDst(DELAY_CAPTION);
|
||||
}
|
||||
|
||||
int displayDst(int delay) {
|
||||
HighGui.imshow( windowName, dst );
|
||||
int c = HighGui.waitKey( delay );
|
||||
if (c >= 0) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public class Smoothing {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new SmoothingRun().run(args);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
|
||||
class BasicLinearTransforms {
|
||||
private byte saturate(double val) {
|
||||
int iVal = (int) Math.round(val);
|
||||
iVal = iVal > 255 ? 255 : (iVal < 0 ? 0 : iVal);
|
||||
return (byte) iVal;
|
||||
}
|
||||
|
||||
public void run(String[] args) {
|
||||
/// Read image given by user
|
||||
//! [basic-linear-transform-load]
|
||||
String imagePath = args.length > 0 ? args[0] : "../data/lena.jpg";
|
||||
Mat image = Imgcodecs.imread(imagePath);
|
||||
if (image.empty()) {
|
||||
System.out.println("Empty image: " + imagePath);
|
||||
System.exit(0);
|
||||
}
|
||||
//! [basic-linear-transform-load]
|
||||
|
||||
//! [basic-linear-transform-output]
|
||||
Mat newImage = Mat.zeros(image.size(), image.type());
|
||||
//! [basic-linear-transform-output]
|
||||
|
||||
//! [basic-linear-transform-parameters]
|
||||
double alpha = 1.0; /*< Simple contrast control */
|
||||
int beta = 0; /*< Simple brightness control */
|
||||
|
||||
/// Initialize values
|
||||
System.out.println(" Basic Linear Transforms ");
|
||||
System.out.println("-------------------------");
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
System.out.print("* Enter the alpha value [1.0-3.0]: ");
|
||||
alpha = scanner.nextDouble();
|
||||
System.out.print("* Enter the beta value [0-100]: ");
|
||||
beta = scanner.nextInt();
|
||||
}
|
||||
//! [basic-linear-transform-parameters]
|
||||
|
||||
/// Do the operation newImage(i,j) = alpha*image(i,j) + beta
|
||||
/// Instead of these 'for' loops we could have used simply:
|
||||
/// image.convertTo(newImage, -1, alpha, beta);
|
||||
/// but we wanted to show you how to access the pixels :)
|
||||
//! [basic-linear-transform-operation]
|
||||
byte[] imageData = new byte[(int) (image.total()*image.channels())];
|
||||
image.get(0, 0, imageData);
|
||||
byte[] newImageData = new byte[(int) (newImage.total()*newImage.channels())];
|
||||
for (int y = 0; y < image.rows(); y++) {
|
||||
for (int x = 0; x < image.cols(); x++) {
|
||||
for (int c = 0; c < image.channels(); c++) {
|
||||
double pixelValue = imageData[(y * image.cols() + x) * image.channels() + c];
|
||||
/// Java byte range is [-128, 127]
|
||||
pixelValue = pixelValue < 0 ? pixelValue + 256 : pixelValue;
|
||||
newImageData[(y * image.cols() + x) * image.channels() + c]
|
||||
= saturate(alpha * pixelValue + beta);
|
||||
}
|
||||
}
|
||||
}
|
||||
newImage.put(0, 0, newImageData);
|
||||
//! [basic-linear-transform-operation]
|
||||
|
||||
//! [basic-linear-transform-display]
|
||||
/// Show stuff
|
||||
HighGui.imshow("Original Image", image);
|
||||
HighGui.imshow("New Image", newImage);
|
||||
|
||||
/// Wait until user press some key
|
||||
HighGui.waitKey();
|
||||
//! [basic-linear-transform-display]
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class BasicLinearTransformsDemo {
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
new BasicLinearTransforms().run(args);
|
||||
}
|
||||
}
|
@ -0,0 +1,202 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
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;
|
||||
|
||||
class ChangingContrastBrightnessImage {
|
||||
private static int MAX_VALUE_ALPHA = 500;
|
||||
private static int MAX_VALUE_BETA_GAMMA = 200;
|
||||
private static final String WINDOW_NAME = "Changing the contrast and brightness of an image demo";
|
||||
private static final String ALPHA_NAME = "Alpha gain (contrast)";
|
||||
private static final String BETA_NAME = "Beta bias (brightness)";
|
||||
private static final String GAMMA_NAME = "Gamma correction";
|
||||
private JFrame frame;
|
||||
private Mat matImgSrc = new Mat();
|
||||
private JLabel imgSrcLabel;
|
||||
private JLabel imgModifLabel;
|
||||
private JPanel controlPanel;
|
||||
private JPanel alphaBetaPanel;
|
||||
private JPanel gammaPanel;
|
||||
private double alphaValue = 1.0;
|
||||
private double betaValue = 0.0;
|
||||
private double gammaValue = 1.0;
|
||||
private JCheckBox methodCheckBox;
|
||||
private JSlider sliderAlpha;
|
||||
private JSlider sliderBeta;
|
||||
private JSlider sliderGamma;
|
||||
|
||||
public ChangingContrastBrightnessImage(String[] args) {
|
||||
String imagePath = args.length > 0 ? args[0] : "../data/lena.jpg";
|
||||
matImgSrc = Imgcodecs.imread(imagePath);
|
||||
if (matImgSrc.empty()) {
|
||||
System.out.println("Empty image: " + imagePath);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
// Create and set up the window.
|
||||
frame = new JFrame(WINDOW_NAME);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(matImgSrc);
|
||||
addComponentsToPane(frame.getContentPane(), img);
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private void addComponentsToPane(Container pane, Image img) {
|
||||
if (!(pane.getLayout() instanceof BorderLayout)) {
|
||||
pane.add(new JLabel("Container doesn't use BorderLayout!"));
|
||||
return;
|
||||
}
|
||||
|
||||
controlPanel = new JPanel();
|
||||
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.PAGE_AXIS));
|
||||
|
||||
methodCheckBox = new JCheckBox("Do gamma correction");
|
||||
methodCheckBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JCheckBox cb = (JCheckBox) e.getSource();
|
||||
if (cb.isSelected()) {
|
||||
controlPanel.remove(alphaBetaPanel);
|
||||
controlPanel.add(gammaPanel);
|
||||
performGammaCorrection();
|
||||
frame.revalidate();
|
||||
frame.repaint();
|
||||
frame.pack();
|
||||
} else {
|
||||
controlPanel.remove(gammaPanel);
|
||||
controlPanel.add(alphaBetaPanel);
|
||||
performLinearTransformation();
|
||||
frame.revalidate();
|
||||
frame.repaint();
|
||||
frame.pack();
|
||||
}
|
||||
}
|
||||
});
|
||||
controlPanel.add(methodCheckBox);
|
||||
|
||||
alphaBetaPanel = new JPanel();
|
||||
alphaBetaPanel.setLayout(new BoxLayout(alphaBetaPanel, BoxLayout.PAGE_AXIS));
|
||||
alphaBetaPanel.add(new JLabel(ALPHA_NAME));
|
||||
sliderAlpha = new JSlider(0, MAX_VALUE_ALPHA, 100);
|
||||
sliderAlpha.setMajorTickSpacing(50);
|
||||
sliderAlpha.setMinorTickSpacing(10);
|
||||
sliderAlpha.setPaintTicks(true);
|
||||
sliderAlpha.setPaintLabels(true);
|
||||
sliderAlpha.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
alphaValue = sliderAlpha.getValue() / 100.0;
|
||||
performLinearTransformation();
|
||||
}
|
||||
});
|
||||
alphaBetaPanel.add(sliderAlpha);
|
||||
|
||||
alphaBetaPanel.add(new JLabel(BETA_NAME));
|
||||
sliderBeta = new JSlider(0, MAX_VALUE_BETA_GAMMA, 100);
|
||||
sliderBeta.setMajorTickSpacing(20);
|
||||
sliderBeta.setMinorTickSpacing(5);
|
||||
sliderBeta.setPaintTicks(true);
|
||||
sliderBeta.setPaintLabels(true);
|
||||
sliderBeta.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
betaValue = sliderBeta.getValue() - 100;
|
||||
performLinearTransformation();
|
||||
}
|
||||
});
|
||||
alphaBetaPanel.add(sliderBeta);
|
||||
controlPanel.add(alphaBetaPanel);
|
||||
|
||||
gammaPanel = new JPanel();
|
||||
gammaPanel.setLayout(new BoxLayout(gammaPanel, BoxLayout.PAGE_AXIS));
|
||||
gammaPanel.add(new JLabel(GAMMA_NAME));
|
||||
sliderGamma = new JSlider(0, MAX_VALUE_BETA_GAMMA, 100);
|
||||
sliderGamma.setMajorTickSpacing(20);
|
||||
sliderGamma.setMinorTickSpacing(5);
|
||||
sliderGamma.setPaintTicks(true);
|
||||
sliderGamma.setPaintLabels(true);
|
||||
sliderGamma.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
gammaValue = sliderGamma.getValue() / 100.0;
|
||||
performGammaCorrection();
|
||||
}
|
||||
});
|
||||
gammaPanel.add(sliderGamma);
|
||||
|
||||
pane.add(controlPanel, BorderLayout.PAGE_START);
|
||||
JPanel framePanel = new JPanel();
|
||||
imgSrcLabel = new JLabel(new ImageIcon(img));
|
||||
framePanel.add(imgSrcLabel);
|
||||
imgModifLabel = new JLabel(new ImageIcon(img));
|
||||
framePanel.add(imgModifLabel);
|
||||
pane.add(framePanel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private void performLinearTransformation() {
|
||||
Mat img = new Mat();
|
||||
matImgSrc.convertTo(img, -1, alphaValue, betaValue);
|
||||
imgModifLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(img)));
|
||||
frame.repaint();
|
||||
}
|
||||
|
||||
private byte saturate(double val) {
|
||||
int iVal = (int) Math.round(val);
|
||||
iVal = iVal > 255 ? 255 : (iVal < 0 ? 0 : iVal);
|
||||
return (byte) iVal;
|
||||
}
|
||||
|
||||
private void performGammaCorrection() {
|
||||
//! [changing-contrast-brightness-gamma-correction]
|
||||
Mat lookUpTable = new Mat(1, 256, CvType.CV_8U);
|
||||
byte[] lookUpTableData = new byte[(int) (lookUpTable.total()*lookUpTable.channels())];
|
||||
for (int i = 0; i < lookUpTable.cols(); i++) {
|
||||
lookUpTableData[i] = saturate(Math.pow(i / 255.0, gammaValue) * 255.0);
|
||||
}
|
||||
lookUpTable.put(0, 0, lookUpTableData);
|
||||
Mat img = new Mat();
|
||||
Core.LUT(matImgSrc, lookUpTable, img);
|
||||
//! [changing-contrast-brightness-gamma-correction]
|
||||
|
||||
imgModifLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(img)));
|
||||
frame.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public class ChangingContrastBrightnessImageDemo {
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
// Schedule a job for the event dispatch thread:
|
||||
// creating and showing this application's GUI.
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new ChangingContrastBrightnessImage(args);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
159
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/erosion_dilatation/MorphologyDemo1.java
vendored
Normal file
159
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/erosion_dilatation/MorphologyDemo1.java
vendored
Normal file
@ -0,0 +1,159 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class MorphologyDemo1 {
|
||||
private static final String[] ELEMENT_TYPE = { "Rectangle", "Cross", "Ellipse" };
|
||||
private static final String[] MORPH_OP = { "Erosion", "Dilatation" };
|
||||
private static final int MAX_KERNEL_SIZE = 21;
|
||||
private Mat matImgSrc;
|
||||
private Mat matImgDst = new Mat();
|
||||
private int elementType = Imgproc.CV_SHAPE_RECT;
|
||||
private int kernelSize = 0;
|
||||
private boolean doErosion = true;
|
||||
private JFrame frame;
|
||||
private JLabel imgLabel;
|
||||
|
||||
//! [constructor]
|
||||
public MorphologyDemo1(String[] args) {
|
||||
String imagePath = args.length > 0 ? args[0] : "../data/LinuxLogo.jpg";
|
||||
matImgSrc = Imgcodecs.imread(imagePath);
|
||||
if (matImgSrc.empty()) {
|
||||
System.out.println("Empty image: " + imagePath);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
// Create and set up the window.
|
||||
frame = new JFrame("Erosion and dilatation demo");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(matImgSrc);
|
||||
addComponentsToPane(frame.getContentPane(), img);
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
//! [constructor]
|
||||
|
||||
//! [components]
|
||||
private void addComponentsToPane(Container pane, Image img) {
|
||||
if (!(pane.getLayout() instanceof BorderLayout)) {
|
||||
pane.add(new JLabel("Container doesn't use BorderLayout!"));
|
||||
return;
|
||||
}
|
||||
|
||||
JPanel sliderPanel = new JPanel();
|
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
|
||||
|
||||
JComboBox<String> elementTypeBox = new JComboBox<>(ELEMENT_TYPE);
|
||||
elementTypeBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@SuppressWarnings("unchecked")
|
||||
JComboBox<String> cb = (JComboBox<String>)e.getSource();
|
||||
if (cb.getSelectedIndex() == 0) {
|
||||
elementType = Imgproc.CV_SHAPE_RECT;
|
||||
} else if (cb.getSelectedIndex() == 1) {
|
||||
elementType = Imgproc.CV_SHAPE_CROSS;
|
||||
} else if (cb.getSelectedIndex() == 2) {
|
||||
elementType = Imgproc.CV_SHAPE_ELLIPSE;
|
||||
}
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(elementTypeBox);
|
||||
|
||||
sliderPanel.add(new JLabel("Kernel size: 2n + 1"));
|
||||
JSlider slider = new JSlider(0, MAX_KERNEL_SIZE, 0);
|
||||
slider.setMajorTickSpacing(5);
|
||||
slider.setMinorTickSpacing(5);
|
||||
slider.setPaintTicks(true);
|
||||
slider.setPaintLabels(true);
|
||||
slider.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
kernelSize = source.getValue();
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(slider);
|
||||
|
||||
JComboBox<String> morphOpBox = new JComboBox<>(MORPH_OP);
|
||||
morphOpBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@SuppressWarnings("unchecked")
|
||||
JComboBox<String> cb = (JComboBox<String>)e.getSource();
|
||||
doErosion = cb.getSelectedIndex() == 0;
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(morphOpBox);
|
||||
|
||||
pane.add(sliderPanel, BorderLayout.PAGE_START);
|
||||
imgLabel = new JLabel(new ImageIcon(img));
|
||||
pane.add(imgLabel, BorderLayout.CENTER);
|
||||
}
|
||||
//! [components]
|
||||
|
||||
//! [update]
|
||||
private void update() {
|
||||
//! [kernel]
|
||||
Mat element = Imgproc.getStructuringElement(elementType, new Size(2 * kernelSize + 1, 2 * kernelSize + 1),
|
||||
new Point(kernelSize, kernelSize));
|
||||
//! [kernel]
|
||||
|
||||
if (doErosion) {
|
||||
//! [erosion]
|
||||
Imgproc.erode(matImgSrc, matImgDst, element);
|
||||
//! [erosion]
|
||||
} else {
|
||||
//! [dilation]
|
||||
Imgproc.dilate(matImgSrc, matImgDst, element);
|
||||
//! [dilation]
|
||||
}
|
||||
Image img = HighGui.toBufferedImage(matImgDst);
|
||||
imgLabel.setIcon(new ImageIcon(img));
|
||||
frame.repaint();
|
||||
}
|
||||
//! [update]
|
||||
|
||||
//! [main]
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
// Schedule a job for the event dispatch thread:
|
||||
// creating and showing this application's GUI.
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new MorphologyDemo1(args);
|
||||
}
|
||||
});
|
||||
}
|
||||
//! [main]
|
||||
}
|
152
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/morph_lines_detection/Morphology_3.java
vendored
Normal file
152
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/morph_lines_detection/Morphology_3.java
vendored
Normal file
@ -0,0 +1,152 @@
|
||||
/**
|
||||
* @file Morphology_3.java
|
||||
* @brief Use morphology transformations for extracting horizontal and vertical lines sample code
|
||||
*/
|
||||
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class Morphology_3Run {
|
||||
|
||||
public void run(String[] args) {
|
||||
|
||||
//! [load_image]
|
||||
// Check number of arguments
|
||||
if (args.length == 0){
|
||||
System.out.println("Not enough parameters!");
|
||||
System.out.println("Program Arguments: [image_path]");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
// Load the image
|
||||
Mat src = Imgcodecs.imread(args[0]);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image: " + args[0]);
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
// Show source image
|
||||
HighGui.imshow("src", src);
|
||||
//! [load_image]
|
||||
|
||||
//! [gray]
|
||||
// Transform source image to gray if it is not already
|
||||
Mat gray = new Mat();
|
||||
|
||||
if (src.channels() == 3)
|
||||
{
|
||||
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
|
||||
}
|
||||
else
|
||||
{
|
||||
gray = src;
|
||||
}
|
||||
|
||||
// Show gray image
|
||||
showWaitDestroy("gray" , gray);
|
||||
//! [gray]
|
||||
|
||||
//! [bin]
|
||||
// Apply adaptiveThreshold at the bitwise_not of gray
|
||||
Mat bw = new Mat();
|
||||
Core.bitwise_not(gray, gray);
|
||||
Imgproc.adaptiveThreshold(gray, bw, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, -2);
|
||||
|
||||
// Show binary image
|
||||
showWaitDestroy("binary" , bw);
|
||||
//! [bin]
|
||||
|
||||
//! [init]
|
||||
// Create the images that will use to extract the horizontal and vertical lines
|
||||
Mat horizontal = bw.clone();
|
||||
Mat vertical = bw.clone();
|
||||
//! [init]
|
||||
|
||||
//! [horiz]
|
||||
// Specify size on horizontal axis
|
||||
int horizontal_size = horizontal.cols() / 30;
|
||||
|
||||
// Create structure element for extracting horizontal lines through morphology operations
|
||||
Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size,1));
|
||||
|
||||
// Apply morphology operations
|
||||
Imgproc.erode(horizontal, horizontal, horizontalStructure);
|
||||
Imgproc.dilate(horizontal, horizontal, horizontalStructure);
|
||||
|
||||
// Show extracted horizontal lines
|
||||
showWaitDestroy("horizontal" , horizontal);
|
||||
//! [horiz]
|
||||
|
||||
//! [vert]
|
||||
// Specify size on vertical axis
|
||||
int vertical_size = vertical.rows() / 30;
|
||||
|
||||
// Create structure element for extracting vertical lines through morphology operations
|
||||
Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size( 1,vertical_size));
|
||||
|
||||
// Apply morphology operations
|
||||
Imgproc.erode(vertical, vertical, verticalStructure);
|
||||
Imgproc.dilate(vertical, vertical, verticalStructure);
|
||||
|
||||
// Show extracted vertical lines
|
||||
showWaitDestroy("vertical", vertical);
|
||||
//! [vert]
|
||||
|
||||
//! [smooth]
|
||||
// Inverse vertical image
|
||||
Core.bitwise_not(vertical, vertical);
|
||||
showWaitDestroy("vertical_bit" , vertical);
|
||||
|
||||
// Extract edges and smooth image according to the logic
|
||||
// 1. extract edges
|
||||
// 2. dilate(edges)
|
||||
// 3. src.copyTo(smooth)
|
||||
// 4. blur smooth img
|
||||
// 5. smooth.copyTo(src, edges)
|
||||
|
||||
// Step 1
|
||||
Mat edges = new Mat();
|
||||
Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2);
|
||||
showWaitDestroy("edges", edges);
|
||||
|
||||
// Step 2
|
||||
Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1);
|
||||
Imgproc.dilate(edges, edges, kernel);
|
||||
showWaitDestroy("dilate", edges);
|
||||
|
||||
// Step 3
|
||||
Mat smooth = new Mat();
|
||||
vertical.copyTo(smooth);
|
||||
|
||||
// Step 4
|
||||
Imgproc.blur(smooth, smooth, new Size(2, 2));
|
||||
|
||||
// Step 5
|
||||
smooth.copyTo(vertical, edges);
|
||||
|
||||
// Show final result
|
||||
showWaitDestroy("smooth - final", vertical);
|
||||
//! [smooth]
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private void showWaitDestroy(String winname, Mat img) {
|
||||
HighGui.imshow(winname, img);
|
||||
HighGui.moveWindow(winname, 500, 0);
|
||||
HighGui.waitKey(0);
|
||||
HighGui.destroyWindow(winname);
|
||||
}
|
||||
}
|
||||
|
||||
public class Morphology_3 {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new Morphology_3Run().run(args);
|
||||
}
|
||||
}
|
143
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/opening_closing_hats/MorphologyDemo2.java
vendored
Normal file
143
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/opening_closing_hats/MorphologyDemo2.java
vendored
Normal file
@ -0,0 +1,143 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class MorphologyDemo2 {
|
||||
private static final String[] MORPH_OP = { "Opening", "Closing", "Gradient", "Top Hat", "Black Hat" };
|
||||
private static final int[] MORPH_OP_TYPE = { Imgproc.MORPH_OPEN, Imgproc.MORPH_CLOSE,
|
||||
Imgproc.MORPH_GRADIENT, Imgproc.MORPH_TOPHAT, Imgproc.MORPH_BLACKHAT };
|
||||
private static final String[] ELEMENT_TYPE = { "Rectangle", "Cross", "Ellipse" };
|
||||
private static final int MAX_KERNEL_SIZE = 21;
|
||||
private Mat matImgSrc;
|
||||
private Mat matImgDst = new Mat();
|
||||
private int morphOpType = Imgproc.MORPH_OPEN;
|
||||
private int elementType = Imgproc.CV_SHAPE_RECT;
|
||||
private int kernelSize = 0;
|
||||
private JFrame frame;
|
||||
private JLabel imgLabel;
|
||||
|
||||
public MorphologyDemo2(String[] args) {
|
||||
String imagePath = args.length > 0 ? args[0] : "../data/LinuxLogo.jpg";
|
||||
matImgSrc = Imgcodecs.imread(imagePath);
|
||||
if (matImgSrc.empty()) {
|
||||
System.out.println("Empty image: " + imagePath);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
// Create and set up the window.
|
||||
frame = new JFrame("Morphology Transformations demo");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(matImgSrc);
|
||||
addComponentsToPane(frame.getContentPane(), img);
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private void addComponentsToPane(Container pane, Image img) {
|
||||
if (!(pane.getLayout() instanceof BorderLayout)) {
|
||||
pane.add(new JLabel("Container doesn't use BorderLayout!"));
|
||||
return;
|
||||
}
|
||||
|
||||
JPanel sliderPanel = new JPanel();
|
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
|
||||
|
||||
JComboBox<String> morphOpBox = new JComboBox<>(MORPH_OP);
|
||||
morphOpBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@SuppressWarnings("unchecked")
|
||||
JComboBox<String> cb = (JComboBox<String>)e.getSource();
|
||||
morphOpType = MORPH_OP_TYPE[cb.getSelectedIndex()];
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(morphOpBox);
|
||||
|
||||
JComboBox<String> elementTypeBox = new JComboBox<>(ELEMENT_TYPE);
|
||||
elementTypeBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@SuppressWarnings("unchecked")
|
||||
JComboBox<String> cb = (JComboBox<String>)e.getSource();
|
||||
if (cb.getSelectedIndex() == 0) {
|
||||
elementType = Imgproc.CV_SHAPE_RECT;
|
||||
} else if (cb.getSelectedIndex() == 1) {
|
||||
elementType = Imgproc.CV_SHAPE_CROSS;
|
||||
} else if (cb.getSelectedIndex() == 2) {
|
||||
elementType = Imgproc.CV_SHAPE_ELLIPSE;
|
||||
}
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(elementTypeBox);
|
||||
|
||||
sliderPanel.add(new JLabel("Kernel size: 2n + 1"));
|
||||
JSlider slider = new JSlider(0, MAX_KERNEL_SIZE, 0);
|
||||
slider.setMajorTickSpacing(5);
|
||||
slider.setMinorTickSpacing(5);
|
||||
slider.setPaintTicks(true);
|
||||
slider.setPaintLabels(true);
|
||||
slider.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
kernelSize = source.getValue();
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(slider);
|
||||
|
||||
pane.add(sliderPanel, BorderLayout.PAGE_START);
|
||||
imgLabel = new JLabel(new ImageIcon(img));
|
||||
pane.add(imgLabel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private void update() {
|
||||
Mat element = Imgproc.getStructuringElement(elementType, new Size(2 * kernelSize + 1, 2 * kernelSize + 1),
|
||||
new Point(kernelSize, kernelSize));
|
||||
|
||||
Imgproc.morphologyEx(matImgSrc, matImgDst, morphOpType, element);
|
||||
Image img = HighGui.toBufferedImage(matImgDst);
|
||||
imgLabel.setIcon(new ImageIcon(img));
|
||||
frame.repaint();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
// Schedule a job for the event dispatch thread:
|
||||
// creating and showing this application's GUI.
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new MorphologyDemo2(args);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
144
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/threshold/Threshold.java
vendored
Normal file
144
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/threshold/Threshold.java
vendored
Normal file
@ -0,0 +1,144 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.Image;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class Threshold {
|
||||
private static int MAX_VALUE = 255;
|
||||
private static int MAX_TYPE = 4;
|
||||
private static int MAX_BINARY_VALUE = 255;
|
||||
private static final String WINDOW_NAME = "Threshold Demo";
|
||||
private static final String TRACKBAR_TYPE = "<html><body>Type: <br> 0: Binary <br> "
|
||||
+ "1: Binary Inverted <br> 2: Truncate <br> "
|
||||
+ "3: To Zero <br> 4: To Zero Inverted</body></html>";
|
||||
private static final String TRACKBAR_VALUE = "Value";
|
||||
private int thresholdValue = 0;
|
||||
private int thresholdType = 3;
|
||||
private Mat src;
|
||||
private Mat srcGray = new Mat();
|
||||
private Mat dst = new Mat();
|
||||
private JFrame frame;
|
||||
private JLabel imgLabel;
|
||||
|
||||
public Threshold(String[] args) {
|
||||
//! [load]
|
||||
String imagePath = "../data/stuff.jpg";
|
||||
if (args.length > 0) {
|
||||
imagePath = args[0];
|
||||
}
|
||||
// Load an image
|
||||
src = Imgcodecs.imread(imagePath);
|
||||
if (src.empty()) {
|
||||
System.out.println("Empty image: " + imagePath);
|
||||
System.exit(0);
|
||||
}
|
||||
// Convert the image to Gray
|
||||
Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY);
|
||||
//! [load]
|
||||
|
||||
//! [window]
|
||||
// Create and set up the window.
|
||||
frame = new JFrame(WINDOW_NAME);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(srcGray);
|
||||
addComponentsToPane(frame.getContentPane(), img);
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
//! [window]
|
||||
}
|
||||
|
||||
private void addComponentsToPane(Container pane, Image img) {
|
||||
if (!(pane.getLayout() instanceof BorderLayout)) {
|
||||
pane.add(new JLabel("Container doesn't use BorderLayout!"));
|
||||
return;
|
||||
}
|
||||
|
||||
JPanel sliderPanel = new JPanel();
|
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
|
||||
|
||||
//! [trackbar]
|
||||
sliderPanel.add(new JLabel(TRACKBAR_TYPE));
|
||||
// Create Trackbar to choose type of Threshold
|
||||
JSlider sliderThreshType = new JSlider(0, MAX_TYPE, thresholdType);
|
||||
sliderThreshType.setMajorTickSpacing(1);
|
||||
sliderThreshType.setMinorTickSpacing(1);
|
||||
sliderThreshType.setPaintTicks(true);
|
||||
sliderThreshType.setPaintLabels(true);
|
||||
sliderPanel.add(sliderThreshType);
|
||||
|
||||
sliderPanel.add(new JLabel(TRACKBAR_VALUE));
|
||||
// Create Trackbar to choose Threshold value
|
||||
JSlider sliderThreshValue = new JSlider(0, MAX_VALUE, 0);
|
||||
sliderThreshValue.setMajorTickSpacing(50);
|
||||
sliderThreshValue.setMinorTickSpacing(10);
|
||||
sliderThreshValue.setPaintTicks(true);
|
||||
sliderThreshValue.setPaintLabels(true);
|
||||
sliderPanel.add(sliderThreshValue);
|
||||
//! [trackbar]
|
||||
|
||||
//! [on_trackbar]
|
||||
sliderThreshType.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
thresholdType = source.getValue();
|
||||
update();
|
||||
}
|
||||
});
|
||||
|
||||
sliderThreshValue.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
thresholdValue = source.getValue();
|
||||
update();
|
||||
}
|
||||
});
|
||||
//! [on_trackbar]
|
||||
|
||||
pane.add(sliderPanel, BorderLayout.PAGE_START);
|
||||
imgLabel = new JLabel(new ImageIcon(img));
|
||||
pane.add(imgLabel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
//! [Threshold_Demo]
|
||||
private void update() {
|
||||
Imgproc.threshold(srcGray, dst, thresholdValue, MAX_BINARY_VALUE, thresholdType);
|
||||
Image img = HighGui.toBufferedImage(dst);
|
||||
imgLabel.setIcon(new ImageIcon(img));
|
||||
frame.repaint();
|
||||
}
|
||||
//! [Threshold_Demo]
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
// Schedule a job for the event dispatch thread:
|
||||
// creating and showing this application's GUI.
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new Threshold(args);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
259
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java
vendored
Normal file
259
3rdparty/opencv-4.5.4/samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java
vendored
Normal file
@ -0,0 +1,259 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.SwingWorker;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
import org.opencv.videoio.VideoCapture;
|
||||
|
||||
public class ThresholdInRange {
|
||||
private static int MAX_VALUE = 255;
|
||||
private static int MAX_VALUE_H = 360/2;
|
||||
private static final String WINDOW_NAME = "Thresholding Operations using inRange demo";
|
||||
private static final String LOW_H_NAME = "Low H";
|
||||
private static final String LOW_S_NAME = "Low S";
|
||||
private static final String LOW_V_NAME = "Low V";
|
||||
private static final String HIGH_H_NAME = "High H";
|
||||
private static final String HIGH_S_NAME = "High S";
|
||||
private static final String HIGH_V_NAME = "High V";
|
||||
private JSlider sliderLowH;
|
||||
private JSlider sliderHighH;
|
||||
private JSlider sliderLowS;
|
||||
private JSlider sliderHighS;
|
||||
private JSlider sliderLowV;
|
||||
private JSlider sliderHighV;
|
||||
private VideoCapture cap;
|
||||
private Mat matFrame = new Mat();
|
||||
private JFrame frame;
|
||||
private JLabel imgCaptureLabel;
|
||||
private JLabel imgDetectionLabel;
|
||||
private CaptureTask captureTask;
|
||||
|
||||
public ThresholdInRange(String[] args) {
|
||||
int cameraDevice = 0;
|
||||
if (args.length > 0) {
|
||||
cameraDevice = Integer.parseInt(args[0]);
|
||||
}
|
||||
//! [cap]
|
||||
cap = new VideoCapture(cameraDevice);
|
||||
//! [cap]
|
||||
if (!cap.isOpened()) {
|
||||
System.err.println("Cannot open camera: " + cameraDevice);
|
||||
System.exit(0);
|
||||
}
|
||||
if (!cap.read(matFrame)) {
|
||||
System.err.println("Cannot read camera stream.");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
//! [window]
|
||||
// Create and set up the window.
|
||||
frame = new JFrame(WINDOW_NAME);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent windowEvent) {
|
||||
captureTask.cancel(true);
|
||||
}
|
||||
});
|
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(matFrame);
|
||||
addComponentsToPane(frame.getContentPane(), img);
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
//! [window]
|
||||
|
||||
captureTask = new CaptureTask();
|
||||
captureTask.execute();
|
||||
}
|
||||
|
||||
//! [while]
|
||||
private class CaptureTask extends SwingWorker<Void, Mat> {
|
||||
@Override
|
||||
protected Void doInBackground() {
|
||||
Mat matFrame = new Mat();
|
||||
|
||||
while (!isCancelled()) {
|
||||
if (!cap.read(matFrame)) {
|
||||
break;
|
||||
}
|
||||
publish(matFrame.clone());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void process(List<Mat> frames) {
|
||||
Mat frame = frames.get(frames.size() - 1);
|
||||
Mat frameHSV = new Mat();
|
||||
Imgproc.cvtColor(frame, frameHSV, Imgproc.COLOR_BGR2HSV);
|
||||
Mat thresh = new Mat();
|
||||
Core.inRange(frameHSV, new Scalar(sliderLowH.getValue(), sliderLowS.getValue(), sliderLowV.getValue()),
|
||||
new Scalar(sliderHighH.getValue(), sliderHighS.getValue(), sliderHighV.getValue()), thresh);
|
||||
update(frame, thresh);
|
||||
}
|
||||
}
|
||||
//! [while]
|
||||
|
||||
private void addComponentsToPane(Container pane, Image img) {
|
||||
if (!(pane.getLayout() instanceof BorderLayout)) {
|
||||
pane.add(new JLabel("Container doesn't use BorderLayout!"));
|
||||
return;
|
||||
}
|
||||
|
||||
JPanel sliderPanel = new JPanel();
|
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
|
||||
|
||||
//! [trackbar]
|
||||
sliderPanel.add(new JLabel(LOW_H_NAME));
|
||||
sliderLowH = new JSlider(0, MAX_VALUE_H, 0);
|
||||
sliderLowH.setMajorTickSpacing(50);
|
||||
sliderLowH.setMinorTickSpacing(10);
|
||||
sliderLowH.setPaintTicks(true);
|
||||
sliderLowH.setPaintLabels(true);
|
||||
sliderPanel.add(sliderLowH);
|
||||
|
||||
sliderPanel.add(new JLabel(HIGH_H_NAME));
|
||||
sliderHighH = new JSlider(0, MAX_VALUE_H, MAX_VALUE_H);
|
||||
sliderHighH.setMajorTickSpacing(50);
|
||||
sliderHighH.setMinorTickSpacing(10);
|
||||
sliderHighH.setPaintTicks(true);
|
||||
sliderHighH.setPaintLabels(true);
|
||||
sliderPanel.add(sliderHighH);
|
||||
|
||||
sliderPanel.add(new JLabel(LOW_S_NAME));
|
||||
sliderLowS = new JSlider(0, MAX_VALUE, 0);
|
||||
sliderLowS.setMajorTickSpacing(50);
|
||||
sliderLowS.setMinorTickSpacing(10);
|
||||
sliderLowS.setPaintTicks(true);
|
||||
sliderLowS.setPaintLabels(true);
|
||||
sliderPanel.add(sliderLowS);
|
||||
|
||||
sliderPanel.add(new JLabel(HIGH_S_NAME));
|
||||
sliderHighS = new JSlider(0, MAX_VALUE, MAX_VALUE);
|
||||
sliderHighS.setMajorTickSpacing(50);
|
||||
sliderHighS.setMinorTickSpacing(10);
|
||||
sliderHighS.setPaintTicks(true);
|
||||
sliderHighS.setPaintLabels(true);
|
||||
sliderPanel.add(sliderHighS);
|
||||
|
||||
sliderPanel.add(new JLabel(LOW_V_NAME));
|
||||
sliderLowV = new JSlider(0, MAX_VALUE, 0);
|
||||
sliderLowV.setMajorTickSpacing(50);
|
||||
sliderLowV.setMinorTickSpacing(10);
|
||||
sliderLowV.setPaintTicks(true);
|
||||
sliderLowV.setPaintLabels(true);
|
||||
sliderPanel.add(sliderLowV);
|
||||
|
||||
sliderPanel.add(new JLabel(HIGH_V_NAME));
|
||||
sliderHighV = new JSlider(0, MAX_VALUE, MAX_VALUE);
|
||||
sliderHighV.setMajorTickSpacing(50);
|
||||
sliderHighV.setMinorTickSpacing(10);
|
||||
sliderHighV.setPaintTicks(true);
|
||||
sliderHighV.setPaintLabels(true);
|
||||
sliderPanel.add(sliderHighV);
|
||||
//! [trackbar]
|
||||
|
||||
//! [low]
|
||||
sliderLowH.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
int valH = Math.min(sliderHighH.getValue()-1, source.getValue());
|
||||
sliderLowH.setValue(valH);
|
||||
}
|
||||
});
|
||||
//! [low]
|
||||
//! [high]
|
||||
sliderHighH.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
int valH = Math.max(source.getValue(), sliderLowH.getValue()+1);
|
||||
sliderHighH.setValue(valH);
|
||||
}
|
||||
});
|
||||
//! [high]
|
||||
sliderLowS.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
int valS = Math.min(sliderHighS.getValue()-1, source.getValue());
|
||||
sliderLowS.setValue(valS);
|
||||
}
|
||||
});
|
||||
sliderHighS.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
int valS = Math.max(source.getValue(), sliderLowS.getValue()+1);
|
||||
sliderHighS.setValue(valS);
|
||||
}
|
||||
});
|
||||
sliderLowV.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
int valV = Math.min(sliderHighV.getValue()-1, source.getValue());
|
||||
sliderLowV.setValue(valV);
|
||||
}
|
||||
});
|
||||
sliderHighV.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
int valV = Math.max(source.getValue(), sliderLowV.getValue()+1);
|
||||
sliderHighV.setValue(valV);
|
||||
}
|
||||
});
|
||||
|
||||
pane.add(sliderPanel, BorderLayout.PAGE_START);
|
||||
JPanel framePanel = new JPanel();
|
||||
imgCaptureLabel = new JLabel(new ImageIcon(img));
|
||||
framePanel.add(imgCaptureLabel);
|
||||
imgDetectionLabel = new JLabel(new ImageIcon(img));
|
||||
framePanel.add(imgDetectionLabel);
|
||||
pane.add(framePanel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private void update(Mat imgCapture, Mat imgThresh) {
|
||||
//! [show]
|
||||
imgCaptureLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(imgCapture)));
|
||||
imgDetectionLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(imgThresh)));
|
||||
frame.repaint();
|
||||
//! [show]
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
// Schedule a job for the event dispatch thread:
|
||||
// creating and showing this application's GUI.
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new ThresholdInRange(args);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,182 @@
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.Image;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
class MatchTemplateDemoRun implements ChangeListener {
|
||||
|
||||
//! [declare]
|
||||
/// Global Variables
|
||||
Boolean use_mask = false;
|
||||
Mat img = new Mat(), templ = new Mat();
|
||||
Mat mask = new Mat();
|
||||
|
||||
int match_method;
|
||||
|
||||
JLabel imgDisplay = new JLabel(), resultDisplay = new JLabel();
|
||||
//! [declare]
|
||||
|
||||
public void run(String[] args) {
|
||||
if (args.length < 2) {
|
||||
System.out.println("Not enough parameters");
|
||||
System.out.println("Program arguments:\n<image_name> <template_name> [<mask_name>]");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
//! [load_image]
|
||||
/// Load image and template
|
||||
img = Imgcodecs.imread(args[0], Imgcodecs.IMREAD_COLOR);
|
||||
templ = Imgcodecs.imread(args[1], Imgcodecs.IMREAD_COLOR);
|
||||
//! [load_image]
|
||||
|
||||
if (args.length > 2) {
|
||||
use_mask = true;
|
||||
mask = Imgcodecs.imread(args[2], Imgcodecs.IMREAD_COLOR);
|
||||
}
|
||||
|
||||
if (img.empty() || templ.empty() || (use_mask && mask.empty())) {
|
||||
System.out.println("Can't read one of the images");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
matchingMethod();
|
||||
createJFrame();
|
||||
}
|
||||
|
||||
private void matchingMethod() {
|
||||
Mat result = new Mat();
|
||||
|
||||
//! [copy_source]
|
||||
/// Source image to display
|
||||
Mat img_display = new Mat();
|
||||
img.copyTo(img_display);
|
||||
//! [copy_source]
|
||||
|
||||
//! [create_result_matrix]
|
||||
/// Create the result matrix
|
||||
int result_cols = img.cols() - templ.cols() + 1;
|
||||
int result_rows = img.rows() - templ.rows() + 1;
|
||||
|
||||
result.create(result_rows, result_cols, CvType.CV_32FC1);
|
||||
//! [create_result_matrix]
|
||||
|
||||
//! [match_template]
|
||||
/// Do the Matching and Normalize
|
||||
Boolean method_accepts_mask = (Imgproc.TM_SQDIFF == match_method || match_method == Imgproc.TM_CCORR_NORMED);
|
||||
if (use_mask && method_accepts_mask) {
|
||||
Imgproc.matchTemplate(img, templ, result, match_method, mask);
|
||||
} else {
|
||||
Imgproc.matchTemplate(img, templ, result, match_method);
|
||||
}
|
||||
//! [match_template]
|
||||
|
||||
//! [normalize]
|
||||
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
|
||||
//! [normalize]
|
||||
|
||||
//! [best_match]
|
||||
/// Localizing the best match with minMaxLoc
|
||||
Point matchLoc;
|
||||
|
||||
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
|
||||
//! [best_match]
|
||||
|
||||
//! [match_loc]
|
||||
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values.
|
||||
/// For all the other methods, the higher the better
|
||||
if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
|
||||
matchLoc = mmr.minLoc;
|
||||
} else {
|
||||
matchLoc = mmr.maxLoc;
|
||||
}
|
||||
//! [match_loc]
|
||||
|
||||
//! [imshow]
|
||||
/// Show me what you got
|
||||
Imgproc.rectangle(img_display, matchLoc, new Point(matchLoc.x + templ.cols(), matchLoc.y + templ.rows()),
|
||||
new Scalar(0, 0, 0), 2, 8, 0);
|
||||
Imgproc.rectangle(result, matchLoc, new Point(matchLoc.x + templ.cols(), matchLoc.y + templ.rows()),
|
||||
new Scalar(0, 0, 0), 2, 8, 0);
|
||||
|
||||
Image tmpImg = HighGui.toBufferedImage(img_display);
|
||||
ImageIcon icon = new ImageIcon(tmpImg);
|
||||
imgDisplay.setIcon(icon);
|
||||
|
||||
result.convertTo(result, CvType.CV_8UC1, 255.0);
|
||||
tmpImg = HighGui.toBufferedImage(result);
|
||||
icon = new ImageIcon(tmpImg);
|
||||
resultDisplay.setIcon(icon);
|
||||
//! [imshow]
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
if (!source.getValueIsAdjusting()) {
|
||||
match_method = source.getValue();
|
||||
matchingMethod();
|
||||
}
|
||||
}
|
||||
|
||||
private void createJFrame() {
|
||||
String title = "Source image; Control; Result image";
|
||||
JFrame frame = new JFrame(title);
|
||||
frame.setLayout(new GridLayout(2, 2));
|
||||
frame.add(imgDisplay);
|
||||
|
||||
//! [create_trackbar]
|
||||
int min = 0, max = 5;
|
||||
JSlider slider = new JSlider(JSlider.VERTICAL, min, max, match_method);
|
||||
//! [create_trackbar]
|
||||
|
||||
slider.setPaintTicks(true);
|
||||
slider.setPaintLabels(true);
|
||||
|
||||
// Set the spacing for the minor tick mark
|
||||
slider.setMinorTickSpacing(1);
|
||||
|
||||
// Customizing the labels
|
||||
Hashtable<Integer, JLabel> labelTable = new Hashtable<>();
|
||||
labelTable.put(new Integer(0), new JLabel("0 - SQDIFF"));
|
||||
labelTable.put(new Integer(1), new JLabel("1 - SQDIFF NORMED"));
|
||||
labelTable.put(new Integer(2), new JLabel("2 - TM CCORR"));
|
||||
labelTable.put(new Integer(3), new JLabel("3 - TM CCORR NORMED"));
|
||||
labelTable.put(new Integer(4), new JLabel("4 - TM COEFF"));
|
||||
labelTable.put(new Integer(5), new JLabel("5 - TM COEFF NORMED : (Method)"));
|
||||
slider.setLabelTable(labelTable);
|
||||
|
||||
slider.addChangeListener(this);
|
||||
|
||||
frame.add(slider);
|
||||
|
||||
frame.add(resultDisplay);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
public class MatchTemplateDemo {
|
||||
public static void main(String[] args) {
|
||||
// load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
// run code
|
||||
new MatchTemplateDemoRun().run(args);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user