设为首页 加入收藏

TOP

OpenCV轮廓检测,计算物体旋转角度(一)
2015-03-04 22:51:21 来源: 作者: 【 】 浏览:107
Tags:OpenCV 轮廓 检测 计算 物体 旋转 角度

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


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


?




?



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


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


#include "stdafx.h"


?


#include
#include
#include
#include
#include
#include



#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 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> 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>::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>::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)而不是st

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 设置系统屏幕亮度 下一篇OpenCV 实现哈哈镜效果

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: