[JAVA] Bureau: OpenCV combine 2 photos avec un clone transparent

Goal
Test OpenCV combine 2 pics with seamless clone.

OpenCV_Combine2PicsWithSeamlessClone.java


import org.opencv.core.*;
import org.opencv.core.Point;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.photo.Photo;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class OpenCV_Combine2PicsWithSeamlessClone {
    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
    private JFrame frmjavaSwing;

    public List<Point> allPoint=new ArrayList<Point>();
    private List<Integer> pointX=new ArrayList<Integer>();
    private List<Integer> pointY=new ArrayList<Integer>();
    private int maxX,maxY,minX,minY;
    private Mat globalRoi;
    private Mat MaskRoi;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    OpenCV_Combine2PicsWithSeamlessClone window = new OpenCV_Combine2PicsWithSeamlessClone();
                    window.frmjavaSwing.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public OpenCV_Combine2PicsWithSeamlessClone() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        final Mat source = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\DSC_0897.jpg ");
        final Mat rightImg = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\DSC_0230.jpg ");

        BufferedImage image=matToBufferedImage(source);
        BufferedImage imageRight=matToBufferedImage(rightImg);

        frmjavaSwing = new JFrame();
        frmjavaSwing.setTitle("Duplication de ROI irrégulière en raison de l'utilisation de plusieurs formes");
        frmjavaSwing.setBounds(100, 100, 922, 482);
        frmjavaSwing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmjavaSwing.getContentPane().setLayout(null);

        final JLabel lblLocation = new JLabel("");
        lblLocation.setBounds(10, 10, 112, 25);
        frmjavaSwing.getContentPane().add(lblLocation);

        final JLabel lblLocation2 = new JLabel("");
        lblLocation2.setBounds(121, 10, 112, 25);
        frmjavaSwing.getContentPane().add(lblLocation2);

        JPanel panel = new JPanel();
        panel.setBounds(10, 59, 200, 379);
        frmjavaSwing.getContentPane().add(panel);

        final JLabel lblNewLabel = new JLabel("");
        panel.add(lblNewLabel);


        lblNewLabel.setIcon(new ImageIcon(image));

        final JLabel label_Right = new JLabel("");
        label_Right.setBounds(430, 92, 449, 346);

        label_Right.setIcon(new ImageIcon(imageRight));
        frmjavaSwing.getContentPane().add(label_Right);

        JButton btnNewButton = new JButton("Fermeture");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                BufferedImage newImage=matToBufferedImage(paintAndSetPolyClose());
                lblNewLabel.setIcon(new ImageIcon(newImage));
            }
        });
        btnNewButton.setBounds(243, 10, 89, 25);
        frmjavaSwing.getContentPane().add(btnNewButton);

        final JLabel Label_mid = new JLabel("");
        Label_mid.setBounds(220, 106, 200, 242);
        frmjavaSwing.getContentPane().add(Label_mid);

        JButton btnNewButton_1 = new JButton("ROI irrégulier pour la reprise");
        btnNewButton_1.setBounds(342, 10, 175, 25);
        frmjavaSwing.getContentPane().add(btnNewButton_1);

        final JSlider sliderX = new JSlider();

        sliderX.setMaximum(465);
        sliderX.setBounds(577, 10, 248, 25);
        frmjavaSwing.getContentPane().add(sliderX);

        JLabel lblNewLabel_1 = new JLabel("X:");
        lblNewLabel_1.setBounds(542, 10, 34, 15);
        frmjavaSwing.getContentPane().add(lblNewLabel_1);

        final JSlider sliderY = new JSlider();

        sliderY.setMaximum(315);
        sliderY.setBounds(577, 45, 248, 25);
        frmjavaSwing.getContentPane().add(sliderY);

        JLabel lblY = new JLabel("Y:");
        lblY.setBounds(542, 45, 34, 15);
        frmjavaSwing.getContentPane().add(lblY);

        final JLabel showX_Val = new JLabel("50");
        showX_Val.setBounds(835, 10, 46, 15);
        frmjavaSwing.getContentPane().add(showX_Val);

        final JLabel showY_Val = new JLabel("50");
        showY_Val.setBounds(835, 45, 46, 15);
        frmjavaSwing.getContentPane().add(showY_Val);

        sliderX.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent arg0) {
                showX_Val.setText(sliderX.getValue()+"");

                Mat currentRightImg = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\DSC_0230.jpg ");
                //Wakasei currentRightImg utilise initialize()Phénomène Img 會 疊 圖 droit
                Mat	rightSideImg=pasteToAnother(sliderX.getValue(),sliderY.getValue(), getGlobalRoi(),currentRightImg);
                BufferedImage	newImage=matToBufferedImage(rightSideImg);
                label_Right.setIcon(new ImageIcon(newImage));
            }
        });

        sliderY.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent arg0) {
                showY_Val.setText(sliderY.getValue()+"");

                Mat currentRightImg = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\DSC_0230.jpg ");
                Mat	rightSideImg=pasteToAnother(sliderX.getValue(),sliderY.getValue(), getGlobalRoi(),currentRightImg);
                BufferedImage	newImage=matToBufferedImage(rightSideImg);
                label_Right.setIcon(new ImageIcon(newImage));

            }
        });
        //Auditeur
        lblNewLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                lblLocation2.setText("");
                lblLocation.setText("Joindre:X:"+arg0.getX()+",Y:"+arg0.getY());

                BufferedImage newImage=matToBufferedImage(paintPointAndPolylines(arg0.getX(),arg0.getY()));
                lblNewLabel.setIcon(new ImageIcon(newImage));

            }
        });

        //ROI irrégulier pour la collecte 點 擊 擷
        btnNewButton_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                Mat roi=getRoi(source);
                BufferedImage newImage=matToBufferedImage(roi);
                Label_mid.setIcon(new ImageIcon(newImage));

                //set global data
                setGlobalRoi(roi);


                //paste to another one,default position is (50,50)
                Mat	rightSideImg=pasteToAnother(100,100, roi,rightImg);
