久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

OpenCV輪廓檢測(cè),,計(jì)算物體旋轉(zhuǎn)角度

 cczzxx123 2016-03-07

 

 

效果還是有點(diǎn)問(wèn)題的,希望大家共同探討一下

 

 

// FindRotation-angle.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn),。//// findContours.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn),。//#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.1415926using namespace std;using namespace cv;int hough_line(Mat src){	//【1】載入原始圖和Mat變量定義   	Mat srcImage = src;//imread('1.jpg');  //工程目錄下應(yīng)該有一張名為1.jpg的素材圖	Mat midImage,dstImage;//臨時(shí)變量和目標(biāo)圖的定義	//【2】進(jìn)行邊緣檢測(cè)和轉(zhuǎn)化為灰度圖	Canny(srcImage, midImage, 50, 200, 3);//進(jìn)行一此canny邊緣檢測(cè)	cvtColor(midImage,dstImage, CV_GRAY2BGR);//轉(zhuǎn)化邊緣檢測(cè)后的圖為灰度圖	//【3】進(jìn)行霍夫線(xiàn)變換	vector lines;//定義一個(gè)矢量結(jié)構(gòu)lines用于存放得到的線(xiàn)段矢量集合	HoughLinesP(midImage, lines, 1, CV_PI/180, 80, 50, 10 );	//【4】依次在圖中繪制出每條線(xiàn)段	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】邊緣檢測(cè)后的圖="" imshow('【邊緣檢測(cè)后的圖】',="" 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);="" 轉(zhuǎn)為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=""><>> 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;=""><>>::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=""><>>::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 	//////////////////////////////////////////////////////////////////////////////	//霍夫變換進(jìn)行直線(xiàn)檢測(cè),此處使用的是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】進(jìn)行邊緣檢測(cè)和轉(zhuǎn)化為灰度圖	//Canny(result_line, tempimage, 50, 200, 3);//進(jìn)行一此canny邊緣檢測(cè)	//imshow('canny',tempimage);	//waitKey(0);	//cvtColor(tempimage,result_line, CV_GRAY2BGR);//轉(zhuǎn)化邊緣檢測(cè)后的圖為灰度圖	vector 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);=""><>>::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);  //開(kāi)辟內(nèi)存空間	CvSeq*      contour = NULL;     //CvSeq類(lèi)型 存放檢測(cè)到的圖像輪廓邊緣所有的像素值,,坐標(biāo)值特征的結(jié)構(gòu)體以鏈表形式	cvFindContours( pSrcImage, storage, &contour, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);//這函數(shù)可選參數(shù)還有不少	for(; contour; contour = contour->h_next)   //如果contour不為空,,表示找到一個(gè)以上輪廓,這樣寫(xiě)法只顯示一個(gè)輪廓		//如改為for(; contour; contour = contour->h_next) 就可以同時(shí)顯示多個(gè)輪廓	{  		End_Rage2D = cvMinAreaRect2(contour);  		//代入cvMinAreaRect2這個(gè)函數(shù)得到最小包圍矩形  這里已得出被測(cè)物體的角度,,寬度,高度,,和中點(diǎn)坐標(biāo)點(diǎn)存放在CvBox2D類(lèi)型的結(jié)構(gòu)體中,		//主要工作基本結(jié)束,。		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=""><'><(float)end_rage2d.angle>< std::endl;="" 被測(cè)物體旋轉(zhuǎn)角度="" }="" cv::imshow('lalalal',result);="" cv::waitkey();="" return="">


 

 

 

 

這個(gè)是原來(lái)實(shí)現(xiàn)的代碼的博客文章:

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

 

 

參考文獻(xiàn):

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

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

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

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多