程序没有写完整,大概功能就是实现了,希望大家分享学习,把他改对

// 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> #pragma comment(lib,"opencv_core2410d.lib")
#pragma comment(lib,"opencv_highgui2410d.lib")
#pragma comment(lib,"opencv_imgproc2410d.lib") #define PI 3.1415926 int main()
{
// Read input binary image char *image_name = "test2.jpg";
cv::Mat image = cv::imread(image_name,0);
if (!image.data)
return 0; cv::namedWindow("Binary Image");
cv::imshow("Binary Image",image); // 从文件中加载原图
IplImage *pSrcImage = cvLoadImage(image_name, 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(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 original");
cv::imshow("Contours on original",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 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;
} 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://blog.csdn.net/wangyaninglm/article/details/43959947

OpenCV 求外接矩形以及旋转角度的更多相关文章

  1. opencv轮廓外接矩形

    1.寻找轮廓 api void cv::findContours( InputOutputArray image, OutputArrayOfArrays contours, OutputArray ...

  2. Opencv 最小外接矩形合并拼接

    前一篇画出了最小外接矩形,但是有时候画出来的矩形由于中间像素干扰或者是其他原因矩形框并不是真正想要的 如图1是一个信号的雨图,被矩形框分割成了多个小框: 需要合并矩形框达到的效果: 主要思想: 扫描两 ...

  3. OpenCV入门之获取图像的旋转角度

      在我们的日常生活中,所碰到的图像往往都有一定的倾斜.那么,如何用OpenCV来获取图像的旋转角度呢?   我们以下面的图片为例,简单介绍如何用OpenCV来获取图像的旋转角度.   可以看到,该图 ...

  4. Opencv绘制最小外接矩形、最小外接圆

    Opencv中求点集的最小外结矩使用方法minAreaRect,求点集的最小外接圆使用方法minEnclosingCircle. minAreaRect方法原型: RotatedRect minAre ...

  5. opencv学习之路(26)、轮廓查找与绘制(五)——最小外接矩形

    一.简介 二.轮廓最小外接矩形的绘制 #include "opencv2/opencv.hpp" using namespace cv; void main() { //轮廓最小外 ...

  6. opencv学习之路(25)、轮廓查找与绘制(四)——正外接矩形

    一.简介 二.外接矩形的查找绘制 #include "opencv2/opencv.hpp" using namespace cv; void main() { //外接矩形的查找 ...

  7. BZOJ 1185: [HNOI2007]最小矩形覆盖-旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标-备忘板子

    来源:旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标 BZOJ又崩了,直接贴一下人家的代码. 代码: #include"stdio.h" #include"str ...

  8. OpenCV代码:画出轮廓的外接矩形,和中心点

    #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include & ...

  9. Opencv 改进的外接矩形合并拼接方法

    上一篇中的方法存在的问题是矩形框不够精确,而且效果不能达到要求 这里使用凸包检测的方法,并将原来膨胀系数由20缩小到5,达到了更好的效果 效果图: 效果图: 代码: #include <open ...

随机推荐

  1. Unity 资源管理插件

    之所以写这个插件呢,就是为了方便整理项目中的资源文件,我记得之前好像也用了这么一个插件,但是也没去找,还是自己动手写一个吧,需要什么功能就看自己的需求. 在项目的过程中呢,已经写了一个插件来管理材质, ...

  2. JVM远程DEBUG(JPDA )

    原理 1. JPDA简介 JPDA(Java Platform Debugger Architecture)为Java平台上的调试器定义了一个标准的体系结构.该体系结构包括3个主要组成部分:JVM T ...

  3. Spring入门介绍(一)

    Spring是一个轻量级控制反转(IOC)和面向切面(AOP)的容器框架,它主要是为了解决企业应用开发的复杂性而诞生的. 目的:解决企业应用开发的复杂性. 功能:使用基本的javaBean代替EJB. ...

  4. 3.2、Android Studio在物理设备中运行APP

    当你构建一个Android应用时,在发布给用户之前,在物理设备上测试一下你的应用是非常必要的. 你可以使用Android设备作为运行.调试和测试应用的环境.包含在SDK中的工具让你在编译完成后在设备中 ...

  5. Android数据库Sqlite-android学习之旅(九)

    简介 sqilte是一个轻量级的数据库,满足数据库的基本操作,由于移动端的内存有限,所以sqilte刚好能满足移动端开发的基本要求. 废话不多说,上代码 1.首先介绍一下,sqlite的管理类SQLi ...

  6. 1082. Read Number in Chinese (25)

    题目如下: Given an integer with no more than 9 digits, you are supposed to read it in the traditional Ch ...

  7. Java进阶(三十五)java int与integer的区别

    Java进阶(三十五)java int与Integer的区别 前言 int与Integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而Integer是对象 ...

  8. 详解EBS接口开发之WIP模块接口

    总体说明 文档目的 本文档针对WIP模块业务功能和接口进行分析和研究,对采用并发请求方式和调用API方式分别进行介绍 内容 WIP模块常用标准表简介 WIP事物处理组成 WIP相关业务流程 WIP相关 ...

  9. Web开发的发展史

    英文出处:arunr.欢迎加入翻译小组. 导读:Arunr 把过去 15 年以来,Web开发从最初的纯 HTML 到 CGI.PHP\JSP\ASP.Ajax.Rails.NodeJS 这个过程简要地 ...

  10. SQL join 语句 画图果然更容易理解

    我认为 Ligaya Turmelle 的关于SQL联合(join)语句的帖子对于新手开发者来说是份很好的材料.SQL 联合语句好像是基于集合的,用韦恩图来解释咋一看是很自然而然的.不过正如在她的帖子 ...