歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV 求外接矩形以及旋轉角度

OpenCV 求外接矩形以及旋轉角度

日期:2017/3/1 9:32:03   编辑:Linux編程

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

int main()
{
// Read input binary image

char *image_name = "test2.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);

// testing the bounding box


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;

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類型的結構體中,主要工作基本結束。

std::cout <<" angle:\n"<<(float)End_Rage2D.angle << std::endl; //被測物體旋轉角度

}
cv::waitKey();
return 0;


}

重新寫了一下這個代碼,還是稍微有點小問題,希望大家共同探討: http://www.linuxidc.com/Linux/2015-02/114136.htm

--------------------------------------分割線 --------------------------------------

Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV無法讀取視頻文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基於QT和OpenCV的人臉識別系統 http://www.linuxidc.com/Linux/2011-11/47806.htm

[翻譯]Ubuntu 14.04, 13.10 下安裝 OpenCV 2.4.9 http://www.linuxidc.com/Linux/2014-12/110045.htm

--------------------------------------分割線 --------------------------------------

OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved