[JAVA] Bureau: histogramme OpenCV

Goal
Test OpenCV histogram.

OpenCV_Histogram.java


import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.image.BufferedImage;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

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

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

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

        BufferedImage image=matToBufferedImage(source);

        frmjavaSwing = new JFrame();
        frmjavaSwing.setTitle("opencv Straight 圖 Pratique statistique");
        frmjavaSwing.setBounds(100, 100, 780, 620);
        frmjavaSwing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmjavaSwing.getContentPane().setLayout(null);

        final JLabel lblNewLabel_histogram = new JLabel("");
        lblNewLabel_histogram.setBounds(250, 10, 512, 551);
        frmjavaSwing.getContentPane().add(lblNewLabel_histogram);

        final JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setBounds(5, 60, image.getHeight()+10, image.getWidth()+10);
        lblNewLabel.setIcon(new ImageIcon(image));
        frmjavaSwing.getContentPane().add(lblNewLabel);

        JButton btn3 = new JButton("Statistiques carrées");
        btn3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                BufferedImage newImage=matToBufferedImage(getHistogram(source));
                lblNewLabel_histogram.setIcon(new ImageIcon(newImage));
            }
        });
        btn3.setBounds(10, 10, 114, 23);
        frmjavaSwing.getContentPane().add(btn3);

    }
    public Mat getHistogram(Mat source){
        List<Mat> bgr_planes = new ArrayList<Mat>();
        List<Mat> bgr_plane = new ArrayList<Mat>();
        Core.split(source, bgr_planes);
        //bgr_plane.add(bgr_planes.get(0));
        MatOfInt histSize = new MatOfInt(256);
        MatOfFloat histRange = new MatOfFloat(0,256);
        Mat hist = new  Mat();
        int hist_w = 512;
        int hist_h = 500;
        long bin_w;
        Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC3);

        for(int j=0;j<3;j++){


            bgr_plane.clear();
            bgr_plane.add(bgr_planes.get(j));

            Imgproc.calcHist((java.util.List<Mat>) bgr_plane, new MatOfInt(0),new Mat(), hist, histSize, histRange);
            //System.out.println("hist="+b_hist.dump()+"size="+b_hist.size());


            bin_w = Math.round((double) (hist_w / 256));

            Core.normalize(hist, hist, 0, histImage.rows(), Core.NORM_MINMAX);



            for (int i = 1; i < 256; i++) {

                if (j==0){
                    Imgproc.line(histImage, new Point(bin_w * (i - 1),hist_h- Math.round(hist.get( i-1,0)[0])),
                            new Point(bin_w * (i), hist_h-Math.round(Math.round(hist.get(i, 0)[0]))),
                            new Scalar(255, 0, 0), 2, 8, 0);
                }else if (j==1){
                    Imgproc.line(histImage, new Point(bin_w * (i - 1),hist_h- Math.round(hist.get( i-1,0)[0])),
                            new Point(bin_w * (i), hist_h-Math.round(Math.round(hist.get(i, 0)[0]))),
                            new  Scalar( 0,255, 0), 2, 8, 0);
                }else if (j==2){
                    Imgproc.line(histImage, new Point(bin_w * (i - 1),hist_h- Math.round(hist.get( i-1,0)[0])),
                            new Point(bin_w * (i), hist_h-Math.round(Math.round(hist.get(i, 0)[0]))),
                            new  Scalar(0, 0,255), 2, 8, 0);
                }


            }
        }
        return histImage;

    }
    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;
    }
}
Result
![opencv_histogram.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/5bd54299-e0a6-f0a4-bed2-26926a9e9bb1.jpeg)

Recommended Posts

Bureau: histogramme OpenCV
Bureau: seuil OpenCV
Bureau: OpenCV BilateralFilterBlur
Bureau: OpenCV Dilate
Bureau: OpenCV Expand
Bureau: OpenCV Emboss
Bureau: OpenCV CLAHE
Bureau: OpenCV Ellipse2Poly
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: OpenCV Dft
Bureau: OpenCV Decolor
Bureau: OpenCV FaceDetector
Bureau: OpenCV Denoise2
Bureau: OpenCV StereoBM
Bureau: Filtre OpenCV Kirsch
Bureau: Filtre laplacien OpenCV 2
Bureau: Changement d'éclairage OpenCV
Bureau: OpenCV Fill ConvexPoly
Bureau: OpenCV Grab Cut
Bureau: Changement de couleur OpenCV
Bureau: Filtre Freichennel OpenCV
Bureau: seuil adaptatif OpenCV
Bureau: OpenCV Draw Circle
Bureau: OpenCV Fill Poly
Bureau: OpenCV Mean Filter
Bureau: référentiel Java OpenCV
Bureau: OpenCV Sobel Filter2
Bureau: OpenCV pyrMeanShift Filter
Bureau: OpenCV OpticalFlow PyrLK
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: 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: OpenCV Seamless Clone
Bureau: OpenCV Draw Point
Bureau: OpenCV Sobel Filter