image = cv::Mat(pSrcImage,true); cv::imwrite("binary.jpg",image); // Get the contours of the connected components std::vector<std::vector<cv::Point>> contours; cv::findContours(image, 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("test.jpg"); 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.jpg",0); // testing the bounding box cv::Rect r0= cv::boundingRect(cv::Mat(contours[0])); cv::rectangle(result,r0,cv::Scalar(0),2);
// 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); CvBox2D End_Rage2D; CvMemStorage *storage = cvCreateMemStorage(0); //开辟内存空间 CvSeq* contour = NULL; //CvSeq类型 存放检测到的图像轮廓边缘所有的像素值,坐标值特征的结构体以链表形式 cvFindContours( pSrcImage, storage, &contour, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);//这函数可选参数还有不少 for(; contour; contour = contour->h_next) //如果contour不为空,表示找到一个以上轮廓,这样写法只显示一个轮廓 //如改为for(; contour; contour = contour->h_next) 就可以同时显示多个轮廓 { End_Rage2D = cvMinAreaRect2(contour); //代入cvMinAreaRect2这个函数得到最小包围矩形 这里已得出被测物体的角度,宽度,高度,和中点坐标点存放在CvBox2D类型的结构体中,主要工作基本结束。
代码有的地方,还是稍微有点问题:::
// 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"#define PI 3.1415926int main()
{
// Read input binary image
cv::Mat image = cv::imread("test.jpg",0);
if (!image.data)
return 0; cv::namedWindow("Binary Image");
cv::imshow("Binary Image",image);
// 从文件中加载原图
IplImage *pSrcImage = cvLoadImage("test.jpg", CV_LOAD_IMAGE_UNCHANGED);
// 转为2值图
cvThreshold(pSrcImage,pSrcImage,200,255,cv::THRESH_BINARY_INV);
image = cv::Mat(pSrcImage,true); cv::imwrite("binary.jpg",image); // Get the contours of the connected components
std::vector<std::vector<cv::Point>> contours;
cv::findContours(image,
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("test.jpg");
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.jpg",0); // testing the bounding box
cv::Rect r0= cv::boundingRect(cv::Mat(contours[0]));
cv::rectangle(result,r0,cv::Scalar(0),2);
// 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);
CvBox2D End_Rage2D; CvMemStorage *storage = cvCreateMemStorage(0); //开辟内存空间
CvSeq* contour = NULL; //CvSeq类型 存放检测到的图像轮廓边缘所有的像素值,坐标值特征的结构体以链表形式 cvFindContours( pSrcImage, storage, &contour, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);//这函数可选参数还有不少 for(; contour; contour = contour->h_next) //如果contour不为空,表示找到一个以上轮廓,这样写法只显示一个轮廓
//如改为for(; contour; contour = contour->h_next) 就可以同时显示多个轮廓
{ End_Rage2D = cvMinAreaRect2(contour); //代入cvMinAreaRect2这个函数得到最小包围矩形 这里已得出被测物体的角度,宽度,高度,和中点坐标点存放在CvBox2D类型的结构体中,主要工作基本结束。
std::cout <<" angle:\n"<<(float)End_Rage2D.angle << std::endl; //被测物体旋转角度
}
cv::waitKey();
return 0;
}
http://www.cnblogs.com/gnuhpc/archive/2012/10/13/2722876.html
http://www.cnblogs.com/gnuhpc/archive/2012/10/13/2722876.html
感谢