[JAVA] Desktop: OpenCV Spot

Goal
Test OpenCV spot.

OpenCV_Spot.java


import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Canvas;

import javax.swing.ImageIcon;
import javax.swing.JColorChooser;
import javax.swing.JPanel;
import javax.swing.JLabel;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class OpenCV_Spot {
    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
    private JFrame frmOpencv;
    private Mat sourceMat=Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena.jpg ");
    private Color color;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    OpenCV_Spot window = new OpenCV_Spot();
                    window.frmOpencv.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {



        final BufferedImage image=matToBufferedImage(sourceMat);

        frmOpencv = new JFrame();
        frmOpencv.setTitle("opencv Jukoto effect practice");
        frmOpencv.getContentPane().setBackground(Color.LIGHT_GRAY);
        frmOpencv.setBounds(100, 100, 547, 627);
        frmOpencv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmOpencv.getContentPane().setLayout(null);

        final JLabel lblShowXY = new JLabel("");
        lblShowXY.setBounds(405, 2, 124, 23);
        frmOpencv.getContentPane().add(lblShowXY);


        JPanel panel = new JPanel();

        final JLabel lblPaintLabel = new JLabel("");
        lblPaintLabel.setIcon(new ImageIcon(image));
        panel.add(lblPaintLabel);


        panel.setBounds(10, 64, 519, 519);
        frmOpencv.getContentPane().add(panel);

        JLabel lblNewLabel = new JLabel("Jukou intensity");
        lblNewLabel.setBounds(10, 20, 58, 15);
        frmOpencv.getContentPane().add(lblNewLabel);

        final JSlider slider = new JSlider();

        slider.setValue(30);
        slider.setMinimum(1);
        slider.setBounds(78, 10, 200, 25);
        frmOpencv.getContentPane().add(slider);

        final JLabel Label_scale = new JLabel("30");
        Label_scale.setBounds(298, 20, 46, 15);
        frmOpencv.getContentPane().add(Label_scale);

        slider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent arg0) {
                Label_scale.setText(slider.getValue()+"");
            }
        });

        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                lblShowXY.setText("X:"+arg0.getX()+",Y:"+arg0.getY());
                BufferedImage newImage=matToBufferedImage(spot(sourceMat,slider.getValue(),arg0.getX(),arg0.getY()) );
                lblPaintLabel.setIcon(new ImageIcon(newImage));
            }
        });
    }

    public Mat spot(Mat source,int Scale,int PntX,int PntY){
        double scale=0-Scale;
        Point pt=new Point(PntX,PntY);
        Mat destination=new Mat(source.rows(),source.cols(),source.type());
        for (int j=0;j<source.rows();j++){
            for(int i=0;i<source.cols();i++){
                double[] temp=source.get(j,i);
                double dx=((j-pt.x)/pt.x);
                double dy=((i-pt.y)/pt.y);
                double weight=Math.exp((dx*dx+dy*dy)*scale);
                temp[0]=Math.round(temp[0]*weight);
                temp[1]=Math.round(temp[1]*weight);
                temp[2]=Math.round(temp[2]*weight);
                destination.put(j, i, temp);
            }
        }
        return destination;
    }


    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 Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}
Result
![opencv_spot.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/312f5a9f-7da7-ac47-f08f-18d1c6b6f062.jpeg)

Recommended Posts

Desktop: OpenCV Spot
Desktop: OpenCV Threshold
Desktop: OpenCV BilateralFilterBlur
Desktop: OpenCV Dilate
Desktop: OpenCV Expand
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV Ellipse2Poly
Desktop: OpenCV HDR
Desktop: OpenCV Polylines
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 Inpaint
Desktop: OpenCV NormalizeBlur
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 Illumination Change
Desktop: OpenCV Add WaterMark
Desktop: OpenCV Fill ConvexPoly
Desktop: OpenCV Grab Cut
Desktop: OpenCV Sharpness Gui
Desktop: OpenCV Color Change
Desktop: OpenCV Freichennel Filter
Desktop: OpenCV Adaptive Threshold
Desktop: OpenCV Fill Poly
Desktop: OpenCV Mean Filter
Desktop: OpenCV Java Repository
Desktop: OpenCV Sobel Filter2
Desktop: OpenCV pyrMeanShift Filter
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 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 Seamless Clone