[JAVA] Bureau: 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

Bureau: OpenCV HDR
Bureau: seuil OpenCV
Bureau: OpenCV BilateralFilterBlur
Bureau: OpenCV Dilate
Bureau: OpenCV Expand
Bureau: OpenCV Affine
Bureau: OpenCV Emboss
Bureau: OpenCV CLAHE
Bureau: OpenCV Ellipse2Poly
Bureau: Polylignes OpenCV
Bureau: OpenCV Denoise
Bureau: netteté OpenCV
Bureau: OpenCV Concat
Bureau: OpenCV OpenCV_SalonUseBlurAddWeighted
Bureau: OpenCV Mosaic
Bureau: OpenCV Erode
Bureau: OpenCV Denoise
Bureau: Rectangle OpenCV
Bureau: OpenCV Watershed
Bureau: Texte OpenCV
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: Filtre OpenCV Kirsch
Bureau: Filtre laplacien OpenCV 2
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: OpenCV OpticalFlow PyrLK
Bureau: Piano virtuel OpenCV
Bureau: image de fusion OpenCV
Bureau: Filtre OpenCV Scharr
Bureau: aperçu de la webcam Opencv
Bureau: Filtre laplacien OpenCV
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