OpenCV輪廓檢測,計算物體旋轉角度

 

 

效果仍是有點問題的,但願你們共同探討一下ios

 

 

// 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/41864251ui

 

 

參考文獻:spa

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

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

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

本文同步分享在 博客「shiter」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。orm

相關文章
相關標籤/搜索