[JAVA] Desktop: OpenCV HDR

Goal
Test OpenCV HDR.

OpenCV_HDR.java


import java.util.ArrayList;
import java.util.List;

public class OpenCV_HDR {

    public static void main( String[] args )
    {
        try{
            System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
            Mat source1 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\HDRmantis1.jpg ", Imgcodecs.CV_LOAD_IMAGE_COLOR);
            Mat source2 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\HDRmantis2.jpg ", Imgcodecs.CV_LOAD_IMAGE_COLOR);
            Mat source3 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\HDRmantis3.jpg ", Imgcodecs.CV_LOAD_IMAGE_COLOR);
            Mat source4 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\HDRmantis4.jpg ", Imgcodecs.CV_LOAD_IMAGE_COLOR);
            Mat source5 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\HDRmantis5.jpg ", Imgcodecs.CV_LOAD_IMAGE_COLOR);
            Mat source6 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\HDRmantis6.jpg ", Imgcodecs.CV_LOAD_IMAGE_COLOR);
            Mat source7 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\HDRmantis7.jpg ", Imgcodecs.CV_LOAD_IMAGE_COLOR);

            Mat response=new Mat();

            Photo photo=new Photo();
            //CalibrateDebevec calibrate=photo.createCalibrateDebevec();
            CalibrateDebevec calibrateDebevec=photo.createCalibrateDebevec(70,10.0f,false);

            Mat calibrateRobertsonMat=new Mat();
            CalibrateRobertson calibrateRobertson= photo.createCalibrateRobertson(30,0.01f);

            //Load images and exposure times
            List<Mat> listMat=new ArrayList<Mat>();
            listMat.add(source1);
            listMat.add(source2);
            listMat.add(source3);
            listMat.add(source4);
            listMat.add(source5);
            listMat.add(source6);
            listMat.add(source7);


            Mat times=new Mat(7,1, CvType.CV_32FC1);
            times.put(0, 0, 32);
            times.put(1, 0, 16);
            times.put(2, 0, 8);
            times.put(3, 0, 4);
            times.put(4, 0, 1);
            times.put(5, 0, 0.5);
            times.put(6, 0, 0.25);

            /*
            times.put(0, 0, 100);
            times.put(1, 0, 225);
            times.put(2, 0, 325);
            times.put(3, 0, 525);
            times.put(4, 0, 625);
            times.put(5, 0, 785);
            times.put(6, 0, 1024);
             */

            //Estimate camera response
            calibrateDebevec.process(listMat, response, times);
            calibrateRobertson.process(listMat, calibrateRobertsonMat, times);
            System.out.println("response="+response.dump());
            System.out.println("calibrateRobertsonMat="+calibrateRobertsonMat.dump());
            System.out.println("times="+times.dump());


            //Make HDR image
            Mat hdr=new Mat();
            Mat hdrRobertson=new Mat();
            MergeDebevec mergeDebevec=photo.createMergeDebevec();
            mergeDebevec.process(listMat, hdr, times,response);
            mergeDebevec.process(listMat, hdrRobertson, times,calibrateRobertsonMat);
            // System.out.println("hdr="+hdr.dump());




            Mat ldr=new Mat();
            Mat ldrRobertson=new Mat();
            //TonemapDurand tonemapDurand=photo.createTonemapDurand(2.2f,11.5f,31.9f,31.1f,31.1f);
            TonemapDurand tonemapDurand=photo.createTonemapDurand(1.0f,4.0f,1.0f,2.0f,2.0f);
            tonemapDurand.process(hdr, ldr);
            tonemapDurand.process(hdrRobertson, ldrRobertson);

            ldr=EachPixelMultiplyNum(ldr,255);
            ldrRobertson=EachPixelMultiplyNum( ldrRobertson,255);


            //Perform exposure fusion
            Mat fusion=new Mat();
            MergeMertens mergeMertens =photo.createMergeMertens();
            mergeMertens.process(listMat, fusion);
            fusion=EachPixelMultiplyNum(fusion,255);


            Mat mergeRobertsonMat=new Mat();
            MergeRobertson mergeRobertson=  photo.createMergeRobertson();
            mergeRobertson.process(listMat, mergeRobertsonMat, times);
            mergeRobertsonMat=EachPixelMultiplyNum(mergeRobertsonMat,255);


//          List<Mat> alignMTBMatList=new ArrayList<Mat>();
//          AlignMTB  alignMTB=photo.createAlignMTB(6, 4, true);
//          alignMTB.process(listMat, alignMTBMatList, times, response);


            //Tonemap HDR image
            Mat tonemapMat=new Mat();
            Tonemap tonemap= photo.createTonemap(1.0f);
            tonemap.process(hdr, tonemapMat);
            tonemapMat=EachPixelMultiplyNum(tonemapMat,255);

            Mat tonemapDragoMat=new Mat();
            TonemapDrago tonemapDrago=photo.createTonemapDrago(1.0f,1.0f,0.85f);
            tonemapDrago.process(hdr, tonemapDragoMat);
            tonemapDragoMat=EachPixelMultiplyNum(tonemapDragoMat,255);


            Mat tonemapMantiukMat=new Mat();
            TonemapMantiuk tonemapMantiuk=  photo.createTonemapMantiuk(1.0f,0.7f,1.0f);
            tonemapMantiuk.process(hdr, tonemapMantiukMat);
            tonemapMantiukMat=EachPixelMultiplyNum(tonemapMantiukMat,255);


            Mat tonemapReinhardMat=new Mat();
            TonemapReinhard tonemapReinhard=   photo.createTonemapReinhard(1.0f, 0.0f, 1.0f, 0.0f);
            tonemapReinhard.process(hdr, tonemapReinhardMat);
            tonemapReinhardMat=EachPixelMultiplyNum(tonemapReinhardMat,255);


            //Write results
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\CalibrateDebevec-ldr.jpg ", ldr);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\CalibrateDebevec-lena-ldrRobertson.jpg ",    ldrRobertson);

            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\CalibrateDebevec-mantis-fusion.jpg ", fusion);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\CalibrateDebevec-mantis-mergeRobertsonMat.jpg ", mergeRobertsonMat);

