Open CV 3.4基础API使用

    /**
     * 人脸识别
     * @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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值