在android中检测图像中的矩形并使用open cv绘制轮廓

在android中检测图像中的矩形并使用open cv绘制轮廓,android,opencv,Android,Opencv,我正在开发一个应用程序,在这个应用程序中,我必须检测矩形对象并绘制轮廓,我正在使用OpenCV android库 我成功地检测到圆并在图像内部绘制轮廓,但多次未能检测到正方形或矩形并绘制……以下是我的代码,用于绘制圆 Bitmap imageBmp = BitmapFactory.decodeResource(MainActivityPDF.this.getResources(),R.drawable.loadingplashscreen); Mat imgSource = new Mat()

我正在开发一个应用程序,在这个应用程序中,我必须检测矩形对象并绘制轮廓,我正在使用OpenCV android库

我成功地检测到圆并在图像内部绘制轮廓,但多次未能检测到正方形或矩形并绘制……以下是我的代码,用于绘制圆

Bitmap imageBmp = BitmapFactory.decodeResource(MainActivityPDF.this.getResources(),R.drawable.loadingplashscreen);

Mat imgSource = new Mat(), imgCirclesOut = new Mat();

Utils.bitmapToMat(imageBmp , imgSource);

    //grey opencv
Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);

Imgproc.GaussianBlur( imgSource, imgSource, new Size(9, 9), 2, 2 );
Imgproc.HoughCircles( imgSource, imgCirclesOut, Imgproc.CV_HOUGH_GRADIENT, 1, imgSource.rows()/8, 200, 100, 0, 0 );

float circle[] = new float[3];

for (int i = 0; i < imgCirclesOut.cols(); i++)
{
        imgCirclesOut.get(0, i, circle);
    org.opencv.core.Point center = new org.opencv.core.Point();
    center.x = circle[0];
    center.y = circle[1];
    Core.circle(imgSource, center, (int) circle[2], new Scalar(255,0,0,255), 4);
    }
    Bitmap bmp = Bitmap.createBitmap(imageBmp.getWidth(), imageBmp.getHeight(), Bitmap.Config.ARGB_8888);

    Utils.matToBitmap(imgSource, bmp);


    ImageView frame = (ImageView) findViewById(R.id.imageView1);

    //Bitmap bmp = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    frame.setImageBitmap(bmp);
