OpenCV 闭合轮廓检测

 

 

这个好像是骨头什么的,但是要求轮廓闭合,于是对图片进行一下膨胀操作,再次检测轮廓就好了。

 

// A closed contour.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

// FindRotation-angle.cpp : 定义控制台应用程序的入口点。
//

// findContours.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include "highlight"
//#include "highgui.h"

#pragma comment(lib,"opencv_core2410d.lib")
#pragma comment(lib,"opencv_highgui2410d.lib")
#pragma comment(lib,"opencv_imgproc2410d.lib")  

#define PI 3.1415926

using namespace std;
using namespace cv;

int main()
{
	// Read input binary image

	char *image_name = "test.bmp";
	cv::Mat image = cv::imread(image_name);
	if (!image.data)
		return 0; 

	// 从文件中加载原图
	  // IplImage *pSrcImage = cvLoadImage(image_name, CV_LOAD_IMAGE_UNCHANGED);
	  Mat gray(image.size(),CV_8U);

	  cvtColor(image,gray,CV_BGR2GRAY);
		 // 转为2值图
	 threshold(gray,gray,145,255,cv::THRESH_BINARY_INV);
	//cvThreshold(pSrcImage,pSrcImage,145,255,cv::THRESH_BINARY_INV);

	   //image = gray;

	   cv::namedWindow("Binary Image");
	   cv::imshow("Binary Image",gray);

	   cv::Mat element(5,5,CV_8U,cv::Scalar(255));

	   cv::dilate(gray,gray,element);
	   //cv::erode(image,image,element);

	   cv::namedWindow("dilate Image");
	   cv::imshow("dilate Image",gray);

	// Get the contours of the connected components
	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(image.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);

	// Eliminate too short or too long contours

	/*
	int cmin= 100;  // minimum contour length
	int cmax= 1000; // maximum contour length
	std::vector<std::vector<cv::Point>>::const_iterator itc= contours.begin();
	while (itc!=contours.end()) {

		if (itc->size() < cmin || itc->size() > cmax)
			itc= contours.erase(itc);
		else
			++itc;
	}

	*/

	// 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,0), // in white
		2); // 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("binary.bmp",0);

	// testing the bounding box 

	std::vector<std::vector<cv::Point>>::const_iterator itc_rec= contours.begin();
	while (itc_rec!=contours.end())
	{
		cv::Rect r0= cv::boundingRect(cv::Mat(*(itc_rec)));
		cv::rectangle(result,r0,cv::Scalar(0),2);
			++itc_rec;
	}

	/*
	// testing the enclosing circle
	float radius;
	cv::Point2f center;
	cv::minEnclosingCircle(cv::Mat(contours[1]),center,radius);
	cv::circle(result,cv::Point(center),static_cast<int>(radius),cv::Scalar(0),2);

	//	cv::RotatedRect rrect= cv::fitEllipse(cv::Mat(contours[1]));
	//	cv::ellipse(result,rrect,cv::Scalar(0),2);

	// testing the approximate polygon
	std::vector<cv::Point> poly;
	cv::approxPolyDP(cv::Mat(contours[2]),poly,5,true);

	std::cout << "Polygon size: " << poly.size() << std::endl;

	// Iterate over each segment and draw it
	std::vector<cv::Point>::const_iterator itp= poly.begin();
	while (itp!=(poly.end()-1)) {
		cv::line(result,*itp,*(itp+1),cv::Scalar(0),2);
		++itp;
	}
	// last point linked to first point
	cv::line(result,*(poly.begin()),*(poly.end()-1),cv::Scalar(20),2);

	// testing the convex hull
	std::vector<cv::Point> hull;
	cv::convexHull(cv::Mat(contours[3]),hull);

	// Iterate over each segment and draw it
	std::vector<cv::Point>::const_iterator it= hull.begin();
	while (it!=(hull.end()-1)) {
		cv::line(result,*it,*(it+1),cv::Scalar(0),2);
		++it;
	}
	// last point linked to first point
	cv::line(result,*(hull.begin()),*(hull.end()-1),cv::Scalar(20),2);

	// testing the moments

	// iterate over all contours
	itc= contours.begin();
	while (itc!=contours.end()) {

		// compute all moments
		cv::Moments mom= cv::moments(cv::Mat(*itc++));

		// draw mass center
		cv::circle(result,
			// position of mass center converted to integer
			cv::Point(mom.m10/mom.m00,mom.m01/mom.m00),
			2,cv::Scalar(0),2); // draw black dot
	}

	*/

	cv::namedWindow("Some Shape descriptors");
	cv::imshow("Some Shape descriptors",result);

	cv::waitKey();
	return 0;

}

 

