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

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://www.linuxidc.com/Linux/2015-02/114135.htm


这个是原来实现的代码的博客文章:http://www.linuxidc.com/Linux/2015-02/114135.htm

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
计算物体旋转角度,可以使用OpenCV中的函数`cv2.minAreaRect()`。该函数可以计算出包围物体的最小矩形区域,并返回该矩形区域的中心坐标、宽度、高度和旋转角度。 下面是一个用OpenCV计算物体旋转角度的示例代码: ```python import cv2 # 读入图像 image = cv2.imread("object.jpg") # 转为灰度图像 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 二值化图像 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 查找物体轮廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 计算包围物体的最小矩形区域 rect = cv2.minAreaRect(contours[0]) # 获取矩形的中心坐标、宽度、高度和旋转角度 center, size, angle = rect # 将浮点型坐标和尺寸转为整型 center = tuple(map(int, center)) size = tuple(map(int, size)) # 绘制矩形轮廓和中心点 box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(image, [box], 0, (0, 0, 255), 2) cv2.circle(image, center, 5, (0, 255, 0), -1) # 显示图像 cv2.imshow("image", image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 上述代码中,我们先读入一张图像并将其转为灰度图像和二值化图像,然后使用`cv2.findContours()`函数查找物体轮廓。接着使用`cv2.minAreaRect()`函数计算物体的最小矩形区域,并获取该矩形的中心坐标、宽度、高度和旋转角度。最后,我们绘制出物体的矩形轮廓和中心点,并显示图像。 需要注意的是,在上述代码中我们只处理了图像中的一个物体轮廓,如果图像中有多个物体轮廓,需要对每个轮廓都进行处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值