[JAVA] Desktop: OpenCV Merge Two Images In Any Shape

Goal
Test OpenCV merge two image with ROI.

OpenCV_MergeTwoImgAnyShape.java


import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

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

public class OpenCV_MergeTwoImgAnyShape {

    public static void main( String[] args )
    {
        try{
            System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
            //Daisuke(Mother)
            Mat source = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\ncku.jpg ",Imgcodecs.CV_LOAD_IMAGE_COLOR);

            //Small 圖(Subgraph)
            Mat source1 = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\jelly_studio_logo.jpg ",Imgcodecs.CV_LOAD_IMAGE_COLOR);
            Mat destination=source.clone();


            // to make the white region transparent
            Mat mask2=new Mat();
            Mat dst=new Mat();


            Imgproc.cvtColor(source1,mask2,Imgproc.COLOR_BGR2GRAY);
            Imgproc.threshold(mask2,mask2,230,255, Imgproc.THRESH_BINARY_INV);
            List<Mat> planes = new ArrayList<Mat>() ;
            List<Mat> result = new ArrayList<Mat>() ;
            Mat result1=new Mat();
            Mat result2=new Mat();
            Mat result3=new Mat();


            Core.split(source1, planes);

            Core.bitwise_and(planes.get(0), mask2, result1);
            Core.bitwise_and(planes.get(1), mask2, result2);
            Core.bitwise_and(planes.get(2), mask2, result3);

            result.add(result1);
            result.add(result2);
            result.add(result3);
            Core.merge(result, dst);
            //White and transparent


            //Re-grabbing small copy to large
            Rect roi=new Rect(50,50,90,62);//Impossible Hihara Keidai,Small
            Mat destinationROI = source.submat( roi );
            dst.copyTo( destinationROI , dst);


            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\merge3.jpg ", source);
        }catch (Exception e) {
            System.out.println("error: " + e.getMessage());
        }
    }
}
Result
![merge3.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/e53a6c2a-c152-a26b-4dce-9aab9230c576.jpeg)

Recommended Posts

Desktop: OpenCV Merge Two Images In Any Shape
Desktop: OpenCV Merge Two Image
Desktop: OpenCV merge Picture
Desktop: OpenCV BilateralFilterBlur
Desktop: OpenCV Dilate
Desktop: OpenCV Expand
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV CLAHE
Desktop: OpenCV Ellipse2Poly
Desktop: OpenCV HDR
Desktop: OpenCV Polylines
Desktop: OpenCV Denoise
Desktop: OpenCV Sharpness
Desktop: OpenCV OpenCV_SalonUseBlurAddWeighted
Desktop: OpenCV Mosaic
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