//					    Mat src_mask=new Mat(roi.size(),roi.depth(),new Scalar(255));
//					    Mat mixed_clone=new Mat();
//					    Photo.seamlessClone(roi,rightImg, src_mask, new Point(150,150), mixed_clone,  Photo.MIXED_CLONE);




                newImage=matToBufferedImage(rightSideImg);
                label_Right.setIcon(new ImageIcon(newImage));

            }
        });
    }
    //Ryoide 點 et propre ligne
    public Mat paintPointAndPolylines(int x,int y){
        Mat src = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\DSC_0897.jpg ");
        List<Integer> pointX=getPointX();
        List<Integer> pointY=getPointY();
        List<Point> allPoint=getAllPoint();
        allPoint.add(new Point(x,y));
        setAllPoint(allPoint);

        pointX.add(x);
        pointY.add(y);
        setPointX(pointX);
        setPointY(pointY);

        //畫 鼠 點 铊 點
        Imgproc.circle(src, new Point(x,y), 2, new Scalar(0,55,255),2);

        //Polyligne
        if(allPoint.size()>=2){
            MatOfPoint mop1=new MatOfPoint();
            List<MatOfPoint> allMatOfPoint=new ArrayList<MatOfPoint>();
            mop1.fromList(allPoint);
            allMatOfPoint.add(mop1);
            Imgproc.polylines(src, allMatOfPoint, false, new Scalar(255,255,255), 2);

        }

        return src;

    }

    //Fermeture irrégulière
    public Mat paintAndSetPolyClose(){
        Mat src = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\DSC_0897.jpg ");
        List<Point> allPoint=getAllPoint();
        //Polyligne
        if(allPoint.size()>=2){
            MatOfPoint mop1=new MatOfPoint();
            List<MatOfPoint> allMatOfPoint=new ArrayList<MatOfPoint>();
            mop1.fromList(allPoint);
            allMatOfPoint.add(mop1);
            Imgproc.polylines(src, allMatOfPoint, true, new Scalar(255,255,255), 2);

        }

        return src;
    }
    //ROI
    public Mat getRoi(Mat src){
        Mat RoiMat=new Mat();
        List<Integer> pointX=getPointX();
        List<Integer> pointY=getPointY();


        //Propriété de 找 點 Collectif min et max,ROI établi
        int X0,Y0,X1,Y1,maxX,maxY,minX,minY;
        maxX=Collections.max(pointX);
        maxY= Collections.max(pointY);
        minX=Collections.min(pointX);
        minY=Collections.min(pointY);

        //Réutilisation de la demande de rapatriement
        setMaxX(maxX);
        setMinX(minX);
        setMaxY(maxY);
        setMinY(minY);


        //ROI rectangulaire établie
        Rect rect=new Rect(new Point(minX,minY),new Point(maxX,maxY));
        RoiMat=src.submat(rect);

        //Masque de type Photoshop,0 Inaction des Noirs
        Mat  MaskTemplate=new Mat(RoiMat.size(),CvType.CV_8UC1,new Scalar(0));

        List<Point> allPoint=getAllPoint();
        List<Point> newPointSet=new ArrayList<Point>();
        // System.out.println("size="+allPoint.size());
        List<MatOfPoint> allMatOfPoint=new ArrayList<MatOfPoint>();
        MatOfPoint mop1=new MatOfPoint();
        Point tmpPoint;
        //Coordonnées de possession collective requises,Coordonnées du coin supérieur gauche de type ROI(minX,minY),Tous sont réduits du coin supérieur gauche
        for(int i=0;i<allPoint.size();i++){
            tmpPoint=allPoint.get(i);
            newPointSet.add(new Point(tmpPoint.x-minX,tmpPoint.y-minY));
        }

        mop1.fromList(newPointSet);
        allMatOfPoint.add(mop1);
        //Utiliser fillPoly Remplir la zone essentielle,255 blanc,action
        Imgproc.fillPoly(MaskTemplate, allMatOfPoint,  new Scalar(255,255,255));

        //Global pour définir le matériau du masque,Ne pas aller
        setMaskRoi(MaskTemplate);

        //Tapis irrégulier
        Mat dst=new Mat();
        Core.bitwise_and(RoiMat, RoiMat, dst,MaskTemplate);
        //Pour Core.ajouter
        //Core.add(RoiMat, RoiMat, dst,MaskTemplate );
        return dst;
    }


    //ROI collé ensemble,image enfant irrégulière roi,autreImg,x,y Oui
    public Mat pasteToAnother(int x,int y,Mat roi,Mat anotherImg){


        Mat src_mask=getMaskRoi();
        Mat mixed_clone=new Mat();
        try {
            Photo.seamlessClone(roi,anotherImg, src_mask, new Point(x,y), mixed_clone,  Photo.NORMAL_CLONE);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return mixed_clone;

    }




    public BufferedImage matToBufferedImage(Mat matrix) {
        int cols = matrix.cols();
        int rows = matrix.rows();
        int elemSize = (int)matrix.elemSize();
        byte[] data = new byte[cols * rows * elemSize];
        int type;
        matrix.get(0, 0, data);
        switch (matrix.channels()) {
            case 1:
                type = BufferedImage.TYPE_BYTE_GRAY;
                break;
            case 3:
                type = BufferedImage.TYPE_3BYTE_BGR;
                // bgr to rgb
                byte b;
                for(int i=0; i<data.length; i=i+3) {
                    b = data[i];
                    data[i] = data[i+2];
                    data[i+2] = b;
                }
                break;
            default:
                return null;
        }
        BufferedImage image2 = new BufferedImage(cols, rows, type);
        image2.getRaster().setDataElements(0, 0, cols, rows, data);
        return image2;
    }



    public List<Point> getAllPoint() {
        return allPoint;
    }

    public void setAllPoint(List<Point> allPoint) {
        this.allPoint = allPoint;
    }

    public List<Integer> getPointX() {
        return pointX;
    }

    public void setPointX(List<Integer> pointX) {
        this.pointX = pointX;
    }

    public List<Integer> getPointY() {
        return pointY;
    }

    public void setPointY(List<Integer> pointY) {
        this.pointY = pointY;
    }

    public int getMaxX() {
        return maxX;
    }

    public void setMaxX(int maxX) {
        this.maxX = maxX;
    }

    public int getMaxY() {
        return maxY;
    }

    public void setMaxY(int maxY) {
        this.maxY = maxY;
    }

    public int getMinX() {
        return minX;
    }

    public void setMinX(int minX) {
        this.minX = minX;
    }

    public int getMinY() {
        return minY;
    }

    public void setMinY(int minY) {
        this.minY = minY;
    }

    public Mat getGlobalRoi() {
        return globalRoi;
    }

    public void setGlobalRoi(Mat globalRoi) {
        this.globalRoi = globalRoi;
    }

    public Mat getMaskRoi() {
        return MaskRoi;
    }

    public void setMaskRoi(Mat maskRoi) {
        MaskRoi = maskRoi;
    }
}
Result
![opencv_combine2_pics_with_seamless_clone.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/92603570-aeb1-9989-fae9-6920de927d62.jpeg)

Recommended Posts

Bureau: OpenCV combine 2 photos avec un clone transparent
Bureau: OpenCV Seamless Clone
Bureau: OpenCV Fill Poly avec contrôle de la souris
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 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: OpenCV utilise EqualizeHist avec ajout de poids pour RVB
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