实现效果:

 

 

时间: 2024-07-29 04:10:27

OpenCV 闭合轮廓检测的相关文章

OpenCV 矩形轮廓检测

    转载请注明出处:http://blog.csdn.net/wangyaninglm/article/details/44151213, 来自:shiter编写程序的艺术 基础介绍 OpenCV里提取目标轮廓的函数是findContours,它的输入图像是一幅二值图像,输出的是每一个连通区域的轮廓点的集合:vector<vector<Point>>.外层vector的size代表了图像中轮廓的个数,里面vector的size代表了轮廓上点的个数. 轮廓进行填充的时候我会有下面

在opencv中利用findContours进行轮廓检测,程序运行结束时为什么会报中断错误,求解?

问题描述 在opencv中利用findContours进行轮廓检测,程序运行结束时为什么会报中断错误,求解? 代码如下: #include #include #include #include #include #include using namespace cv; using namespace std; //一.将源图片转成单通道的灰阶图片 Mat changeSourceImg(Mat img){ Mat great(img.size(), CV_8U); cvtColor(img, g

opencv 人眼轮廓-opencv检测人眼并画出人眼轮廓

问题描述 opencv检测人眼并画出人眼轮廓 如果仅仅是检测人眼的话本人还是能够写出代码的,但现在要求是:1.要检测出人眼并画出轮廓2.闭眼时轮廓跟着合上,睁眼时轮廓也跟着打开,及轮廓不是仅仅用方框或是圆形画出来,而是要和眼睛同步动作 急求代码,,,,,,小弟感激不尽啊!! 解决方案 是需要轮廓检测么

基于opencv的皮肤检测

一个超精准的肤色检测!! 修改自opencv的adaptiveskindetector.cpp,去掉了复杂的命令行参数输入,只需要一个网络摄像头即可运行. 原理方面大致看了下,主要还是利用HSV空间的色调信息. 效果还可以,但似乎对于白色,尤其是乳白色的墙壁,壁板等检测效果较差. 这是在这里公布的第一个小东西,尽量一周更新一个,基本都会附带源代码(C++, VS2008)   /***********************************************************

opencv怎么样有效检测出红绿灯的位置?

问题描述 opencv怎么样有效检测出红绿灯的位置? 我在检测时是先用Canny检测边缘,然后再用HoughCircles进行圆的检测,但是效果十分不理想,有时什么都检测不出,有时检测出一堆圆但就是没有我想要的那个,最好的结果是得到一堆圆里面有想要的,但是无用圆数量太多了.我该怎么操作才能更加准确地检测呢? 解决方案 http://www.zhihu.com/question/45686643

怎样实现c++利用opencv实现人脸检测与识别

问题描述 怎样实现c++利用opencv实现人脸检测与识别 就是指通过摄像头保存识别的人脸,再次识别时如果被识别的人是已经添加图片的,就把他的名字显示出来,如果没有就将人脸保存.求大神,提前谢谢. 解决方案 这你需要机器学习才能够实现. 具体学习方法可以用BOOSTING算法,随机森林算法或者K邻近算法,具体代码可以从<学习OPENCV>中找到,若是不想自己敲代码,也可以在百度中区找.其实OPENCV的sample里也有相关的代码.不过你自己需要建立自己的数据库.

如何在Android中用OPENCV实现人脸检测,人脸识别等功能,如何实现,用什么方法?

问题描述 如何在Android中用OPENCV实现人脸检测,人脸识别等功能,如何实现,用什么方法? 如何在Android中用OPENCV实现人脸检测,人脸识别等功能,如何实现,用什么方法? 解决方案 请问您做的怎么样了?

OpenCV 轮廓检测

读入彩色3通道图像,转换成灰度图像,再转换成二值图像,完后检测轮廓.   // cvtcolor.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include <opencv2/highgui/highgui.hpp> #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp&g

OpenCV轮廓检测,计算物体旋转角度

    效果还是有点问题的,希望大家共同探讨一下     // FindRotation-angle.cpp : 定义控制台应用程序的入口点. // // findContours.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include <vector> #include <opencv2/opencv.hpp> #include <opencv2/cor