有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在javacv中获取提取对象的x,y坐标?

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

enter image description here

我需要识别这些物体,并在这些物体周围画正方形。我阅读了一些教程,并尝试使用以下代码识别对象

    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归档我的目标吗


共 (5) 个答案

  1. # 1 楼答案

    你知道吗findContours函数在背景上查找轮廓

    只需对你的图像进行bitwise_not(或JavaCV中的模拟),然后应用findContours。这实际上可以解决你的问题

  2. # 2 楼答案

    cvFindContours是解决您问题的正确方法
    我已经证实它对你的图像有效
    Here is my experiment

    你有系统。出来println inside,输出是什么

    cvFindContours可能很难使用,我把它包装成了一个more general C++ function。我还使用class called vBlob来描述对象CVFindContentours。从java代码中我看到,java版本API与C/C++版本非常相似。所以重写它并不难

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

  3. # 3 楼答案

    不需要第三方库可以使用一种称为bounding box的技术在OpenCV中实现您想要的:

    enter image description here

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

    • this post中有一个很好的等高线介绍
    • 有一个Java demo实现了一个版本的边界框来检测汽车牌照

    顺便说一下cvMinEnclosingCircle()将圆的中心返回为CvPoint2D32f

  4. # 4 楼答案

    我使用C++ API,但是我认为JavaCV中也应该有一些函数,它使用DopTrimes的输出。p>

  5. # 5 楼答案

    试着阅读下面的代码,它会回答你的问题

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

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

    enter image description here