[JAVA] Bureau: OpenCV Trouver les coins de l'échiquier par WebCam

Goal
Test OpenCV find chessboard corners by webcam.

OpenCV_Undistort.java


import java.util.ArrayList;

import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.MatOfPoint3f;
import org.opencv.core.Point3;
import org.opencv.core.Size;
import org.opencv.core.TermCriteria;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class OpenCV_Undistort {
    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
    public static void main( String[] args )
    {
        try{
            Mat source = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\005.jpg ",
                    Imgcodecs.CV_LOAD_IMAGE_COLOR);
            Mat destination=source.clone();
            // Mat destination1=new Mat();
            Imgproc.cvtColor(source, 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){
                TermCriteria term =new TermCriteria(TermCriteria.EPS|TermCriteria.MAX_ITER,30,0.1);
                Imgproc.cornerSubPix(destination, imageCorners, new Size(11,11), new Size(-1,-1),term);
                Calib3d.drawChessboardCorners(source, boarderSize, imageCorners, found);

            }

            ArrayList<Mat> rvecs = new ArrayList<Mat>();
            ArrayList<Mat> tvecs = new ArrayList<Mat>();
            ArrayList<Mat> objectPoints=new ArrayList<Mat>();
            ArrayList<Mat> imagePoints=new ArrayList<Mat>();
            MatOfPoint3f obj=new MatOfPoint3f();
            Mat cameraMatrix=new Mat(3,3,CvType.CV_32FC1);
            cameraMatrix.put(0, 0, 1);
            cameraMatrix.put(1, 1, 1);
            Mat distCoeffs=new Mat();
            Mat undistort=new Mat(source.size(), source.type());

            for(int j=0;j<54;j++){
                //obj.push_back(new MatOfPoint3f(new Point3(j/6,j%9,0.0f)));
                obj.push_back(new MatOfPoint3f(new Point3(j/6,j%9,0.0f)));//º§º¬3,3
            }
            objectPoints.add(obj);
            imagePoints.add(imageCorners);

            Calib3d.calibrateCamera(objectPoints, imagePoints, destination.size(), cameraMatrix, distCoeffs, rvecs, tvecs);
            Imgproc.undistort(source, undistort, cameraMatrix, distCoeffs);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\Calib3d-undistort.jpg ", undistort);



        }catch (Exception e) {
            System.out.println("error: " + e.getMessage());
        }
    }
}

Result
![Calib3d-undistort.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/7d52ba82-8904-8850-0a2c-5ce0d42966da.jpeg)

Recommended Posts

Bureau: OpenCV Trouver les coins de l'échiquier par WebCam
Bureau: OpenCV Trouver les coins de l'échiquier par WebCam
Bureau: OpenCV FaceDetector par WebCam
Bureau: OpenCV Show Stylisation par WebCam
Bureau: aperçu de la webcam Opencv
Bureau: OpenCV Find Transform ECC
Bureau: OpenCV Subdiv2d Make Delaunay For Face By WebCam
Bureau: Tutoriel de prévisualisation de la webcam OpenCV
Bureau: vérification du rectangle OpenCV à l'intérieur par ClipLine
Bureau: seuil OpenCV
Bureau: OpenCV BilateralFilterBlur
Bureau: OpenCV Dilate
Bureau: OpenCV Affine
Bureau: OpenCV Emboss
Bureau: OpenCV CLAHE
Bureau: OpenCV Ellipse2Poly
Bureau: OpenCV HDR
Bureau: OpenCV Denoise
Bureau: OpenCV Concat
Bureau: OpenCV Mosaic
Bureau: OpenCV Erode
Bureau: OpenCV Denoise
Bureau: Rectangle OpenCV
Bureau: OpenCV Watershed
Bureau: OpenCV Inpaint
Bureau: OpenCV NormalizeBlur
Bureau: OpenCV StereoSGBM
Bureau: OpenCV Spot
Bureau: OpenCV Canny
Bureau: OpenCV Denoise3
Bureau: histogramme OpenCV
Bureau: OpenCV Dft
Bureau: OpenCV Decolor
Bureau: OpenCV FaceDetector
Bureau: OpenCV Denoise2
Bureau: OpenCV StereoBM
Bureau: Changement d'éclairage OpenCV
Bureau: OpenCV Add WaterMark
Bureau: OpenCV Fill ConvexPoly
Bureau: OpenCV Grab Cut
Bureau: OpenCV Sharpness Gui
Bureau: Changement de couleur OpenCV
Bureau: Filtre Freichennel OpenCV
Bureau: seuil adaptatif OpenCV
Bureau: OpenCV Draw Circle
Bureau: OpenCV Fill Poly
Bureau: OpenCV Mean Filter
Bureau: référentiel Java OpenCV
Bureau: OpenCV Sobel Filter2
Bureau: OpenCV pyrMeanShift Filter
Bureau: Piano virtuel OpenCV
Bureau: image de fusion OpenCV
Bureau: Filtre OpenCV Scharr
Bureau: Flou médian OpenCV
Bureau: OpenCV Add Broad
Bureau: OpenCV Robinson Filter
Bureau: enregistrement vidéo OpenCV
Bureau: Aplatissement de la texture OpenCV
Bureau: OpenCV Flood Fill
Bureau: espace colorimétrique OpenCV
Bureau: Filtre OpenCV SqrBox