1. 程式人生 > 實用技巧 >Java 呼叫 OpenCV (可獲取到影象)

Java 呼叫 OpenCV (可獲取到影象)

1.

package com;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDouble;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.ml.SVM;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.objdetect.HOGDescriptor;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.Videoio;

public class CaptureBasic extends JPanel {
    private BufferedImage mImg;

    public static void main(String[] args) {
        try {
            //載入本地native庫  配置較繁瑣,不用
            // System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
            //獲取opencv.dll絕對路徑  推薦
            URL url = ClassLoader.getSystemResource("lib/opencv_java440.dll");
            System.load(url.getPath());

            //獲取本地攝像頭
            // VideoCapture capture = new VideoCapture(0);

            //獲取網路攝像頭
            VideoCapture capture = new VideoCapture();
            capture.open("rtsp://admin:[email protected]:554/stream0");

            int height = (int) capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);
            int width = (int) capture.get(Videoio.CAP_PROP_FRAME_WIDTH);
            if (height == 0 || width == 0) {
                throw new Exception("camera not found!");
            }

            //Java視窗容器
            JFrame frame = new JFrame("camera");
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            //Java畫板容器
            CaptureBasic panel = new CaptureBasic();
            addMouseListener(panel);
            //配置關係
            frame.setContentPane(panel);
            frame.setVisible(true);
            frame.setSize(width + frame.getInsets().left + frame.getInsets().right, height + frame.getInsets().top + frame.getInsets().bottom);
            int n = 0;
            Mat capImg = new Mat();
            Mat temp = new Mat();
            while (frame.isShowing() && ++n < 500) {
                //把攝像頭資料讀到Mat
                capture.read(capImg);
                //彩色空間轉換,把影象轉換為灰度的、佔用空間小的
                Imgproc.cvtColor(capImg, temp, Imgproc.COLOR_RGB2GRAY);
                // temp = capImg.clone();
                //儲存
                Imgcodecs.imwrite("D:/VideoRec/Img/back" + n + ".png", temp);
                //進行人臉識別
                Mat mat = detectFace(capImg);
                //把識別畫框影象放在畫板上
                panel.mImg = panel.mat2BI(mat);
                //繪製
                panel.repaint();
            }
            capture.release();
            frame.dispose();
        } catch (Exception e) {
            System.out.println("異常:" + e.getMessage() + " --- " + Arrays.toString(e.getStackTrace()));
        } finally {
            System.out.println("--done--");
        }
    }

    private BufferedImage mat2BI(Mat mat) {
        int dataSize = mat.cols() * mat.rows() * (int) mat.elemSize();
        byte[] data = new byte[dataSize];
        mat.get(0, 0, data);
        int type = mat.channels() == 1 ? BufferedImage.TYPE_BYTE_GRAY : BufferedImage.TYPE_3BYTE_BGR;
        if (type == BufferedImage.TYPE_3BYTE_BGR) {
            for (int i = 0; i < dataSize; i += 3) {
                byte blue = data[i + 0];
                data[i + 0] = data[i + 2];
                data[i + 2] = blue;
            }
        }
        BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
        image.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);
        return image;
    }

    /**
     * opencv實現人臉識別
     *
     * @param img
     */
    public static Mat detectFace(Mat img) throws Exception {
        System.out.println("Running DetectFace ... ");
        // 從配置檔案lbpcascade_frontalface.xml中建立一個人臉識別器,該檔案位於opencv安裝目錄中
        // CascadeClassifier faceDetector = new CascadeClassifier("D:\\TDDOWNLOAD\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
        CascadeClassifier faceDetector = new CascadeClassifier("D:/Program/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");

        // 在圖片中檢測人臉
        MatOfRect faceDetections = new MatOfRect();

        faceDetector.detectMultiScale(img, faceDetections);

        //System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

        Rect[] rects = faceDetections.toArray();
        if (rects != null && rects.length >= 1) {
            for (Rect rect : rects) {
                Imgproc.rectangle(img, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                        new Scalar(0, 0, 255), 2);
            }
        }
        return img;
    }

    /**
     * opencv實現人型識別,hog預設的分類器。所以效果不好。
     *
     * @param img
     */
    public static Mat detectPeople(Mat img) {
        //System.out.println("detectPeople...");
        if (img.empty()) {
            System.out.println("image is exist");
        }
        HOGDescriptor hog = new HOGDescriptor();
        hog.setSVMDetector(HOGDescriptor.getDefaultPeopleDetector());
        System.out.println(HOGDescriptor.getDefaultPeopleDetector());
        //hog.setSVMDetector(HOGDescriptor.getDaimlerPeopleDetector());
        MatOfRect regions = new MatOfRect();
        MatOfDouble foundWeights = new MatOfDouble();
        //System.out.println(foundWeights.toString());
        hog.detectMultiScale(img, regions, foundWeights);
        for (Rect rect : regions.toArray()) {
            Imgproc.rectangle(img, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255), 2);
        }
        return img;
    }

    /**
     * 畫到容器
     *
     * @param g
     */
    public void paintComponent(Graphics g) {
        if (mImg != null) {
            g.drawImage(mImg, 0, 0, mImg.getWidth(), mImg.getHeight(), this);
        }
    }

    /**
     * 新增滑鼠監聽
     *
     * @param panel
     */
    private static void addMouseListener(CaptureBasic panel) {
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("click");
            }

            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("mousePressed");
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("mouseReleased");

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                System.out.println("mouseEntered");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("mouseExited");
            }

            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                System.out.println("mouseWheelMoved");
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                System.out.println("mouseDragged");
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println("mouseMoved");
            }
        });
    }
}