【问题标题】:Android OpenCV Find contoursAndroid OpenCV 查找轮廓
【发布时间】:2012-11-06 22:28:35
【问题描述】:

我需要提取图像的最大轮廓。 这是我目前正在使用的代码。网上收集了几个sn-ps

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(outerBox, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = -1;
int maxAreaIdx = -1;
for (int idx = 0; idx < contours.size(); idx++) {
    Mat contour = contours.get(idx);
    double contourarea = Imgproc.contourArea(contour);
    if (contourarea > maxArea) {
        maxArea = contourarea;
        maxAreaIdx = idx;
    }
}

而且它似乎有效。但是,我不太确定如何从这里开始。 我尝试使用Imgproc.floodFill,但我不太确定如何使用。 此功能需要与原始Mat 相同大小的桅杆Mat +2 水平和+2 垂直。 当我在轮廓contours.get(maxAreaIdx) 上运行它时,它给了我一个错误。 代码:

Mat mask = Mat.zeros(contour.rows() + 2, contour.cols() + 2, CvType.CV_8UC1);
int area = Imgproc.floodFill(contour, mask, new Point(0,0), new Scalar(255, 255, 255));

错误:

11-18 19:07:49.406: E/cv::error()(3117): OpenCV Error: Unsupported format or combination of formats () in void cvFloodFill(CvArr*, CvPoint, CvScalar, CvScalar, CvScalar, CvConnectedComp*, int, CvArr*), file /home/oleg/sources/opencv/modules/imgproc/src/floodfill.cpp, line 621

所以基本上我的问题是,在找到面积最大的轮廓后,我怎样才能“突出显示”它?我希望其他一切都是黑色的,轮廓是白色的

谢谢!

【问题讨论】:

    标签: java android opencv javacv


    【解决方案1】:

    您可以在 OpenCV 中使用 DrawContours 函数:http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=drawcontours#drawcontours

    或者您可以在 C++ 中使用此实现(您可以在 OpenCV 文档中找到 Java 中的等价物,只需键入 OpenCV + google 上的函数名称)

    Mat src = imread("your image"); int row = src.rows; int col = src.cols;
        //Create contour
    vector<vector<Point> > contours; 
    vector<Vec4i> hierarchy;
    Mat src_copy = src.clone();
        findContours( src_copy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
    
    // Create Mask
    Mat_<uchar> mask(row,col);    
    for (int j=0; j<row; j++)
        for (int i=0; i<col; i++)
            {
                if ( pointPolygonTest( contours[0], Point2f(i,j),false) =0)
                {mask(j,i)=255;}
                else
                {mask(j,i)=0;}
            };
    

    尝试轮廓[1]、轮廓[2]...找到最大的一个

    这是为了显示你的轮廓:

    namedWindow("Contour",CV_WINDOW_AUTOSIZE);
    imshow("Contour", mask);
    

    【讨论】:

    • @Marek 你得到这个的java实现了吗??
    • color-blob-detection 示例中有 java/opencv4android findContours 示例。
    猜你喜欢
    • 1970-01-01
    • 2015-06-24
    • 2018-01-18
    • 2012-12-28
    • 2011-06-13
    • 1970-01-01
    • 2016-08-11
    • 2019-09-09
    • 2017-11-11
    相关资源
    最近更新 更多