[JAVA] Bureau: OpenCV Seamless Clone

Goal
Test opencv seamless clone.

OpenCV_SeamlessClone.java


import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.photo.Photo;

public class OpenCV_SeamlessClone {
    public static void main(String[] args) {
        System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
        Mat src1 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\monster.jpg ",Imgcodecs.CV_LOAD_IMAGE_COLOR);
        Mat dst1 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\DSCN3629s.jpg ",Imgcodecs.CV_LOAD_IMAGE_COLOR);

        Mat src2 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\monster-bg-less.jpg ",Imgcodecs.CV_LOAD_IMAGE_COLOR);

        Mat src3 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\IloveOpencv-s.jpg ",Imgcodecs.CV_LOAD_IMAGE_COLOR);
        Mat dst3 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\wood2.jpg ",Imgcodecs.CV_LOAD_IMAGE_COLOR);

        Mat src4 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\IloveOpencv-gbg.jpg ",Imgcodecs.CV_LOAD_IMAGE_COLOR);

        // Create an all white mask
        Mat src_mask = new Mat(src1.size(), src1.depth(),new Scalar(255));

//	    for (int i=0;i<src_mask.rows();i++){
//      	  for (int j=0;j<src_mask.cols();j++){
//      		  double[] data=new double[3];
//      		  data=src_mask.get(i, j);
//      		  data[0]= 255;
//
//      		src_mask.put(i, j, data);
//      	  }
//
//        }

        Mat src_mask3 = new Mat(src3.size(), src3.depth(),new Scalar(255));
//	    for (int i=0;i<src_mask3.rows();i++){
//	      	  for (int j=0;j<src_mask3.cols();j++){
//	      		  double[] data=new double[3];
//	      		  data=src_mask3.get(i, j);
//	      		  data[0]= 255;
//
//	      		src_mask3.put(i, j, data);
//	      	  }
//
//	        }





        // The location of the center of the src in the dst
        Point center=new Point(dst1.cols()/2,dst1.rows()/2);
        Point center3=new Point(dst3.cols()/2,dst3.rows()/2);

        // Seamlessly clone src into dst and put the results in output
        Mat normal_clone1=new Mat();
        Mat mixed_clone1=new Mat();

        Mat normal_clone2=new Mat();
        Mat mixed_clone2=new Mat();

        Mat normal_clone3=new Mat();
        Mat mixed_clone3=new Mat();


        Mat normal_clone4=new Mat();
        Mat mixed_clone4=new Mat();

        Photo.seamlessClone(src1, dst1, src_mask, center, normal_clone1, Photo.NORMAL_CLONE);
        Photo.seamlessClone(src1, dst1, src_mask, center, mixed_clone1,  Photo.MIXED_CLONE);

        Photo.seamlessClone(src2, dst1, src_mask, center, normal_clone2, Photo.NORMAL_CLONE);
        Photo.seamlessClone(src2, dst1, src_mask, center, mixed_clone2,  Photo.MIXED_CLONE);

        Photo.seamlessClone(src3, dst3, src_mask3, center3, normal_clone3, Photo.NORMAL_CLONE);
        Photo.seamlessClone(src3, dst3, src_mask3, center3, mixed_clone3,  Photo.MIXED_CLONE);

        Photo.seamlessClone(src4, dst3, src_mask3, center3, normal_clone4, Photo.NORMAL_CLONE);
        Photo.seamlessClone(src4, dst3, src_mask3, center3, mixed_clone4,  Photo.MIXED_CLONE);

        System.out.println("Photo.NORMAL_CLONE="+Photo.NORMAL_CLONE);
        System.out.println("Photo.MIXED_CLONE="+ Photo.MIXED_CLONE);

        // Save results
        Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\SeamlessClone-normalClone-LakeMonster.jpg ", normal_clone1);
        Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\SeamlessClone-MixedClone-LakeMonster.jpg ", mixed_clone1);

        Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\SeamlessClone-normalClone-LakeMonster-bgLess.jpg ", normal_clone2);
        Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\SeamlessClone-MixedClone-LakeMonster-bgLess.jpg ", mixed_clone2);

        Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\SeamlessClone-normalClone-iloveCv.jpg ", normal_clone3);
        Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\SeamlessClone-MixedClone-iloveCv.jpg ", mixed_clone3);

        Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\SeamlessClone-normalClone-iloveCv2.jpg ", normal_clone4);
        Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\SeamlessClone-MixedClone-iloveCv2.jpg ", mixed_clone4);
    }
}
Result
![SeamlessClone-MixedClone-LakeMonster-bgLess.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/3cc1f06c-40af-1654-7707-f7e7d7392696.jpeg)

SeamlessClone-MixedClone-LakeMonster.jpg

SeamlessClone-normalClone-LakeMonster-bgLess.jpg

SeamlessClone-normalClone-LakeMonster.jpg

Recommended Posts

Bureau: OpenCV Seamless Clone
Bureau: seuil OpenCV
Bureau: OpenCV Dilate
Bureau: OpenCV Expand
Bureau: OpenCV Affine
Bureau: OpenCV Emboss
Bureau: OpenCV CLAHE
Bureau: OpenCV HDR
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 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: OpenCV Add WaterMark
Bureau: OpenCV Fill ConvexPoly
Bureau: OpenCV Grab Cut
Bureau: OpenCV Sharpness Gui
Bureau: Filtre Freichennel OpenCV
Bureau: OpenCV Draw Circle
Bureau: OpenCV Mean Filter
Bureau: OpenCV Sobel Filter2
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
Bureau: Filtre OpenCV SqrBox
Bureau: redimensionner l'image OpenCV
Bureau: Croquis au crayon OpenCV
Bureau: carte des couleurs OpenCV
Bureau: OpenCV Draw Point
Bureau: OpenCV Sobel Filter
Bureau: OpenCV OpticalFlow Farneback
Bureau: Filtre OpenCV Prewitt