Bitmap-imageBmp=BitmapFactory.decodeResource(mainActivityDF.this.getResources(),R.drawable.loadingplashscreen);
Mat imgSource=new Mat(),imgCirclesOut=new Mat();
Utils.bitmapToMat(imageBmp,imgSource);
//灰色opencv
Imgproc.cvt颜色(imgSource,imgSource,Imgproc.COLOR\u bgr2灰色);
Imgproc.GaussianBlur(imgSource,imgSource,新尺寸(9,9),2,2);
Imgproc.HoughCircles(imgSource,imgCircleOut,Imgproc.CV_HOUGH_GRADIENT,1,imgSource.rows()/82000100,0,0);
浮动圆[]=新浮动[3];
for(int i=0;i
<> Po>为Android检测平方/矩形……我想2天内…每个例子都是C++或C++,我不能通过这些语言…< /P>
谢谢。

您使用HoughtTransformation的方法是正确的。您必须使用Houghlines并检查获得的线的交点,而不是使用Houghleels。如果你真的必须找到矩形(而不是四边多边形),你应该寻找具有相同角度(+-一个小偏移)的线,如果你至少找到了一对这样的线,你必须寻找与之垂直的线,同时找到一对并检查交叉点。使用矢量(端点-起点)和直线来执行角度和交点测试应该不是什么大问题。

使用opencv检测矩形有很多方法,最合适的方法是在应用Canny边缘检测后找到轮廓

步骤如下:- 1.将图像转换为MAT

Utils.bitmapToMat(image,src)
  • 对图像进行灰度处理
  • 3.应用高斯模糊

    Imgproc.GaussianBlur(gray, gray, Size(5.0, 5.0), 0.0)
    
    4.应用形态学填充孔(如有)

    5.应用Canny检测

       val edges = Mat(src.rows(), src.cols(), src.type())
       Imgproc.Canny(gray, edges, 75.0, 200.0)
    
    6.找到图像的轮廓

           val kernel = Imgproc.getStructuringElement(
               Imgproc.MORPH_ELLIPSE, Size(
                   5.0,
                   5.0
               )
           )
           Imgproc.morphologyEx(
               gray,
               gray,
               Imgproc.MORPH_CLOSE,
               kernel
           ) // fill holes
           Imgproc.morphologyEx(
               gray,
               gray,
               Imgproc.MORPH_OPEN,
               kernel
           ) //remove noise
           Imgproc.dilate(gray, gray, kernel)
    
       val contours = ArrayList<MatOfPoint>()
            val hierarchy = Mat()
            Imgproc.findContours(
                edges, contours, hierarchy, Imgproc.RETR_LIST,
                Imgproc.CHAIN_APPROX_SIMPLE
            )
    
    7.找出其余部分的最大轮廓

      public int findLargestContour(ArrayList<MatOfPoint> contours) {
    
            double maxVal = 0;
            int maxValIdx = 0;
            for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
                double contourArea = Imgproc.contourArea(contours.get(contourIdx));
                if (maxVal < contourArea) {
                    maxVal = contourArea;
                    maxValIdx = contourIdx;
                }
            }
    
    
            return maxValIdx;
    
        }
    
    8.画出最大的轮廓线

    代码如下- 1.将图像转换为MAT

    Utils.bitmapToMat(image,src)
    
  • 对图像进行灰度处理
  • 3.应用高斯模糊

    Imgproc.GaussianBlur(gray, gray, Size(5.0, 5.0), 0.0)
    
    4.应用形态学填充孔洞(如有),并放大图像

           val kernel = Imgproc.getStructuringElement(
               Imgproc.MORPH_ELLIPSE, Size(
                   5.0,
                   5.0
               )
           )
           Imgproc.morphologyEx(
               gray,
               gray,
               Imgproc.MORPH_CLOSE,
               kernel
           ) // fill holes
           Imgproc.morphologyEx(
               gray,
               gray,
               Imgproc.MORPH_OPEN,
               kernel
           ) //remove noise
           Imgproc.dilate(gray, gray, kernel)
    
       val contours = ArrayList<MatOfPoint>()
            val hierarchy = Mat()
            Imgproc.findContours(
                edges, contours, hierarchy, Imgproc.RETR_LIST,
                Imgproc.CHAIN_APPROX_SIMPLE
            )
    
    5.应用Canny检测

       val edges = Mat(src.rows(), src.cols(), src.type())
       Imgproc.Canny(gray, edges, 75.0, 200.0)
    
    6.找到图像的轮廓

           val kernel = Imgproc.getStructuringElement(
               Imgproc.MORPH_ELLIPSE, Size(
                   5.0,
                   5.0
               )
           )
           Imgproc.morphologyEx(
               gray,
               gray,
               Imgproc.MORPH_CLOSE,
               kernel
           ) // fill holes
           Imgproc.morphologyEx(
               gray,
               gray,
               Imgproc.MORPH_OPEN,
               kernel
           ) //remove noise
           Imgproc.dilate(gray, gray, kernel)
    
       val contours = ArrayList<MatOfPoint>()
            val hierarchy = Mat()
            Imgproc.findContours(
                edges, contours, hierarchy, Imgproc.RETR_LIST,
                Imgproc.CHAIN_APPROX_SIMPLE
            )
    
    你已经找到矩形了。 如果获取过程中仍然存在任何错误,请尝试将源图像的大小调整为其高度和宽度的一半

    请查看下面的链接,以获取上述内容的正确Java代码 也,

    您是在寻找轴对齐的矩形还是可以旋转的矩形?谢谢您的回复……kindley给我一个轴对齐的矩形的想法……请注意,例如,蓝色的是轴对齐的,红色的不是。谢谢你澄清我的疑问。实际上,我的问题是,如果我从相机拍摄图像,那么我必须检测矩形,它可以位于任何位置,如对齐或手写……那么你有任何链接或示例吗..在安卓中。。Thanks@Deepankar你找到解决问题的办法了吗?