            //Imgcodecs.imwrite("C://opencv3.1//samples//CalibrateDebevec-hdr.hdr", hdr);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\CalibrateDebevec-mantis-tonemapMat.jpg ", tonemapMat);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\CalibrateDebevec-mantis-tonemapDragoMat.jpg ", tonemapDragoMat);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\CalibrateDebevec-mantis-tonemapMantiukMat.jpg ", tonemapMantiukMat);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\CalibrateDebevec-mantis-tonemapReinhardMat.jpg ",  tonemapReinhardMat);

            //Imgcodecs.imwrite("C://opencv3.1//samples//CalibrateDebevec-AlignMTB.jpg ", alignMTBMatList.get(0));
            // Imgcodecs.imwrite("C://opencv3.1//samples//CalibrateDebevec-Robertson.jpg ",  calibrateRobertsonMat);
            //System.out.println("hdr="+hdr.dump());

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

    public static Mat EachPixelMultiplyNum(Mat inputMat, int Val){
        for (int i=0;i<inputMat.rows();i++){
            for (int j=0;j<inputMat.cols();j++){
                double[] data=new double[3];
                data=inputMat.get(i, j);
                data[0]= data[0]*Val;
                data[1]= data[1]*Val;
                data[2]= data[2]*Val;

                inputMat.put(i, j, data);
            }

        }
        return inputMat;
    }
}
Result
![CalibrateDebevec-ldr.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/497eabb2-1117-a8ab-22f8-885117f78788.jpeg) ![CalibrateDebevec-mantis-fusion.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/a28194e0-a6dd-5ac4-da12-d4eedad79938.jpeg) ![CalibrateDebevec-mantis-tonemapDragoMat.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/341b2db5-dfbb-b49c-74a3-eb306b5f5cb5.jpeg) ![CalibrateDebevec-mantis-tonemapMantiukMat.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/7fba9f77-ab68-58bb-0dc9-06df9f83ff03.jpeg) ![CalibrateDebevec-mantis-tonemapMat.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/4fa6ceb5-5041-fa50-741e-e3b699cc7205.jpeg) ![CalibrateDebevec-mantis-tonemapReinhardMat.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/9fc3e9c6-a3ac-0ce5-f9c9-0daf303aac28.jpeg)

Recommended Posts

Desktop: OpenCV HDR
Desktop: OpenCV Threshold
Desktop: OpenCV BilateralFilterBlur
Desktop: OpenCV Dilate
Desktop: OpenCV Expand
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV CLAHE
Desktop: OpenCV Ellipse2Poly
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 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 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