java jmf变成_用JavaVC替换JMF连接摄像头

importjava.io.File;importjava.net.URL;importorg.bytedeco.javacv.*;importorg.bytedeco.javacpp.*;importorg.bytedeco.javacpp.indexer.*;importstaticorg.bytedeco.javacpp.opencv_core.*;importstaticorg.bytedeco.javacpp.opencv_imgproc.*;importstaticorg.bytedeco.javacpp.opencv_calib3d.*;importstaticorg.bytedeco.javacpp.opencv_objdetect.*;publicclassDemo {publicstaticvoidmain(String[] args)throwsException {

String classifierName=null;if(args.length>0) {

classifierName=args[0];

}else{

URL url=newURL("https://raw.github.com/Itseez/opencv/2.4/data/haarcascades/haarcascade_frontalface_alt.xml");

File file=Loader.extractResource(url,null,"classifier",".xml");

file.deleteOnExit();

classifierName=file.getAbsolutePath();

}//Preload the opencv_objdetect module to work around a known bug.Loader.load(opencv_objdetect.class);//We can "cast" Pointer objects by instantiating a new object of the desired class.CvHaarClassifierCascade classifier=newCvHaarClassifierCascade(cvLoad(classifierName));if(classifier.isNull()) {

System.err.println("Error loading classifier file \""+ classifierName +"\".");

System.exit(1);

}//The available FrameGrabber classes include OpenCVFrameGrabber (opencv_highgui),//DC1394FrameGrabber, FlyCaptureFrameGrabber, OpenKinectFrameGrabber,//PS3EyeFrameGrabber, VideoInputFrameGrabber, and FFmpegFrameGrabber.FrameGrabber grabber=FrameGrabber.createDefault(0);

grabber.start();//FAQ about IplImage://- For custom raw processing of data, createBuffer() returns an NIO direct//buffer wrapped around the memory pointed by imageData, and under Android we can//also use that Buffer with Bitmap.copyPixelsFromBuffer() and copyPixelsToBuffer().//- To get a BufferedImage from an IplImage, we may call getBufferedImage().//- The createFrom() factory method can construct an IplImage from a BufferedImage.//- There are also a few copy*() methods for BufferedImageIplImage data transfers.IplImage grabbedImage=grabber.grab();intwidth=grabbedImage.width();intheight=grabbedImage.height();

IplImage grayImage=IplImage.create(width, height, IPL_DEPTH_8U,1);

IplImage rotatedImage=grabbedImage.clone();//Objects allocated with a create*() or clone() factory method are automatically released//by the garbage collector, but may still be explicitly released by calling release().//You shall NOT call cvReleaseImage(), cvReleaseMemStorage(), etc. on objects allocated this way.CvMemStorage storage=CvMemStorage.create();//The OpenCVFrameRecorder class simply uses the CvVideoWriter of opencv_highgui,//but FFmpegFrameRecorder also exists as a more versatile alternative.FrameRecorder recorder=FrameRecorder.createDefault("output.avi", width, height);

recorder.start();//CanvasFrame is a JFrame containing a Canvas component, which is hardware accelerated.//It can also switch into full-screen mode when called with a screenNumber.//We should also specify the relative monitor/camera response for proper gamma correction.CanvasFrame frame=newCanvasFrame("Some Title", CanvasFrame.getDefaultGamma()/grabber.getGamma());//Let's create some random 3D rotation

9b8a8a44dd1c74ae49c20a7cd451974e.pngCvMat randomR=CvMat.create(3,3), randomAxis=CvMat.create(3,1);//We can easily and efficiently access the elements of matrices and images//through an Indexer object with the set of get() and put() methods.DoubleIndexer Ridx=randomR.createIndexer(), axisIdx=randomAxis.createIndexer();

axisIdx.put(0, (Math.random()-0.5)/4, (Math.random()-0.5)/4, (Math.random()-0.5)/4);

cvRodrigues2(randomAxis, randomR,null);doublef=(width+height)/2.0;  Ridx.put(0,2, Ridx.get(0,2)*f);

Ridx.put(1,2, Ridx.get(1,2)*f);

Ridx.put(2,0, Ridx.get(2,0)/f); Ridx.put(2,1, Ridx.get(2,1)/f);

System.out.println(Ridx);//We can allocate native arrays using constructors taking an integer as argument.CvPoint hatPoints=newCvPoint(3);while(frame.isVisible()&&(grabbedImage=grabber.grab())!=null) {

cvClearMemStorage(storage);//Let's try to detect some faces! but we need a grayscale image

9b8a8a44dd1c74ae49c20a7cd451974e.pngcvCvtColor(grabbedImage, grayImage, CV_BGR2GRAY);

CvSeq faces=cvHaarDetectObjects(grayImage, classifier, storage,1.1,3, CV_HAAR_DO_CANNY_PRUNING);inttotal=faces.total();for(inti=0; i

CvRect r=newCvRect(cvGetSeqElem(faces, i));intx=r.x(), y=r.y(), w=r.width(), h=r.height();

cvRectangle(grabbedImage, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.RED,1, CV_AA,0);//To access or pass as argument the elements of a native array, call position() before.hatPoints.position(0).x(x-w/10)   .y(y-h/10);

hatPoints.position(1).x(x+w*11/10).y(y-h/10);

hatPoints.position(2).x(x+w/2)    .y(y-h/2);

cvFillConvexPoly(grabbedImage, hatPoints.position(0),3, CvScalar.GREEN, CV_AA,0);

}//Let's find some contours! but first some thresholding

9b8a8a44dd1c74ae49c20a7cd451974e.pngcvThreshold(grayImage, grayImage,64,255, CV_THRESH_BINARY);//To check if an output argument is null we may call either isNull() or equals(null).CvSeq contour=newCvSeq(null);

cvFindContours(grayImage, storage, contour, Loader.sizeof(CvContour.class),

CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);while(contour!=null&&!contour.isNull()) {if(contour.elem_size()>0) {

CvSeq points=cvApproxPoly(contour, Loader.sizeof(CvContour.class),

storage, CV_POLY_APPROX_DP, cvContourPerimeter(contour)*0.02,0);

cvDrawContours(grabbedImage, points, CvScalar.BLUE, CvScalar.BLUE,-1,1, CV_AA);

}

contour=contour.h_next();

}

cvWarpPerspective(grabbedImage, rotatedImage, randomR);

frame.showImage(rotatedImage);

recorder.record(rotatedImage);

}

frame.dispose();

recorder.stop();

grabber.stop();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值