效果还是有点问题的,希望大家共同探讨一下

// 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 using namespace std;
using namespace cv; int hough_line(Mat src)
{
//【1】载入原始图和Mat变量定义
Mat srcImage = src;//imread("1.jpg"); //工程目录下应该有一张名为1.jpg的素材图
Mat midImage,dstImage;//临时变量和目标图的定义 //【2】进行边缘检测和转化为灰度图
Canny(srcImage, midImage, 50, 200, 3);//进行一此canny边缘检测
cvtColor(midImage,dstImage, CV_GRAY2BGR);//转化边缘检测后的图为灰度图 //【3】进行霍夫线变换
vector<Vec4i> lines;//定义一个矢量结构lines用于存放得到的线段矢量集合
HoughLinesP(midImage, lines, 1, CV_PI/180, 80, 50, 10 ); //【4】依次在图中绘制出每条线段
for( size_t i = 0; i < lines.size(); i++ )
{
Vec4i l = lines[i];
line( dstImage, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(186,88,255), 1, CV_AA);
} //【5】显示原始图
imshow("【原始图】", srcImage); //【6】边缘检测后的图
imshow("【边缘检测后的图】", midImage); //【7】显示效果图
imshow("【效果图】", dstImage); //waitKey(0); return 0;
} int main()
{
// Read input binary image char *image_name = "test.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); //imshow("lll",result);
//waitKey(0); // testing the bounding box
//////////////////////////////////////////////////////////////////////////////
//霍夫变换进行直线检测,此处使用的是probabilistic Hough transform(cv::HoughLinesP)而不是standard Hough transform(cv::HoughLines) cv::Mat result_line(image.size(),CV_8U,cv::Scalar(255));
result_line = result.clone(); hough_line(result_line); //Mat tempimage; //【2】进行边缘检测和转化为灰度图
//Canny(result_line, tempimage, 50, 200, 3);//进行一此canny边缘检测
//imshow("canny",tempimage);
//waitKey(0); //cvtColor(tempimage,result_line, CV_GRAY2BGR);//转化边缘检测后的图为灰度图
vector<Vec4i> lines; cv::HoughLinesP(result_line,lines,1,CV_PI/180,80,50,10); for(int i = 0; i < lines.size(); i++)
{
line(result_line,cv::Point(lines[i][0],lines[i][1]),cv::Point(lines[i][2],lines[i][3]),Scalar(0,0,0),2,8,0);
}
cv::namedWindow("line");
cv::imshow("line",result_line);
//waitKey(0); /////////////////////////////////////////////////////////////////////////////////////////////
// //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;
CvPoint2D32f rectpoint[4];
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类型的结构体中,
//主要工作基本结束。
for(int i = 0;i< 4;i++)
{
//CvArr* s=(CvArr*)&result;
//cvLine(s,cvPointFrom32f(rectpoint[i]),cvPointFrom32f(rectpoint[(i+1)%4]),CV_G(0,0,255),2);
line(result,cvPointFrom32f(rectpoint[i]),cvPointFrom32f(rectpoint[(i+1)%4]),Scalar(125),2);
}
cvBoxPoints(End_Rage2D,rectpoint); std::cout <<" angle:\n"<<(float)End_Rage2D.angle << std::endl; //被测物体旋转角度 }
cv::imshow("lalalal",result);
cv::waitKey();
return 0; }

这个是原来实现的代码的博客文章:

http://blog.csdn.net/wangyaninglm/article/details/41864251

参考文献:

http://blog.csdn.net/z397164725/article/details/7248096

http://blog.csdn.net/fdl19881/article/details/6730112

http://blog.csdn.net/mine1024/article/details/6044856

