OpenCV轮廓检测,计算物体旋转角度
效果还是有点问题的,希望大家共同探讨一下
// 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轮廓检测,计算物体旋转角度的更多相关文章
- OpenCV 轮廓检测
使用OpenCV可以对图像的轮廓进行检测.这是之前用过的代码,挺简单的,回顾一下.主要要进行以下2步操作: 1.cvThreshold():对图像进行二值化处理 2.cvFindContours(): ...
- (转载)利用SIFT和RANSAC算法(openCV框架)实现物体的检测与定位,并求出变换矩阵(findFundamentalMat和findHomography的比较) 置顶
原文链接:https://blog.csdn.net/qq_25352981/article/details/46914837#commentsedit 本文目标是通过使用SIFT和RANSAC算法, ...
- OpenCV—Python 轮廓检测 绘出矩形框(findContours\ boundingRect\rectangle
千万注意opencv的轮廓检测和边缘检测是两码事 本文链接:https://blog.csdn.net/wsp_1138886114/article/details/82945328 1 获取轮廓 O ...
- OpenCV图像轮廓检测
轮廓检测: 轮廓检测的原理通俗的说就是掏空内部点,比如原图中有3*3的矩形点.那么就可以将中间的那一点去掉. 一.关键函数1.1 cvFindContours函数功能:对图像进行轮廓检测,这个函数将 ...
- 第十七节,OpenCV(学习六)图像轮廓检测
1.检测轮廓 轮廓检测是图像处理中经常用到的,OpenCV-Python接口中使用cv2.findContours()函数查找检测物体的轮廓. cv2.findContours(image, mode ...
- OpenCV 求外接矩形以及旋转角度
程序没有写完整,大概功能就是实现了,希望大家分享学习,把他改对 // FindRotation-angle.cpp : 定义控制台应用程序的入口点. // // findContours.cpp : ...
- 机器学习进阶-图像金字塔与轮廓检测-轮廓检测 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表示颜色的变换形式 ...
- opencv——轮廓发现与轮廓(二值图像)分析
引言 二值图像分析最常见的一个主要方式就是轮廓发现与轮廓分析,其中轮廓发现的目的是为轮廓分析做准备,经过轮廓分析我们可以得到轮廓各种有用的属性信息. 这里顺带提下边缘检测,和轮廓提取的区别: 边缘检测 ...
- OPENCV条形码检测与识别
条形码是当前超市和部分工厂使用比较普遍的物品,产品标识技术,使用摄像头检测一张图片的条形码包含有两个步骤,第一是定位条形码的位置,定位之后剪切出条形码,并且识别出条形码对应的字符串,然后就可以调用网络 ...
随机推荐
- GitLab服务器IP地址设置
最近使用GitLab 搭建了Git的私有仓库,但是发现私有仓库的地址居然是localhost,不是本机的IP地址,最后百度了一下,找了很久才找到,特此记录一下. 首先说明一下,我linux虚拟机的IP ...
- Android自定义异常类
当一个项目中,异常可能出现地方非常多的时候就需要考虑封装处理异常信息.本篇博客就对自定义异常做一个封装,模拟实际开发中的异常处理. 新建一个基类异常HException: public class H ...
- Compile C++ code in Matlab with OpenCV support
Provides a function named as "mex_opencv(src)" The code function mex_opencv(src) ARC = 'x6 ...
- 硬件模块化机器人操作系统 Hardware Robot Operating System (H-ROS)
原文网址:http://www.ros.org/news/2016/10/hardware-robot-operating-system-h-ros.html 推荐网址:https://h-ros.c ...
- frameset 与frame 设置的技巧
今天来写点不一样的.如下图: 实现的效果就是原生的类似于导航形式的frameset. frameset 注意: 包含frameset的网页应该只是作为框架而存在,所以不能有body标签. 这个标签可以 ...
- iOS常见控件的基本使用
UI相关类继承关系 UIView 常见属性和方法 UIView属性 UIView方法 UIControl 常用控件 UIImageView 图片显示控件android ImageView UISlid ...
- 关于React Native 安卓首屏白屏优化
问题描述 在android中,当点击某个rn模块的入口按钮,弹出rn的activity到rn的页面展现出来的过程中,会有很明显的白屏现象,不同的机型不同(cpu好的白屏时间短),大概1s到2s的时间. ...
- testng的使用
TestNG教程 TestNG是一个测试框架,其灵感来自JUnit和NUnit,但同时引入了一些新的功能,使其功能更强大,使用更方便. TestNG设计涵盖所有类型的测试:单元,功能,端到端,集成等, ...
- 【并发编程】ThreadPoolExecutor参数详解
ThreadPoolExecutor executor = new ThreadPoolExecutor( int corePoolSize, int maximumPoolSize, long ke ...
- Android 9Patch图片的使用-android学习之旅(十八)
9patch的使用方法 9patch图片常被用来做消息发送等的图片,只是缩放照片的部分区域,使得图片的整体形状不会受到影响,比较方便. 下面我们介绍一下: 在android的SDK安装目录下的tool ...