简体   繁体   English

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

[英]Detect Rectangle in image and draw outline using open cv in android

I am developing application in which I have to detect rectangular object and draw outline I am using Open cv android library....我正在开发应用程序,我必须在其中检测矩形对象并绘制轮廓我正在使用 Open cv android 库....

I succesfully detect Circle and draw outline inside image but repeatedly fail to detect Square or rectangle and draw....Here is my code to for circle..我成功检测到圆形并在图像内绘制轮廓,但反复无法检测方形或矩形并绘制....这是我的圆形代码..

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);

any help for detect square/rectangle for android ......I am wondering from 2 days ..every example are in either C++ or in C++ and I can't get through that languages...检测 android 正方形/矩形的任何帮助......我想知道从 2 天开始......每个例子都是在 C++ 或 C++ 中,我无法通过这些语言......

Thanks.谢谢。

You are on the right way by using the Houghtransformation.通过使用霍夫变换,您走在正确的道路上。 Instead of using Houghcircles you have to use Houghlines and check the obtained lines for intersections.您必须使用 Houghlines 并检查获得的线的交叉点,而不是使用 Houghcircles。 If you really have to find rectangles (and not 4 edged polygones) - you should look for lines with the same angle(+- a small offset) and if you found at least a pair of these lines you have to look for lines that lay perpendicular to this, find a pair as well and check for intersections.如果您真的必须找到矩形(而不是 4 个边多边形)-您应该寻找具有相同角度的线(+- 一个小的偏移量),如果您找到至少一对这样的线,您必须寻找铺设的线与此垂直,也找到一对并检查交叉点。 It should not be a big deal using vectors(endpoint - startpoint) and lines to perform the angle and intersection tests.使用向量(端点 - 起点)和线来执行角度和相交测试应该不是什么大问题。

There are many ways of detecting a rectangle using opencv, the most appropriate way of doing this is by finding the contours after applying Canny Edge Detection.使用 opencv 检测矩形的方法有很多,最合适的方法是在应用 Canny Edge Detection 后找到轮廓。

Steps are as follows :- 1.Convert the image to MAT步骤如下:- 1.将图像转换为MAT

  1. Grayscale the image灰度图像

3.Apply Gausian Blur 3.应用高斯模糊

4.Apply Morphology for filling the holes if any 4.如果有的话,应用形态学来填充孔

5.Apply Canny Detection 5.应用Canny检测

6.Find Contours of the image 6.查找图像的轮廓

7.Find the largest contour of the rest 7.找到其余部分的最大轮廓

8.Draw the largest contour. 8.绘制最大轮廓。

Code is as follows - 1.Convert the image to MAT代码如下 - 1.Convert the image to MAT

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

3.Apply Gausian Blur 3.应用高斯模糊

Imgproc.GaussianBlur(gray, gray, Size(5.0, 5.0), 0.0)

4.Apply Morphology for filling the holes if any and also dilate the image 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.Apply Canny Detection 5.应用Canny检测

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

6.Find Contours of the image 6.查找图像的轮廓

   val contours = ArrayList<MatOfPoint>()
        val hierarchy = Mat()
        Imgproc.findContours(
            edges, contours, hierarchy, Imgproc.RETR_LIST,
            Imgproc.CHAIN_APPROX_SIMPLE
        )

7.Find the largest contour of the rest 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.Draw the largest contour which is the rectangle 8.绘制最大的等高线,即矩形

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

There you go you have found the rectangle .你已经找到了矩形。 If any error persist in getting the process .Try resizing the source Image to half of its height and width.如果在获取过程中仍然存在任何错误。尝试将源图像的大小调整为其高度和宽度的一半。

Have a look at the below link for proper Java code of the above explained https://github.com/dhananjay-91/DetectRectangle Also, https://github.com/aashari/android-opencv-rectangle-detector看看下面的链接,上面解释的正确 Java 代码https://github.com/dhananjay-91/DetectRectangle另外, https://github.com/aashari/android-opencv-rectangle-detector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM