opencv学习之houghlinesp include include

include

using namespace cv;
using namespace std;
//值传递和引用的问题
void drawDetectLines(Mat& image,const vector& lines,Scalar color)
{
// 将检测到的直线在图上画出来
vector::const_iterator it=lines.begin();
while(it!=lines.end())
{
Point pt1((it)[0],(it)[1]);
Point pt2((it)[2],(it)[3]);
line(image,pt1,pt2,color,2); // 线条宽度设置为2
++it;
}
}
int main(){

Mat pic = imread("/Users/leung/Documents/imgs/Convolution_filter.png");
Mat pic_blur,pic_gray,pic_thresh;

//GaussianBlur(pic, pic_blur, Size(3,3), 0);
cvtColor(pic, pic_gray, COLOR_BGR2GRAY);
threshold(pic_gray, pic_thresh, 0, 0xff, THRESH_OTSU);
morphologyEx(pic_thresh, pic_blur, MORPH_OPEN, 5);

Mat seg = pic_blur.clone();
vector<vector<Point>> cnts;
findContours(seg, cnts, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
drawContours(pic, cnts, -1, Scalar(0,0,0xff));
Mat contours;
Canny(pic, contours, 50, 200);
threshold(contours,contours,128,255,THRESH_BINARY);
imshow("contours", pic);

vector<Vec4i> lines;

HoughLinesP(contours,lines,1,CV_PI/180,80,50,10);

//Scalar a(0,255,0);
drawDetectLines(pic,lines,Scalar(0,0,255));

float area;
Rect roi;
int count=0;

for(int i=0; i<cnts.size(); i++){
    vector<Point> c= cnts[i];
    area = contourArea(c);
    if (area < 10) {
        continue;
    }
    count++;
    roi = boundingRect(c);
    
    rectangle(pic, roi, Scalar(0,0,255));
    
}


imshow("pic_blur", pic);
waitKey();
return 0;

}
HoughLinesP(<#InputArray image#>, <#OutputArray lines#>, <#double rho#>, <#double theta#>, <#int threshold#>)

它的输入是一个二值的轮廓图像,往往是边缘检测得到的结果图像;它的输出是一个包含多个Vec2f点的数组,数组中的每个元素是一个二元浮点数据 对<rou,theta>,rou代表直线离坐标原点的距离,theta代表角度。第3和第4个参数代表步长,因为Hough变换实际上是一 个穷举的算法,rho表示距离的步长,theta代表角度的步长。第5个参数是一个阈值设置直接的最低投票个数