图像凸包是表达图像一维属性(比如长宽、面积等)信息的一种方式。
所以,计算图像凸包对一些图像前期、后期处理都有一定的帮助
看到别人的一篇代码如下
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