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

Desktop: OpenCV Seamless Clone
Desktop: OpenCV Threshold
Desktop: OpenCV Dilate
Desktop: OpenCV Expand
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV CLAHE
Desktop: OpenCV HDR
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 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 Add WaterMark
Desktop: OpenCV Fill ConvexPoly
Desktop: OpenCV Grab Cut
Desktop: OpenCV Sharpness Gui
Desktop: OpenCV Freichennel Filter
Desktop: OpenCV Draw Circle
Desktop: OpenCV Mean Filter
Desktop: OpenCV Sobel Filter2
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
Desktop: OpenCV Color Map
Desktop: OpenCV Draw Point
Desktop: OpenCV Sobel Filter
Desktop: OpenCV OpticalFlow Farneback
Desktop: OpenCV Prewitt Filter