opencv学习之图像凸包
来源:互联网 发布:java的设计模式有哪些 编辑:程序博客网 时间:2024/06/09 16:59
图像凸包是表达图像一维属性(比如长宽、面积等)信息的一种方式。
所以,计算图像凸包对一些图像前期、后期处理都有一定的帮助
看到别人的一篇代码如下
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- opencv学习之图像凸包
- OpenCV学习之图像操作
- opencv学习之遍历图像
- opencv学习之图像修补
- opencv学习之图像滤波
- Opencv学习之图像直方图
- OpenCV学习之显示图像
- OpenCV学习之图像平滑
- OpenCV学习之图像处理
- OpenCV学习之图像分割
- OpenCV学习笔记之图像视频显示
- OpenCV学习之图像轮廓提取
- opencv学习之图像颜色空间转换
- opencv学习--图像拼接之 Stitcher
- 学习OPENCV之读取图像和视频
- OpenCV学习(六)之图像缩放变换
- OpenCV学习(十五)之图像金字塔
- 第一篇 学习OpenCV之图像显示
- mysql 常用操作
- Ant使用
- Unsupported major.minor version 52.0解决
- Java synchronized关键字学习一
- Struts标签库结合OGNL的使用
- opencv学习之图像凸包
- Vuex2.0+Vue2.0构建备忘录应用实践
- leetcode题解-347. Top K Frequent Elements
- secureCRT 常用命令
- 从Ctrl-C看Python多线程的信号处理
- java中int和Integer的理解
- go slice
- Android获取SD card路径
- zookeeper学习笔记(二) —— 应用场景概览