美文网首页
convexHull(凸包检测)

convexHull(凸包检测)

作者: itfitness | 来源:发表于2019-08-21 11:11 被阅读0次

概念

凸包对于图像来说其实就是图像轮廓突出点的连线。对于点集合来说就是点集合最外围点的连线,通过这些连线形成的多边形可以刚好将所有的点包裹起来。

效果图对比

●源图像



●处理后的图像(绿色为轮廓,蓝色为凸包)


函数讲解

●函数原型(findContours)
○c++

void convexHull( InputArray points, OutputArray hull,
                              bool clockwise = false, bool returnPoints = true )

○Android

void convexHull(MatOfPoint points, MatOfInt hull, boolean clockwise)

●参数解释
○points:输入的二维点集合,一般是用图像轮廓函数求得的轮廓点。
○hull:输出的是凸包的二维xy点的坐标值,针对每一个轮廓形成的。
○clockwise:操作方向,当标识符为真时,输出凸包为顺时针方向,否则为逆时针方向。
○returnPoints:操作标识符,默认值为true,此时返回各凸包的各个点,否则返回凸包各点的指数,当输出数组是std::vector时,此标识被忽略。

函数使用

●c++中

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main() {
    Mat src = imread("C:/Users/Administrator/Desktop/t01.jpg");//引入源图像
    if (src.empty()) {
        return -1;
    }
    cvtColor(src,src,COLOR_BGR2GRAY);//图像灰度化
    threshold(src, src, 200, 255, CV_THRESH_BINARY_INV);//图像二值化方便检测
    imshow("src", src);
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    //检测外围轮廓
    findContours(src, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0));//轮廓检测
     //寻找图像凸包
    vector<vector<Point>>hull(contours.size());
    Mat dst = Mat::zeros(src.size(),CV_8UC3);
    for (int i = 0; i < contours.size(); i++)
    {
        convexHull(contours[i], hull[i], false,true);
        //绘制轮廓
        drawContours(dst, contours, i, Scalar(0, 255, 0), 2, 8, hierarchy);
        //绘制凸包
        drawContours(dst, hull, i, Scalar(255,0,0), 2, 8, Mat());
    }
    imshow("dst", dst);
    waitKey(0);
    return 0;
}

●Android中

private Bitmap convexHull() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.t01);
        Mat src = new Mat();
        //将Bitmap对象转为Mat对象
        Utils.bitmapToMat(bitmap, src);
        //将图像转为灰度图
        Imgproc.cvtColor(src,src,Imgproc.COLOR_RGBA2GRAY);
        //将图像转为二值图
        Imgproc.threshold(src, src, 200, 255, Imgproc.THRESH_BINARY_INV);
        //最终图像的背景为黑色,大小与源图像相同
        Mat dst = Mat.zeros(src.size(), CvType.CV_8UC4);
        Mat hierarchy = new Mat();
        ArrayList<MatOfPoint> contours = new ArrayList<>();
        //轮廓发现
        Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
        ArrayList<Point> hullPointList = new ArrayList<>();
        MatOfPoint hullPointMat = new MatOfPoint();
        ArrayList<MatOfPoint> hullPoints = new ArrayList<>();
        for (int k=0; k < contours.size(); k++){
            MatOfInt hullInt = new MatOfInt();
            //凸包检测
            Imgproc.convexHull(contours.get(k), hullInt);

            for(int j=0; j < hullInt.toList().size(); j++){
                hullPointList.add(contours.get(k).toList().get(hullInt.toList().get(j)));
            }

            hullPointMat.fromList(hullPointList);
            hullPoints.add(hullPointMat);
            //绘制轮廓
            Imgproc.drawContours(dst,contours,k,new Scalar(0,255,0,255),2,8,hierarchy);
            //绘制凸包
            Imgproc.drawContours(dst,hullPoints,k,new Scalar(0,0,255,255),2,8,new Mat());
        }
        //将Mat对象转为Bitmap对象
        Utils.matToBitmap(dst,bitmap);
        return bitmap;
    }

相关文章

  • convexHull(凸包检测)

    概念 凸包对于图像来说其实就是图像轮廓突出点的连线。对于点集合来说就是点集合最外围点的连线,通过这些连线形成的多边...

  • google/amap folygon 多边形交叉排序

    多坐标点凸包检测排序

  • OpenCV 计算物体的凸包

    使用OpenCV函数 convexHull 步骤 滤波——消除噪声 增强——二值化,使轮廓更明显 检测——选出边缘...

  • 凸包

    convexHull 实例

  • 凸包

    向量的叉乘就是由该两个向量所组成的平行四边形的面积(x1y2-x2y1).这个凸包不是太懂.先留模板在此这个是水平...

  • 凸包

    凸包最常用的凸包算法是Graham扫描法和Jarvis步进法Graham's Scan法这个算法是由数学大师葛立恒...

  • 凸包

    逼近多边形是轮廓的高度近似,但是有时候我们希望使用一个多边形的凸包来简化它。凸包跟逼近多边形很像,只不过它是物体最...

  • Python3 趣味系列题8 ------ 凸包动态绘制

    本文介绍利用Graham Scan算法获得凸包(平面凸包),并动态展示凸包的形成过程。下面用比较通俗的语言,介绍下...

  • 算法复习-geometric algo

    convex hull 凸包-video25&video26 凸包算法剖析https://cyw3.github....

  • 图像轮廓(2)

    1. 凸包 凸缺陷可以用来处理手势识别问题 points:轮廓clockwise:True:凸包按顺时针方向排列;...

网友评论

      本文标题:convexHull(凸包检测)

      本文链接:https://www.haomeiwen.com/subject/pqxbsctx.html