package opencvImpl;
import org.opencv.core.Core;
import org.opencv.core.Mat;
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.objdetect.CascadeClassifier;
import org.opencv.video.Video;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.Videoio;
import opencvImpl.view.ImageViewer;
import opencvImpl.view.VideoViewer;
public class VideoTest {
static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
public static void main(String args[]){
VideoViewer videoViewer=new VideoViewer();
videoViewer.createJFrame("sky");
Mat matImg=new Mat();
VideoCapture videoCapture=new VideoCapture(0);
videoCapture.set(Videoio.CAP_PROP_FRAME_WIDTH, 1120);
videoCapture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 840);
if(videoCapture.isOpened()){
CascadeClassifier facedectector=new CascadeClassifier("D:/IMGprocesingOpenCV/opencvv3/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");
CascadeClassifier eyedetecter=new CascadeClassifier("D:/IMGprocesingOpenCV/opencvv3/opencv/sources/data/haarcascades/haarcascade_eye.xml");
CascadeClassifier smiledetecter=new CascadeClassifier("D:/IMGprocesingOpenCV/opencvv3/opencv/sources/data/haarcascades/haarcascade_smile.xml");
MatOfRect faceDetections = new MatOfRect();
MatOfRect eyeDetections = new MatOfRect();
MatOfRect smileDetections = new MatOfRect();
while(true){
videoCapture.read(matImg);
facedectector.detectMultiScale(matImg, faceDetections);
for(Rect rect:faceDetections.toArray()){
Imgproc.rectangle(matImg, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
eyedetecter.detectMultiScale(matImg, eyeDetections);
for(Rect rect:eyeDetections.toArray()){
Imgproc.rectangle(matImg, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
// smiledetecter.detectMultiScale(matImg, smileDetections);
// for(Rect rect:smileDetections.toArray()){
// Imgproc.rectangle(matImg, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
//
// }
videoViewer.show(matImg);
}
}else {
System.out.println("Err in CAMERA IO");
}
}
}
package opencvImpl.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import org.opencv.core.Mat;
public class VideoViewer {
private JLabel imageView;
public void show(Mat image){
show(image, "");
}
public void show(Mat image,String windowName){
setSystemLookAndFeel();
// JFrame frame = createJFrame(windowName);
Image loadedImage = toBufferedImage(image);
imageView.setIcon(new ImageIcon(loadedImage));
// frame.pack();
// frame.setLocationRelativeTo(null);
// frame.setVisible(true);
}
public JFrame createJFrame(String windowName) {
JFrame frame = new JFrame(windowName);
imageView = new JLabel();
final JScrollPane imageScrollPane = new JScrollPane(imageView);
imageScrollPane.setPreferredSize(new Dimension(640, 480));
frame.add(imageScrollPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
return frame;
}
private void setSystemLookAndFeel() {
try {
UIManager.setLookAndFeel
(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
public Image toBufferedImage(Mat matrix){
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( matrix.channels() > 1 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = matrix.channels()*matrix.cols()*matrix.rows();
byte [] buffer = new byte[bufferSize];
matrix.get(0,0,buffer); // get all the pixels
BufferedImage image = new BufferedImage(matrix.cols(),matrix.
rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(buffer, 0, targetPixels, 0, buffer.length);
return image;
}
}
OutPut
No comments:
Post a Comment