OpenCV轮廓检测,计算物体旋转角度的更多相关文章

  1. OpenCV 轮廓检测

    使用OpenCV可以对图像的轮廓进行检测.这是之前用过的代码,挺简单的,回顾一下.主要要进行以下2步操作: 1.cvThreshold():对图像进行二值化处理 2.cvFindContours(): ...

  2. (转载)利用SIFT和RANSAC算法(openCV框架)实现物体的检测与定位,并求出变换矩阵(findFundamentalMat和findHomography的比较) 置顶

    原文链接:https://blog.csdn.net/qq_25352981/article/details/46914837#commentsedit 本文目标是通过使用SIFT和RANSAC算法, ...

  3. OpenCV—Python 轮廓检测 绘出矩形框(findContours\ boundingRect\rectangle

    千万注意opencv的轮廓检测和边缘检测是两码事 本文链接:https://blog.csdn.net/wsp_1138886114/article/details/82945328 1 获取轮廓 O ...

  4. OpenCV图像轮廓检测

    轮廓检测: 轮廓检测的原理通俗的说就是掏空内部点,比如原图中有3*3的矩形点.那么就可以将中间的那一点去掉. 一.关键函数1.1  cvFindContours函数功能:对图像进行轮廓检测,这个函数将 ...

  5. 第十七节,OpenCV(学习六)图像轮廓检测

    1.检测轮廓 轮廓检测是图像处理中经常用到的,OpenCV-Python接口中使用cv2.findContours()函数查找检测物体的轮廓. cv2.findContours(image, mode ...

  6. OpenCV 求外接矩形以及旋转角度

    程序没有写完整,大概功能就是实现了,希望大家分享学习,把他改对 // FindRotation-angle.cpp : 定义控制台应用程序的入口点. // // findContours.cpp : ...

  7. 机器学习进阶-图像金字塔与轮廓检测-轮廓检测 1.cv2.cvtColor(图像颜色转换) 2.cv2.findContours(找出图像的轮廓) 3.cv2.drawContours(画出图像轮廓) 4.cv2.contourArea(轮廓面积) 5.cv2.arcLength(轮廓周长) 6.cv2.aprroxPloyDP(获得轮廓近似) 7.cv2.boudingrect(外接圆)..

    1. cv2.cvtcolor(img, cv2.COLOR_BGR2GRAY) # 将彩色图转换为灰度图 参数说明: img表示输入的图片, cv2.COLOR_BGR2GRAY表示颜色的变换形式 ...

  8. opencv——轮廓发现与轮廓(二值图像)分析

    引言 二值图像分析最常见的一个主要方式就是轮廓发现与轮廓分析,其中轮廓发现的目的是为轮廓分析做准备,经过轮廓分析我们可以得到轮廓各种有用的属性信息. 这里顺带提下边缘检测,和轮廓提取的区别: 边缘检测 ...

  9. OPENCV条形码检测与识别

    条形码是当前超市和部分工厂使用比较普遍的物品,产品标识技术,使用摄像头检测一张图片的条形码包含有两个步骤,第一是定位条形码的位置,定位之后剪切出条形码,并且识别出条形码对应的字符串,然后就可以调用网络 ...

随机推荐

  1. 1、win10下连接本地系统上的Linux操作系统(分别以Nat方式和桥接模式实现)

    1.win10下连接本地系统上的Linux操作系统(分别以Nat方式和桥接模式实现) 一.准备知识:win10下打开Administrator的方式 在win10操作系统中,Administrator ...

  2. Web自动化框架LazyUI使用手册(6)--8种控件对应的class,及可对其进行的操作

    概述: 本文详述8种控件对应的class,及可对其进行的操作 回顾: 回顾一下,下文中的工具设计思路部分: http://blog.csdn.net/kaka1121/article/details/ ...

  3. React Native组件只Image

    不管在Android还是在ios原生的开发中,图片都是作为控件给出来的,在RN中也有这么一个控件(Image).根据官网的资料,图片分为本地静态图片,网络图片和混合app资源.一下分类介绍来源官网. ...

  4. Android基于Retrofit2.0 +RxJava 封装的超好用的RetrofitClient工具类(六)

    csdn :码小白 原文地址: http://blog.csdn.net/sk719887916/article/details/51958010 RetrofitClient 基于Retrofit2 ...

  5. android 获取SD卡的图片及其路径

    1.首先是intent的设置: private static final int IMAGECODE = 0; Intent imageIntent = new Intent(Intent.ACYIO ...

  6. iOS开发之二:UIWindow与UIView

    1.UIWindow UIWindow 继承自UIView,它是整个应用的容器,一般来说一个应用就只有一个UIWindow. 如果不使用storyboard 时,需要我们自己创建UIWindow.实例 ...

  7. 1033. To Fill or Not to Fill (25) -贪心算法

    题目如下: With highways available, driving a car from Hangzhou to any other city is easy. But since the ...

  8. UNIX环境高级编程——死锁

    操作系统中有若干进程并发执行,它们不断申请.使用.释放系统资源,虽然系统的进程协调.通信机制会对它们进行控制,但也可能出现若干进程都相互等待对方释放资源才能继续运行,否则就阻塞的情况.此时,若不借助外 ...

  9. Android项目-高考作文项目架构(二)

    1, 普通的http json请求 请看下面架构草图: 这样就抽象出了其他Activity可能需要的Http Json请求的功能. 只要其他Activity有Http Json请求的需求都可以继承Ba ...

  10. HTML5 classList API接口

    原文地址:HTML5 classList API 原文日期: 2010年07月13日 翻译日期: 2013年08月23日 当我陷入JavaScrip和JavaScript类库框架之中时,我总是有种希望 ...