本博客为老魏原创,如需转载请留言咨询。
效果预览:(没办法,为了效果只能上像了,丑别介意。哈哈。。)
上代码:
1 package com.lw.test;
2
3 import java.awt.Graphics;
4 import java.awt.event.MouseAdapter;
5 import java.awt.event.MouseEvent;
6 import java.awt.image.BufferedImage;
7
8 import javax.swing.JFrame;
9 import javax.swing.JPanel;
10 import javax.swing.WindowConstants;
11
12 import org.opencv.core.Core;
13 import org.opencv.core.Mat;
14 import org.opencv.core.MatOfDouble;
15 import org.opencv.core.MatOfRect;
16 import org.opencv.core.Point;
17 import org.opencv.core.Rect;
18 import org.opencv.core.Scalar;
19 import org.opencv.imgproc.Imgproc;
20 import org.opencv.ml.SVM;
21 import org.opencv.objdetect.CascadeClassifier;
22 import org.opencv.objdetect.HOGDescriptor;
23 import org.opencv.videoio.VideoCapture;
24 import org.opencv.videoio.Videoio;
25
26 public class CaptureBasic extends JPanel {
27
28 private BufferedImage mImg;
29
30 private BufferedImage mat2BI(Mat mat){
31 int dataSize =mat.cols()*mat.rows()*(int)mat.elemSize();
32 byte[] data=new byte[dataSize];
33 mat.get(0, 0,data);
34 int type=mat.channels()==1?
35 BufferedImage.TYPE_BYTE_GRAY:BufferedImage.TYPE_3BYTE_BGR;
36
37 if(type==BufferedImage.TYPE_3BYTE_BGR){
38 for(int i=0;i<dataSize;i+=3){
39 byte blue=data[i+0];
40 data[i+0]=data[i+2];
41 data[i+2]=blue;
42 }
43 }
44 BufferedImage image=new BufferedImage(mat.cols(),mat.rows(),type);
45 image.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);
46
47 return image;
48 }
49
50 public void paintComponent(Graphics g){
51 if(mImg!=null){
52 g.drawImage(mImg, 0, 0, mImg.getWidth(),mImg.getHeight(),this);
53 }
54 }
55
56 public static void main(String[] args) {
57 try{
58 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
59
60 Mat capImg=new Mat();
61 VideoCapture capture=new VideoCapture(0);
62 int height = (int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);
63 int width = (int)capture.get(Videoio.CAP_PROP_FRAME_WIDTH);
64 if(height==0||width==0){
65 throw new Exception("camera not found!");
66 }
67
68 JFrame frame=new JFrame("camera");
69 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
70 CaptureBasic panel=new CaptureBasic();
71 panel.addMouseListener(new MouseAdapter() {
72 @Override
73 public void mouseClicked(MouseEvent arg0) {
74 System.out.println("click");
75 }
76 @Override
77 public void mouseMoved(MouseEvent arg0) {
78 System.out.println("move");
79
80 }
81 @Override
82 public void mouseReleased(MouseEvent arg0) {
83 System.out.println("mouseReleased");
84 }
85 @Override
86 public void mousePressed(MouseEvent arg0) {
87 System.out.println("mousePressed");
88 }
89 @Override
90 public void mouseExited(MouseEvent arg0) {
91 System.out.println("mouseExited");
92 //System.out.println(arg0.toString());
93 }
94 @Override
95 public void mouseDragged(MouseEvent arg0) {
96 System.out.println("mouseDragged");
97 //System.out.println(arg0.toString());
98 }
99
100 });
101 frame.setContentPane(panel);
102 frame.setVisible(true);
103 frame.setSize(width+frame.getInsets().left+frame.getInsets().right,
104 height+frame.getInsets().top+frame.getInsets().bottom);
105 int n=0;
106 Mat temp=new Mat();
107 while(frame.isShowing()&& n<500){
108 //System.out.println("第"+n+"张");
109 capture.read(capImg);
110 Imgproc.cvtColor(capImg, temp, Imgproc.COLOR_RGB2GRAY);
111 //Imgcodecs.imwrite("G:/opencv/lw/neg/back"+n+".png", temp);
112 panel.mImg=panel.mat2BI(detectFace(capImg));
113 panel.repaint();
114 //n++;
115 //break;
116 }
117 capture.release();
118 frame.dispose();
119 }catch(Exception e){
120 System.out.println("例外:" + e);
121 }finally{
122 System.out.println("--done--");
123 }
124
125 }
126 /**
127 * opencv实现人脸识别
128 * @param img
129 */
130 public static Mat detectFace(Mat img) throws Exception
131 {
132
133 System.out.println("Running DetectFace ... ");
134 // 从配置文件lbpcascade_frontalface.xml中创建一个人脸识别器,该文件位于opencv安装目录中
135 CascadeClassifier faceDetector = new CascadeClassifier("D:\\TDDOWNLOAD\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
136
137
138 // 在图片中检测人脸
139 MatOfRect faceDetections = new MatOfRect();
140
141 faceDetector.detectMultiScale(img, faceDetections);
142
143 //System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
144
145 Rect[] rects = faceDetections.toArray();
146 if(rects != null && rects.length >= 1){
147 for (Rect rect : rects) {
148 Imgproc.rectangle(img, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
149 new Scalar(0, 0, 255), 2);
150 }
151 }
152 return img;
153 }
154
155
156 /**
157 * opencv实现人型识别,hog默认的分类器。所以效果不好。
158 * @param img
159 */
160 public static Mat detectPeople(Mat img) {
161 //System.out.println("detectPeople...");
162 if (img.empty()) {
163 System.out.println("image is exist");
164 }
165 HOGDescriptor hog = new HOGDescriptor();
166 hog.setSVMDetector(HOGDescriptor.getDefaultPeopleDetector());
167 System.out.println(HOGDescriptor.getDefaultPeopleDetector());
168 //hog.setSVMDetector(HOGDescriptor.getDaimlerPeopleDetector());
169 MatOfRect regions = new MatOfRect();
170 MatOfDouble foundWeights = new MatOfDouble();
171 //System.out.println(foundWeights.toString());
172 hog.detectMultiScale(img, regions, foundWeights);
173 for (Rect rect : regions.toArray()) {
174 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);
175 }
176 return img;
177 }
178
179 }