// 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;
}
--------------------------------------分割线 --------------------------------------
Ubuntu Linux下安装OpenCV2.4.1所需包
CentOS下OpenCV无法读取视频文件
[翻译]Ubuntu 14.04, 13.10 下安装 OpenCV 2.4.9
--------------------------------------分割线 --------------------------------------