[JAVA] Desktop: OpenCV StereoSGBM

Goal
Test OpenCV StereoSGBM.

OpenCV_StereoSGBM.java


import org.opencv.calib3d.StereoBM;
import org.opencv.calib3d.StereoSGBM;
import org.opencv.core.Core;
import org.opencv.core.Core.MinMaxLocResult;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

public class OpenCV_StereoSGBM {
    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
    //SGBM(Semi-global block matching), BM(Block matching)
    public static void main(String[] args) {
        Mat imgLeft = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\scene_l.bmp",Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
        Mat imgRight = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\scene_r.bmp",Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);


        //Founding self-correcting image piece
        Mat imgDisparity16S = new Mat( imgLeft.rows(), imgLeft.cols(), CvType.CV_16S );
        Mat imgDisparity8U = new Mat( imgLeft.rows(), imgLeft.cols(), CvType.CV_8UC1 );


        //Preparation Stereo BM Constructor Demand Demand
        int ndisparities = 16*5;   //Parallax range
        int SADWindowSize = 21; //block window Block window Large and small,Odd number

        //Computational parallax image
        StereoSGBM sgbm = StereoSGBM.create( 0,32,10);
        //StereoSGBM sgbm = StereoSGBM.create( 0,16,3);
        sgbm.compute( imgLeft, imgRight, imgDisparity16S );

        //檢查 extreme 值
        Core.MinMaxLocResult minMaxVal=new Core.MinMaxLocResult();
        double minVal; double maxVal;


        minMaxVal=Core.minMaxLoc( imgDisparity16S);
        minVal= minMaxVal.minVal;
        maxVal=minMaxVal.maxVal;

        System.out.println("Min value="+minVal+", Max value="+maxVal);

        //CV used_8UC1 Formal image
        imgDisparity16S.convertTo( imgDisparity8U, CvType.CV_8UC1, 255/(maxVal - minVal));

        Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\SGBM_sample.png ", imgDisparity16S);
    }
}
Result
![SGBM_sample.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/f89c10e1-f5f9-8b64-6a34-ad993f4e3789.png)

Recommended Posts

Desktop: OpenCV StereoSGBM
Desktop: OpenCV Threshold
Desktop: OpenCV BilateralFilterBlur
Desktop: OpenCV Dilate
Desktop: OpenCV Expand
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV Ellipse2Poly
Desktop: OpenCV HDR
Desktop: OpenCV Polylines
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 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 Freichennel Filter
Desktop: OpenCV Adaptive Threshold
Desktop: OpenCV Fill Poly
Desktop: OpenCV Mean Filter
Desktop: OpenCV Java Repository
Desktop: OpenCV Sobel Filter2
Desktop: OpenCV pyrMeanShift Filter
Desktop: OpenCV OpticalFlow PyrLK
Desktop: OpenCV Virtual Piano
Desktop: OpenCV merge Picture
Desktop: OpenCV Scharr Filter
Desktop: Opencv webcam preview
Desktop: OpenCV Laplacian 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
Desktop: OpenCV resize image
Desktop: OpenCV Pencil Sketch