0

我编写了代码来检测打开的 cv 中的矩形。而且我能够检测到很少的物体,但我无法检测到物理门或大矩形。如果我在某处错了,请检查我的代码并纠正我。另一个问题是这段代码无法不断检测矩形,所以当我绘制矩形时,它来来去去,来来去去,看起来很糟糕。在每一帧中定期检测的任何方法。

 Mat  output= getGray(inputFrame.rgba(),inputFrame.rgba());
        Imgproc.medianBlur(output, output, 5);
        Imgproc.erode(output, output, new Mat());
        Imgproc.dilate(output, output, new Mat());
         Mat edges = new Mat();
        Imgproc.Canny(output, output, 5, 50);
//      Vector<MatOfPoint> vector=new Vector<MatOfPoint>();
//      Imgproc.findContours(output, points, output, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
         contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();
        contours.clear();
        Imgproc.findContours(output, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

 MatOfPoint2f approxCurve = new MatOfPoint2f();
        rgbImage=inputFrame.rgba();
        mDrawnContours.clear();

> Blockquote

 for(int i=0;i< contours.size();i++){
            MatOfPoint tempContour=contours.get(i);
            MatOfPoint2f newMat = new MatOfPoint2f( tempContour.toArray() );
            int contourSize = (int)tempContour.total();
            Imgproc.approxPolyDP(newMat, approxCurve, contourSize*0.15, true);
            MatOfPoint points=new MatOfPoint(approxCurve.toArray());

            if((Math.abs(Imgproc.contourArea(tempContour))<100) || !Imgproc.isContourConvex(points)){
                Log.i(TAG, "::onCameraFrame:" + " too small");
                appendLog("Too small");
                continue;
            }
            else if(points.toArray().length >= 4 && points.toArray().length <= 6){
                int vtc = points.toArray().length;
                Vector<Double> cosList=new Vector<Double>();
                for (int j = 2; j < vtc+1; j++){

                    cosList.add(angle(points.toArray()[j%vtc], points.toArray()[j-2], points.toArray()[j-1]));

                }   
                   double mincos = getMin(cosList);
                   double maxcos = getMax(cosList);
                   Log.i(TAG, "::onCameraFrame:" + "mincos:"+mincos+"maxcos:"+maxcos);
                   if (vtc == 4 && mincos >= -0.1 && maxcos <= 0.3)
                   {
                       mTotalSquare++;

                        Imgproc.drawContours(rgbImage, contours, i, new Scalar(0,0,255));
                        DrawnContours contours2=new DrawnContours();
                        contours2.setIndex(i);
                        mDrawnContours.add(contours2);
                        Log.i(TAG, "::onCameraFrame:" + "found");
                        appendLog("found");

                   }
                   else{
                       Log.i(TAG, "::onCameraFrame:" +" not found " +"mincos:"+mincos+"maxcos:"+maxcos);
                       appendLog("not found 1");
                   }

            }

return rgbImage

如果您有任何问题,请告诉我。

4

1 回答 1

0

我想,大轮廓有超过 4 个边缘。它们的轮廓由大量短线段组成(取决于线中的近似函数参数

 Imgproc.approxPolyDP(newMat, approxCurve, contourSize*0.15, true);

)。

你有条件检查边号:

points.toArray().length <= 6
于 2013-11-12T19:19:14.553 回答