Desktop: OpenCV Find Chessboard Corners By 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

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 Dilate
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV CLAHE
Desktop: OpenCV Ellipse2Poly
Desktop: OpenCV HDR
Desktop: OpenCV Denoise
Desktop: OpenCV Concat
Desktop: OpenCV Mosaic
Desktop: OpenCV Erode
Desktop: OpenCV Denoise
Desktop: OpenCV Rectangle
Desktop: OpenCV Watershed
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 Illumination Change
Desktop: OpenCV Add WaterMark
Desktop: OpenCV Fill ConvexPoly
Desktop: OpenCV Grab Cut
Desktop: OpenCV Sharpness Gui
Desktop: OpenCV Color Change
Desktop: OpenCV Freichennel Filter
Desktop: OpenCV Adaptive Threshold
Desktop: OpenCV Draw Circle
Desktop: OpenCV Fill Poly
Desktop: OpenCV Mean Filter
Desktop: OpenCV Java Repository
Desktop: OpenCV Sobel Filter2
Desktop: OpenCV pyrMeanShift Filter
Desktop: OpenCV Virtual Piano
Desktop: OpenCV merge Picture
Desktop: OpenCV Scharr Filter
Desktop: OpenCV Median Blur
Desktop: OpenCV Add Broad
Desktop: OpenCV Robinson Filter
Desktop: OpenCV record video
Desktop: OpenCV Texture Flatting
Desktop: OpenCV Flood Fill
Desktop: OpenCV color space
Desktop: OpenCV SqrBox Filter