OpenCV学习代码记录——轮廓(contour)检测
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录。
代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tree/master/OpenCVTest
效果
代码
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp> // cvtColor
int contours_test();
int ContourDetection()
{
//return contours_test();
cv::Mat image; // 加载原始图片
cv::Mat gary; // 存储灰度图像
cv::Mat dstimg; // 绘制轮廓目标图片
// 创建两个窗口
cv::namedWindow("src"); // 原始图片显示窗口
cv::namedWindow("dst"); // 轮廓图片显示窗口
// 载入原始图片
image = cv::imread("../Image/sisy.jpg");
if (image.empty()) {
puts("图片加载失败");
return -1;
}
cv::imshow("src", image); // 显示原始图片
gary.create(image.size(), CV_8U); // 申请灰度图存储空间
cv::cvtColor(image, gary, CV_BGR2GRAY); // 转换原始图为灰度图
cv::threshold(gary, gary, 128, 255, cv::THRESH_BINARY); // 转换为二值图
std::vector<std::vector<cv::Point> > contours; // 检测的轮廓数组
std::vector<cv::Vec4i> hierarchy; //
int mode = CV_RETR_EXTERNAL; // 轮廓检测模式
//mode表示轮廓的检索模式
// CV_RETR_EXTERNAL表示只检测外轮廓
// CV_RETR_LIST检测的轮廓不建立等级关系
// CV_RETR_CCOMP建立两个等级的轮廓,上面的一层为外边界,里面的一层为内孔的边界信息。如果内孔内还有一个连通物体,这个物体的边界也在顶层。
// CV_RETR_TREE建立一个等级树结构的轮廓。具体参考contours.c这个demo
int method = CV_CHAIN_APPROX_SIMPLE;
//method为轮廓的近似办法
// CV_CHAIN_APPROX_NONE存储所有的轮廓点,相邻的两个点的像素位置差不超过1,即max(abs(x1 - x2),abs(y2 - y1)) == 1
// CV_CHAIN_APPROX_SIMPLE压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息
// CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似算法
// 查找contour
cv::findContours(gary/*输入图像(必须为一个2值单通道图像)*/,
contours/*, hierarchy*/, mode, method);
// 为轮廓显示图片申请空间
dstimg = cv::Mat(image.size(), CV_8UC3); // 3通道图像,以便彩色显示
image.copyTo(dstimg); // 拷贝源图像
// 将轮廓画出
cv::drawContours(dstimg/*目标图像*/,
contours/*输入的轮廓组*/,
-1 /*指明画第几个轮廓(负值表示全部轮廓)*/,
cv::Scalar(0,0,255)/*轮廓颜色BGR(此处以红色绘制)*/,
2 /*轮廓线宽*/,
8 /*轮廓线型*/,
cv::noArray()/*轮廓结构信息*/);
// 显示轮廓图片
cv::imshow("dst", dstimg);
// 等待按键
cv::waitKey();
}
int contours_test()
{
std::string image_name = "../Image/sisy.jpg";
cv::Mat src = cv::imread(image_name);
cv::imshow("src", src);
cv::Mat gray(src.size(), CV_8U);
cv::cvtColor(src, gray, CV_BGR2GRAY);//转换成灰度图
cv::imshow("gray", gray);
cv::threshold(gray, gray, 128, 255, cv::THRESH_BINARY);//转换成2值图像
cv::imshow("binary", gray);
/////////////////////////////////////////////////////////////////////
std::vector<std::vector<cv::Point>> contours;
cv::findContours(gray,
contours, // a vector of contours
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours
// Print contours' length
// std::cout << "Contours: " << contours.size() << std::endl;
std::vector<std::vector<cv::Point>>::const_iterator itContours = contours.begin();
for (; itContours != contours.end(); ++itContours)
{
//std::cout << "Size: " << itContours->size() << std::endl;
}
// draw black contours on white image
cv::Mat result(gray.size(), CV_8U, cv::Scalar(255));
cv::drawContours(result, contours,
-1, // draw all contours
cv::Scalar(0), // in black
2); // with a thickness of 2
cv::namedWindow("Contours");
cv::imshow("Contours", result);
// draw contours on the original image
cv::Mat original = cv::imread(image_name);
cv::drawContours(original, contours,
-1, // draw all contours
cv::Scalar(255, 255, 255), // in white
-1); // with a thickness of 2
cv::namedWindow("Contours on Animals");
cv::imshow("Contours on Animals", original);
// Let's now draw black contours on white image
result.setTo(cv::Scalar(255));
cv::drawContours(result, contours,
-1, // draw all contours
cv::Scalar(0), // in black
-1); // with a thickness of 1
//image= cv::imread("test.png",0);
cv::waitKey(0);
return 0;
}
OpenCV学习代码记录——轮廓(contour)检测的更多相关文章
- OpenCV学习代码记录——Hough线段检测
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- OpenCV学习代码记录—— Snake轮廓
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- OpenCV学习代码记录——人脸检测
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- OpenCV学习代码记录——canny边缘检测
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- OpenCV学习(34) 点到轮廓的距离
在OpenCV中,可以很方便的计算一个像素点到轮廓的距离,计算距离的函数为: double pointPolygonTest(InputArray contour, Point2f pt, bool ...
- Opencv学习笔记------Harris角点检测
image算法测试iteratoralgorithmfeatures 原创文章,转载请注明出处:http://blog.csdn.net/crzy_sparrow/article/details/73 ...
- OpenCV学习(32) 求轮廓的包围盒
在OpenCV中,能够很方便的求轮廓包围盒.包括矩形,圆形,椭圆形以及倾斜的矩形(包围面积最小)集中包围盒.用到的四个函数是: Rect boundingRect(InputArray points) ...
- OpenCV学习笔记(14)——轮廓的性质
提取一些经常使用的对象特征 1.长宽比 边界矩形的宽高比 x,y,w,h = cv2.boundingRect(cnt) a ...
- python 设计模式学习代码记录
@工厂模式class Beijing: def printreslut(self): print("ok") class Shanghai: def printreslut(sel ...
随机推荐
- Guava CaseFormat
概述 CaseFormat用来转换各种不同的编程语言间的变量名命名格式, 主要用到的方法只有一个 CaseFormat.to(CaseFormat from, String s) CaseFormat ...
- [转]使用互斥对象让程序只运行一次(delphi)
使用互斥对象让程序只运行一次“怎么让我的程序在运行时不能重复打开?”经常在论坛上看到有朋友问这方面的问题.本文将比较详细的说明这一问题,并给出一个较为完善的解决方案. 尽管这已经不是一个新问题了,但这 ...
- C++中一些类和数据结构的大小的总结
针对class,虚函数等情况写了一些代码测试. #include <stdio.h> class A { }; class B { public: void f(); void g(); ...
- siamese网络&&tripletnet
siamese网络 - 之前记录过: https://www.cnblogs.com/ranjiewen/articles/7736089.html - 原始的siamese network: 输入一 ...
- Wildcard Matching leetcode java
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- Active Learning
怎么办?进行Active Learning主动学习 Active Learning是最近又流行起来了的概念,是一种半监督学习方法. 一种典型的例子是:在没有太多数据的情况下,算法通过不断给出在决策边界 ...
- echart 图例设置自定义图标?
option = { legend: { orient: 'horizontal', // 'vertical' x: 'right', // 'center' | 'left' | {number} ...
- Mac 隐私与安全没有允许任何来源选项
mac 允许任何来源的 app 在 macOS Sierra 10.12 及之后的版本,都没有 打开任何来源 的选项,解决方法: 终端执行命令: sudo spctl --master-disable
- 阿里云centos 6安装iRedmail过程
全新系统 yum update cd /root wget http://www.iredmail.com/iRedMail-0.8.7.tar.bz2 tar xvf iRedMail-0.8.7. ...
- LintCode: Search A 2d Matrix
1. 设查找的数位y,第一行最后一列的数位x 如果x<y,x是第一行最大的,所以第一行都小于y,删除第一行: 如果x>y,x是最后一列最小的,所以最后一列都大于y,删除最后一列: 这样保证 ...