【问题标题】:How to get x,y coordinates of extracted objects in javacv?如何在javacv中获取提取对象的x,y坐标?
【发布时间】:2012-08-19 20:02:54
【问题描述】:

目前我正在开发图像处理项目,我正在使用 javacv 开发图像处理组件。我能够提取图像的一些有趣部分,现在我需要读取这些对象的 x 和 y 坐标。 这是我提取的图像

我需要识别这些对象并在这些对象周围绘制正方形。我浏览了一些教程并尝试使用以下代码识别对象。

    IplImage img="sourceimage";
    CvSize sz = cvSize(img.width(), img.height());
    IplImage gry=cvCreateImage(sz, img.depth(), 1);
    cvCvtColor(img, gry, CV_BGR2GRAY);
    cvThreshold(gry, gry, 200, 250, CV_THRESH_BINARY);


    CvMemStorage mem = CvMemStorage.create();
    CvSeq contours = new CvSeq();
    CvSeq ptr = new CvSeq();
    cvFindContours(gry, mem, contours, Loader.sizeof(CvContour.class) , CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

    CvRect boundbox;

    for (ptr = contours; ptr != null; ptr = ptr.h_next()) {
        boundbox = cvBoundingRect(ptr, 0);
        if(boundbox.height()>10||boundbox.height()>10){
            cvRectangle( gry, cvPoint( boundbox.x(), boundbox.y() ), cvPoint( boundbox.x() + boundbox.width(), boundbox.y() + boundbox.height()),CvScalar.BLUE, 0, 0, 0 );
            System.out.println(boundbox.x()+", "+boundbox.y());
        }
    }
    cvShowImage("contures", gry);
    cvWaitKey(0);

但它不会在对象周围绘制为矩形。我想知道我是否可以使用 cvFindContours 方法来识别这些对象?请有人解释一下如何使用 javacv/opencv 归档我的目标?

【问题讨论】:

  • 那么当您绘制这些矩形时,它们与您拥有的对象的关系如何?
  • 为什么忽略你的问题的答案??
  • @MahdeTo 我需要知道如何在这些对象周围绘制矩形。
  • @Astor 我没有忽略这些答案我正在检查这些答案,因为我必须将它们转换为 javacv。但是 thnax 4 rply。

标签: java opencv image-processing javacv


【解决方案1】:

尝试通过下面的代码,它会回答你的问题。

    IplImage img=cvLoadImage("pathtosourceimage");
    CvSize cvSize = cvSize(img.width(), img.height());
    IplImage gry=cvCreateImage(cvSize, img.depth(), 1);
    cvCvtColor(img, gry, CV_BGR2GRAY);
    cvThreshold(gry, gry, 200, 255, CV_THRESH_BINARY);
    cvAdaptiveThreshold(gry, gry, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV, 11, 5);

    CvMemStorage storage = CvMemStorage.create();
    CvSeq contours = new CvContour(null);

    int noOfContors = cvFindContours(gry, storage, contours, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, new CvPoint(0,0));

    CvSeq ptr = new CvSeq();

    int count =1;
    CvPoint p1 = new CvPoint(0,0),p2 = new CvPoint(0,0);

    for (ptr = contours; ptr != null; ptr = ptr.h_next()) {

        CvScalar color = CvScalar.BLUE;
        CvRect sq = cvBoundingRect(ptr, 0);

            System.out.println("Contour No ="+count);
            System.out.println("X ="+ sq.x()+" Y="+ sq.y());
            System.out.println("Height ="+sq.height()+" Width ="+sq.width());
            System.out.println("");

            p1.x(sq.x());
            p2.x(sq.x()+sq.width());
            p1.y(sq.y());
            p2.y(sq.y()+sq.height());
            cvRectangle(img, p1,p2, CV_RGB(255, 0, 0), 2, 8, 0);
            cvDrawContours(img, ptr, color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
            count++;

    }

    cvShowImage("contures",img);
    cvWaitKey(0);

这是我为您的给定图像得到的输出。

【讨论】:

    【解决方案2】:

    您知道findContours 函数可以在黑色背景上找到白色轮廓吗?

    只需对您的图像执行bitwise_not(或JavaCV 中的类似),然后应用findContours。这实际上可以解决您的问题。

    【讨论】:

      【解决方案3】:

      无需第三方库!您正在寻找的内容可以在 OpenCV 中使用一种称为 bounding box 的技术来实现:

      诀窍是使用cvFindContours() 检索轮廓,然后使用cvApproxPoly() 改进结果。请注意,您必须使用 cvNot() 反转输入图像的颜色,因为 cvFindContours() 搜索 white 轮廓。

      • this post 中有很好的轮廓介绍。
      • 有一个 Java demo 实现了一个边界框版本来检测车牌。

      顺便说一句,cvMinEnclosingCircle() 将圆心返回为CvPoint2D32f

      【讨论】:

      • 您是否从问题中读取了代码,实际上是问题本身?他已经知道如何找到边界框。在这种情况下,approxPoly 如何改善结果?问题是 topicstarter 不明白为什么他得到 1 个矩形而不是 4 个。我已经解释了原因。
      • @Astor,您不应该与 karlphillip 争论,至少出于礼貌,如果不是为了更好的理由。你知道,你永远应该尊重你的长辈。他是这里的长者,在 opencv 标签上;)这就像告诉 Jon Skeet 他的代码无法编译
      【解决方案4】:

      cvFindContours 是您问题的正确解决方案。
      我已经验证它适用于您的图片。

      你里面有 System.out.println,输出是什么?

      cvFindContours 使用起来可能很棘手,我已将其包装成 more general C++ function。我还使用class called vBlob 来描述 cvFindContours 检测到的 object。从我从您的 java 代码中看到的,java 版本 API 与 C/C++ 版本非常相似。所以重写它并不难。

      ps。阿斯特给出了正确的答案。

      【讨论】:

        【解决方案5】:

        我正在使用 c++ api,但我认为 javacv 中还应该有一些名为 drawContours 或 cvDrawContours 的函数,它使用 findContours 的输出。

        【讨论】:

        • 但是 findContours 的输出是整个图像。
        猜你喜欢
        • 2016-06-23
        • 2022-11-13
        • 1970-01-01
        • 1970-01-01
        • 2019-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多