Javacv Blob检测

时间:2012-02-03 12:59:52

标签: java opencv feature-detection blobs javacv

我想在我的应用程序中使用一些blob检测,这是用Java编写的,因此使用JavaCV代替OpenCV。我找到了许多课程:

SimpleBlobDetectorCvBlobDetectorCvBlob,...但我找不到任何教程或演示/示例代码在Java中使用它们。 任何人都可以告诉我如何使用这些,因为我无法弄明白,并没有很好的文档。 谢谢!

2 个答案:

答案 0 :(得分:8)

我正是在这个时刻处理同样的问题,并有第一个解决方案。 那里有很多方法,但不幸的是大多数方法都是丑陋而缓慢的。 我的主要目标是留在JavaCV / OpenCV-World。

这些链接促使我朝着正确的方向前进:

  1. Blob extraction in OpenCV
  2. http://voices.yahoo.com/connected-components-using-opencv-5462975.html?cat=15
  3. http://opencv.willowgarage.com/documentation/cpp/structural_analysis_and_shape_descriptors.html#cv-findcontours
  4. 这是一个完整的演示类,应该向您展示它是如何完成的。 尝试使用带有简单物体和尖角的黑色背景图像,以获得最佳效果。请务必查看上面的第二个链接,以了解代码中发生的情况。

    import static com.googlecode.javacpp.Loader.sizeof;
    import static com.googlecode.javacv.cpp.opencv_core.CV_FILLED;
    import static com.googlecode.javacv.cpp.opencv_core.CV_RGB;
    import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
    import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
    import static com.googlecode.javacv.cpp.opencv_core.cvCreateMemStorage;
    import static com.googlecode.javacv.cpp.opencv_core.cvDrawContours;
    import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
    import static com.googlecode.javacv.cpp.opencv_core.cvPoint;
    import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
    import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
    import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
    import static com.googlecode.javacv.cpp.opencv_imgproc.CV_CHAIN_APPROX_SIMPLE;
    import static com.googlecode.javacv.cpp.opencv_imgproc.CV_RETR_CCOMP;
    import static com.googlecode.javacv.cpp.opencv_imgproc.CV_THRESH_BINARY;
    import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
    import static com.googlecode.javacv.cpp.opencv_imgproc.cvFindContours;
    import static com.googlecode.javacv.cpp.opencv_imgproc.cvThreshold;
    
    import java.awt.Color;
    import java.util.Random;
    
    import com.googlecode.javacv.cpp.opencv_core.CvContour;
    import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
    import com.googlecode.javacv.cpp.opencv_core.CvScalar;
    import com.googlecode.javacv.cpp.opencv_core.CvSeq;
    import com.googlecode.javacv.cpp.opencv_core.IplImage;
    
    /**
     * A demo for blob extraction using only JavaCV / OpenCV
     * @see https://stackoverflow.com/questions/4641817/blob-extraction-in-opencv
     * @see http://voices.yahoo.com/connected-components-using-opencv-5462975.html?cat=15
     * @see http://opencv.willowgarage.com/documentation/cpp/structural_analysis_and_shape_descriptors.html#cv-findcontours
     * @author happyburnout
     */
    
    public class JavaCVBlobDemo {
    
        static String sourcePath = "c:/test/source.jpg";
        static String targetPath = "c:/test/target.jpg";
    
        public static void main (String args[]){
            IplImage image = cvLoadImage(sourcePath);
            IplImage grayImage = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
            cvCvtColor(image, grayImage, CV_BGR2GRAY);
    
            CvMemStorage mem;
            CvSeq contours = new CvSeq();
            CvSeq ptr = new CvSeq();
            cvThreshold(grayImage, grayImage, 150, 255, CV_THRESH_BINARY);
            mem = cvCreateMemStorage(0);
    
            cvFindContours(grayImage, mem, contours, sizeof(CvContour.class) , CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
    
            Random rand = new Random();
            for (ptr = contours; ptr != null; ptr = ptr.h_next()) {
                Color randomColor = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
                CvScalar color = CV_RGB( randomColor.getRed(), randomColor.getGreen(), randomColor.getBlue());
                cvDrawContours(image, ptr, color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
            }
            cvSaveImage(targetPath, image);
        }
    
    }
    

答案 1 :(得分:-2)

public static IplImage detectObjects(IplImage srcImage){

    IplImage resultImage = cvCloneImage(srcImage);

    CvMemStorage mem = CvMemStorage.create();
    CvSeq contours = new CvSeq();
    CvSeq ptr = new CvSeq();

    cvFindContours(srcImage, 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);

        cvRectangle( resultImage, cvPoint( boundbox.x(), boundbox.y() ), cvPoint( boundbox.x() + boundbox.width(), boundbox.y() + boundbox.height()),cvScalar( 0, 255, 255, 0 ), 3, 0, 0 ); 
    }

    return resultImage;
}