/** * 人脸识别 * @throws IOException */ private CascadeClassifier faceDetector; private void initFaceDetector() throws IOException{ InputStream input = getResources().openRawResource(R.raw.haarcascade_upperbody); File cascadeDir = this.getDir("cascade", Context.MODE_PRIVATE); File file = new File(cascadeDir.getAbsoluteFile(),"haarcascade_upperbody.xml"); FileOutputStream output = new FileOutputStream(file); byte[] buff = new byte[1024]; int len = 0; while ((len = input.read(buff))!= -1){ output.write(buff,0,len); } input.close(); output.close(); faceDetector = new CascadeClassifier(file.getAbsolutePath()); } /** * 人脸识别 * @param detector */ private void faceDetect(CascadeClassifier detector){ Bitmap temp = selectBitmap.copy(selectBitmap.getConfig(),true); Mat src = new Mat(); Mat dst = new Mat(); Utils.bitmapToMat(temp,src); Imgproc.cvtColor(src,dst,Imgproc.COLOR_BGRA2GRAY); MatOfRect faces = new MatOfRect(); detector.detectMultiScale(dst,faces,1.1,15,0,new Size(50,50),new Size()); List<Rect> faceList = faces.toList(); if (faceList.size() >0){ for (Rect rect:faceList){ Imgproc.rectangle(src,rect.tl(),rect.br(),new Scalar(255,0,0,255),2,8,0); } } Utils.matToBitmap(src,temp); src.release(); dst.release(); mImageView.setImageBitmap(temp); } /** * 对象测量 */ private void measureObjects(){ Bitmap temp = selectBitmap.copy(selectBitmap.getConfig(),true); Mat src = new Mat(); Mat dst = new Mat(); Utils.bitmapToMat(temp,src); Imgproc.cvtColor(src,src,Imgproc.COLOR_BGRA2GRAY); int t = 100; Imgproc.Canny(src,dst,t,t*2,3,false); List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(dst,contours,hierarchy,Imgproc.RETR_TREE,Imgproc.CHAIN_APPROX_SIMPLE,new Point(0,0)); Imgproc.cvtColor(src,src,Imgproc.COLOR_GRAY2BGR); double[][] result = new double[contours.size()][2]; for(int i = 0;i <contours.size();i++){ Moments moments = Imgproc.moments(contours.get(i),false); double m00 = moments.get_m00(); double m10 = moments.get_m10(); double m01 = moments.get_m01(); double x0 = m10 /m00; double y0 = m01 /m00; double arclength = Imgproc.arcLength(new MatOfPoint2f(contours.get(i).toArray()),true); double area = Imgproc.contourArea(contours.get(i)); result[i][0] = arclength; result[i][1] = area; Log.d("liubin11 i = ",""+i); Log.d("liubin11 arclength = ",""+arclength); Log.d("liubin11 area = ",""+area); Imgproc.circle(src,new Point(x0,y0),2,new Scalar(255,0,0),2,8,0); } Utils.matToBitmap(src,temp); src.release(); dst.release(); hierarchy.release(); mImageView.setImageBitmap(temp); } /** * 轮廓发现 */ private void findAndDrawContours(){ Bitmap temp = selectBitmap.copy(selectBitmap.getConfig(),true); Mat src = new Mat(); Mat dst = new Mat(); Utils.bitmapToMat(temp,src); Imgproc.cvtColor(src,src,Imgproc.COLOR_BGRA2GRAY); int t = 90; Imgproc.Canny(src,dst,t,t*2,3,false); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours(dst,contours,hierarchy,Imgproc.RETR_TREE,Imgproc.CHAIN_APPROX_SIMPLE,new Point(0,0)); Imgproc.cvtColor(src,src,Imgproc.COLOR_GRAY2BGR); for (int i =0;i<contours.size();i++){ MatOfPoint points = contours.get(i); Imgproc.drawContours(src,contours,i,new Scalar(255,0,0),2,8,hierarchy,0,new Point(0,0)); } Utils.matToBitmap(src,temp); src.release(); dst.release(); hierarchy.release(); mImageView.setImageBitmap(temp); } /** * 模板匹配 */ private void templateMat(){ Bitmap tpl = BitmapFactory.decodeResource(this.getResources(),R.drawable.fj); Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.fjy); Mat src = new Mat(); Mat tplMat = new Mat(); Utils.bitmapToMat(bitmap,src); Utils.bitmapToMat(tpl,tplMat); int width = bitmap.getWidth() - tpl.getWidth() +1; int height = bitmap.getHeight() - tpl.getHeight() +1; Mat result = new Mat(width,height,CvType.CV_32FC1);//result 必须为单通道32位 Imgproc.matchTemplate(src,tplMat,result,Imgproc.TM_CCORR_NORMED); Core.normalize(result,result,0,1.0,Core.NORM_MINMAX,-1); Core.Mi
Open CV 3.4基础API使用
最新推荐文章于 2024-01-08 14:14:42 发布