5

我正在开发应用程序,其中我必须检测矩形对象并绘制轮廓我正在使用 Open cv 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);

对检测 android 的正方形/矩形有任何帮助......我想知道从 2 天开始......每个例子都是 C++ 或 C++,我无法通过那些语言......

谢谢。

4

2 回答 2

1

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

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

  1. 灰度图像

3.应用高斯模糊

4.应用形态学填充孔(如果有)

5.应用 Canny 检测

6.查找图像的轮廓

7.找到其余的最大轮廓

8.画出最大的轮廓。

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

Utils.bitmapToMat(image,src)
  1. 灰度图像
val gray = Mat(src.rows(), src.cols(), src.type())
  Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY)

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)

5.应用 Canny 检测

   val edges = Mat(src.rows(), src.cols(), src.type())
   Imgproc.Canny(gray, edges, 75.0, 200.0)

6.查找图像的轮廓

   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.画出矩形的最大轮廓

  Imgproc.drawContours(src, contours, idx, Scalar(0.0, 255.0, 0.0), 3)

在那里你找到了矩形。如果在获取过程中仍然存在任何错误。尝试将源图像的大小调整为其高度和宽度的一半。

请查看以下链接以获取上述说明的正确 Java 代码 https://github.com/dhananjay-91/DetectRectangle 另外, https://github.com/aashari/android-opencv-rectangle-detector

于 2020-10-12T11:33:47.017 回答
0

通过使用 Houghtransformation,您走在了正确的道路上。您必须使用 Houghlines 并检查获得的线是否有交叉点,而不是使用 Houghcircles。如果你真的必须找到矩形(而不是 4 个边缘的多边形) - 你应该寻找具有相同角度的线(+ - 一个小的偏移量),如果你找到至少一对这样的线,你必须寻找铺设的线垂直于此,也找到一对并检查交叉点。使用向量(端点 - 起点)和线来执行角度和相交测试应该没什么大不了的。

于 2017-02-01T14:31:38.823 回答