美文网首页
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(凸包检测)

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