本文主要是介绍opencv3_java 摄像头图形图像检测DetectObjectsCamera videoioVideoCapture,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
摄像头图形图像检测DetectObjectsCamera videoioVideoCapture
package opencv_java_demo;import javax.swing.*;import java.awt.*;
import java.awt.image.BufferedImage;import org.opencv.core.*;
import org.opencv.core.Point;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.Videoio;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.imgproc.Imgproc;public class DetectObjectsCamera extends JPanel {private static final long serialVersionUID=1L;private BufferedImage mImg;private static Mat dobj(CascadeClassifier objDetector, Mat src){Mat dst=src.clone();MatOfRect objDetections=new MatOfRect();objDetector.detectMultiScale(dst, objDetections);if(objDetections.toArray().length<=0){return src;}for(Rect rect:objDetections.toArray()){Imgproc.rectangle(dst, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height), new Scalar(0,0,255),2);}return dst;}public void paintComponent(Graphics g){if(mImg!=null){g.drawImage(mImg, 0, 0, mImg.getWidth(),mImg.getHeight(),this);}}public static void main(String[] args) {try{System.loadLibrary(Core.NATIVE_LIBRARY_NAME);CascadeClassifier objDetector=new CascadeClassifier("data/lbpcascades/lbpcascade_frontalface.xml");Mat capImg=new Mat();VideoCapture capture=new VideoCapture(0);int height =(int)capture.get(Videoio.CV_CAP_PROP_FRAME_HEIGHT);int width =(int)capture.get(Videoio.CV_CAP_PROP_FRAME_WIDTH);if(height==0||width==0){throw new Exception("camera not found");}JFrame frame=new JFrame("camera");frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);DetectObjectsCamera panel=new DetectObjectsCamera();frame.setContentPane(panel);frame.setVisible(true);frame.setSize(width+frame.getInsets().left+frame.getInsets().right, height+frame.getInsets().top+frame.getInsets().bottom);MatToBufferedImage matToBi=new MatToBufferedImage();Mat dst=new Mat();while(frame.isShowing()){capture.read(capImg);dst=dobj(objDetector,capImg);panel.mImg=matToBi.mat2BI(dst);panel.repaint();}capture.release();}catch(Exception e){System.out.println("例外"+e);}finally{System.out.println("--done--");}}
}
这篇关于opencv3_java 摄像头图形图像检测DetectObjectsCamera videoioVideoCapture的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!