gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:34 24 4
gpt4 key购买 nike

我正在开发应用程序,我必须在其中检测矩形对象并绘制轮廓我正在使用 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++,我无法通过这些语言......

谢谢。

最佳答案

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

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

  1. 灰度图像

3.应用高斯模糊

4.Apply Morphology 填充空洞

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

关于android - 在android中使用open cv检测图像中的矩形并绘制轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16807117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com