opencv学习之图像凸包

图像凸包是表达图像一维属性(比如长宽、面积等)信息的一种方式。

所以,计算图像凸包对一些图像前期、后期处理都有一定的帮助

看到别人的一篇代码如下

clc; clear all; close all; 

I = imread('c://ce.jpg'); 

I = rgb2gray(I); 

bw = im2bw(I, graythresh(I)); 

figure; imshow(I); 

stats = regionprops(bwlabel(bw),'ConvexHull'); 

tn = stats.ConvexHull; 

hold on; 

h = patch(tn(:, 1), tn(:, 2), 'r'); 

set(h, 'FaceColor', 'none', 'EdgeColor','r', 'Marker', '.'); 

figure; imshow(I); 

stats = regionprops(bwlabel(bw),'BoundingBox'); 

tn = stats.BoundingBox; 

hold on; 

h = rectangle('Position', tn, 'EdgeColor','r'); 

 

求凸包的代码如下:

int main( int /*argc*/, char** /*argv*/ ) 
    { 
    Mat img(500, 500, CV_8UC3); 
    RNG& rng = theRNG();

    cout << "\n这个程序演示了凸包函数的使用,任意给定一些点,求出包围这些点的凸包\n" <<endl;

    for(;;) 
        { 
        char key; 
        int i, count = (unsigned)rng%100 + 1;

        vector<Point> points; 
        //随机在1-100个点,这些点位于图像中心3/4处。 
        for( i = 0; i < count; i++ ) 
            { 
            Point pt; 
            pt.x = rng.uniform(img.cols/4, img.cols*3/4); 
            pt.y = rng.uniform(img.rows/4, img.rows*3/4);

            points.push_back(pt); 
            }

        //计算凸包 
        vector<int> hull; 
        convexHull(Mat(points), hull, true);

       //画随即点 
        img = Scalar::all(0); 
        for( i = 0; i < count; i++ ) 
            circle(img, points[i], 3, Scalar(0, 0, 255), CV_FILLED, CV_AA);

        int hullcount = (int)hull.size(); 
        Point pt0 = points[hull[hullcount-1]]; 
        //画凸包 
        for( i = 0; i < hullcount; i++ ) 
            { 
            Point pt = points[hull[i]]; 
            line(img, pt0, pt, Scalar(0, 255, 0), 1, CV_AA); 
            pt0 = pt; 
            }

        imshow("hull", img);

        key = (char)waitKey(); 
        if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC' 
            break; 
        }

    return 0; 
    }

convexHull(Mat(points), hull, true);

convex

 

      convexHull第一个参数是要求凸包的点集,第二个参数是输出的凸包点,第三个参数是一个bool变量,表示求得的凸包是顺时针方向还是逆时针方向,true是顺时针方向。注意:第二个参数可以为vector<int>,此时返回的是凸包点在原轮廓点集中的索引,也可以为vector<Point>,此时存放的是凸包点的位置。

OpenCV中求凸包的算法参见paper:

Sklansky, J., Finding the Convex Hull of a Simple Polygon. PRL 1 $number, pp 79-83 (1982)

转载自:http://www.cnblogs.com/mikewolf2002/p/3422197.html


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值