[JAVA] Desktop: OpenCV Find Chessboard Corners By WebCam

Goal
Test OpenCV find chessboard corners by webcam.

OpenCV_FindChessboardCornersByWebCam.java


import javax.swing.JFrame;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Size;
import org.opencv.core.TermCriteria;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;

public class OpenCV_FindChessboardCornersByWebCam {
    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
    public static void main(String arg[]) throws Exception{
        // It is better to group all frames together so cut and paste to
        // create more frames is easier
        JFrame frame1 = new JFrame("Detective checkerboard Live version");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setSize(640,480);
        frame1.setBounds(0, 0, frame1.getWidth(), frame1.getHeight());
        Panel panel1 = new Panel();
        frame1.setContentPane(panel1);
        frame1.setVisible(true);

        //-- 2. Read the video stream
        VideoCapture capture = new VideoCapture();
        capture.open(0);
        Mat webcam_image=new Mat();

        capture.read(webcam_image);
        frame1.setSize(webcam_image.width()+40,webcam_image.height()+60);

        if( capture.isOpened())
        {
            while( true )
            {
                Thread.sleep(200);


                capture.read(webcam_image);
                if( !webcam_image.empty() ){

                    Mat destination=webcam_image.clone();
                    Imgproc.cvtColor(destination, destination, Imgproc.COLOR_BGR2GRAY);
                    Size boarderSize=new Size(9,6);
                    MatOfPoint2f imageCorners=new MatOfPoint2f();
                    boolean found=Calib3d.findChessboardCorners(destination, boarderSize, imageCorners,Calib3d.CALIB_CB_ADAPTIVE_THRESH+Calib3d.CALIB_CB_NORMALIZE_IMAGE+Calib3d.CALIB_CB_FAST_CHECK);
                    if (found){
                        Imgproc.cornerSubPix(destination, imageCorners, new Size(11,11), new Size(-1,-1),new TermCriteria(TermCriteria.EPS|TermCriteria.MAX_ITER,30,0.1));
                        Calib3d.drawChessboardCorners(webcam_image, boarderSize, imageCorners, found);
                    }
                    panel1.setimagewithMat(webcam_image);
                    frame1.repaint();
                }
                else
                {
                    System.out.println("No supplementary appointment!");
                    break;
                }
            }
        }
        return;
    }

}
Result
![test-Calib3d-2.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/77b15c1b-347c-8e7b-16d7-c7df09c1ba92.jpeg)

Recommended Posts

Desktop: OpenCV Find Chessboard Corners By WebCam
Desktop: OpenCV Find Chessboard Corners By WebCam
Desktop: OpenCV FaceDetector By WebCam
Desktop: OpenCV Show Stylization By WebCam
Desktop: Opencv webcam preview
Desktop: OpenCV Find Transform ECC
Desktop: OpenCV Subdiv2d Make Delaunay For Face By WebCam
Desktop: OpenCV webcam preview tutorial
Desktop: OpenCV Rectangle Check Inside By ClipLine
Desktop: OpenCV Threshold
Desktop: OpenCV BilateralFilterBlur
Desktop: OpenCV Expand
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV CLAHE
Desktop: OpenCV Ellipse2Poly
Desktop: OpenCV HDR
Desktop: OpenCV Polylines
Desktop: OpenCV Denoise
Desktop: OpenCV Sharpness
Desktop: OpenCV Concat
Desktop: OpenCV OpenCV_SalonUseBlurAddWeighted
Desktop: OpenCV Mosaic
Desktop: OpenCV Erode
Desktop: OpenCV Denoise
Desktop: OpenCV Rectangle
Desktop: OpenCV Watershed
Desktop: OpenCV Text
Desktop: OpenCV Inpaint
Desktop: OpenCV NormalizeBlur
Desktop: OpenCV StereoSGBM
Desktop: OpenCV Spot
Desktop: OpenCV Canny
Desktop: OpenCV Denoise3
Desktop: OpenCV Histogram
Desktop: OpenCV Dft
Desktop: OpenCV Decolor
Desktop: OpenCV FaceDetector
Desktop: OpenCV Denoise2
Desktop: OpenCV StereoBM
Desktop: OpenCV Kirsch Filter
Desktop: OpenCV Laplacian Filter 2
Desktop: OpenCV Illumination Change
Desktop: OpenCV Add WaterMark
Desktop: OpenCV Fill ConvexPoly
Desktop: OpenCV Grab Cut
Desktop: OpenCV Sharpness Gui
Desktop: OpenCV Color Change
Desktop: OpenCV Adaptive Threshold
Desktop: OpenCV Draw Circle
Desktop: OpenCV Fill Poly
Desktop: OpenCV Mean Filter
Desktop: OpenCV Java Repository
Desktop: OpenCV pyrMeanShift Filter
Desktop: OpenCV OpticalFlow PyrLK
Desktop: OpenCV Virtual Piano
Desktop: OpenCV merge Picture
Desktop: OpenCV Scharr Filter
Desktop: OpenCV Laplacian Filter
Desktop: OpenCV Median Blur
Desktop: OpenCV Add Broad