Android用opencv识别红绿灯,求完整的基于opencv的交通信号灯检测与识别的代码

OpenCV整个项目的结构图:编写DetectFaceDemo.java,代码如下:[java]viewplaincopyprint?packagecom.njupt.zhb.test;importorg.opencv.core.Core;importorg.opencv.core.Mat;importorg.opencv.core.MatOfRect;importorg.opencv.core.Point;importorg.opencv.core.Rect;importorg.opencv.core.Scalar;importorg.opencv.highgui.Highgui;importorg.opencv.objdetect.CascadeClassifier;Detectsfacesinanimage,drawsboxesaroundthem,andwritestheresults//to"faceDetection.png".//publicclassDetectFaceDemo{publicvoidrun(){System.out.println("\nRunningDetectFaceDemo");System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());//Createafacedetectorfromthecascadefileintheresources//directory.//CascadeClassifierfaceDetector=newCascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());//Matimage=Highgui.imread(getClass().getResource("lena.png").getPath());//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误/**Detected0facesWritingfaceDetection.pnglibpngwarning:Image*widthiszeroinIHDRlibpngwarning:ImageheightiszeroinIHDR*libpngerror:InvalidIHDRdata*///因此,我们将第一个字符去掉StringxmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);CascadeClassifierfaceDetector=newCascadeClassifier(xmlfilePath);Matimage=Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));//Detectfacesintheimage.//MatOfRectisaspecialcontainerclassforRect.MatOfRectfaceDetections=newMatOfRect();faceDetector.detectMultiScale(image,faceDetections);System.out.println(String.format("Detected%sfaces",faceDetections.toArray().length));//Drawaboundingboxaroundeachface.for(Rectrect:faceDetections.toArray()){Core.rectangle(image,newPoint(rect.x,rect.y),newPoint(rect.x+rect.width,rect.y+rect.height),newScalar(0,255,0));}//Savethevisualizeddetection.Stringfilename="faceDetection.png";System.out.println(String.format("Writing%s",filename));Highgui.imwrite(filename,image);}}packagecom.njupt.zhb.test;importorg.opencv.core.Core;importorg.opencv.core.Mat;importorg.opencv.core.MatOfRect;importorg.opencv.core.Point;importorg.opencv.core.Rect;importorg.opencv.core.Scalar;importorg.opencv.highgui.Highgui;importorg.opencv.objdetect.CascadeClassifier;Detectsfacesinanimage,drawsboxesaroundthem,andwritestheresults//to"faceDetection.png".//publicclassDetectFaceDemo{publicvoidrun(){System.out.println("\nRunningDetectFaceDemo");System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());//Createafacedetectorfromthecascadefileintheresources//directory.//CascadeClassifierfaceDetector=newCascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());//Matimage=Highgui.imread(getClass().getResource("lena.png").getPath());//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误/**Detected0facesWritingfaceDetection.pnglibpngwarning:Image*widthiszeroinIHDRlibpngwarning:ImageheightiszeroinIHDR*libpngerror:InvalidIHDRdata*///因此,我们将第一个字符去掉StringxmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);CascadeClassifierfaceDetector=newCascadeClassifier(xmlfilePath);Matimage=Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));//Detectfacesintheimage.//MatOfRectisaspecialcontainerclassforRect.MatOfRectfaceDetections=newMatOfRect();faceDetector.detectMultiScale(image,faceDetections);System.out.println(String.format("Detected%sfaces",faceDetections.toArray().length));//Drawaboundingboxaroundeachface.for(Rectrect:faceDetections.toArray()){Core.rectangle(image,newPoint(rect.x,rect.y),newPoint(rect.x+rect.width,rect.y+rect.height),newScalar(0,255,0));}//Savethevisualizeddetection.Stringfilename="fac

eDetection.png";System.out.println(String.format("Writing%s",filename));Highgui.imwrite(filename,image);}}3.编写测试类:[java]viewplaincopyprint?packagecom.njupt.zhb.test;publicclassTestMain{publicstaticvoidmain(String[]args){System.out.println("Hello,OpenCV");//Loadthenativelibrary.System.loadLibrary("opencv_java246");newDetectFaceDemo().run();}}//运行结果://Hello,OpenCVRunningDetectFaceDemo///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml//Detected8faces//WritingfaceDetection.pngpackagecom.njupt.zhb.test;publicclassTestMain{publicstaticvoidmain(String[]args){System.out.println("Hello,OpenCV");//Loadthenativelibrary.System.loadLibrary("opencv_java246");newDetectFaceDemo().run();}}//运行结果://Hello,OpenCVRunningDetectFaceDemo///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml//Detected8faces//WritingfaceDetection.png

阅读全文 >

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值