opencv入门笔记之三 简单图像识别,识别线,点,圆,轮廓

opencv入门笔记之三 简单图像识别,识别线,点,圆,轮廓

Edge detection

int main(){
IplImage* girl = cvLoadImage(“road.jpg”, 0);
// convert color to grag
Mat boy;
//*************start******************************///
Mat cow = Mat(girl);
//Mat() convert IplImage to Mat
imshow(“cow”, cow);
Canny(cow, boy, 125, 350);
//125< |pixel - pixel| <350 (hysteresis thresholding)
imshow(“boy”, boy);
Mat Inv;
threshold(boy, Inv, 128, 255, THRESH_BINARY_INV);
// invert color
//*****************end**************************///
duration = static_cast(getTickCount()) - duration;
duration /= getTickFrequency();
cout << duration;
//////////////////////////////////////////////////////////////////////////
imshow(“Inv”, Inv);
waitKey(0);

}

2/5/2015 10:12 AM - Screen Clipping

Lines detection
Mat boundless = Mat(cvLoadImage(“road.jpg”, 0));
//convert color to grey
Mat bound;
Canny(boundless, bound, 125, 350);
vector lines;
HoughLines(bound, lines, 1, M_PI / 180, 80);
// if u wanna use PI there must be a #define _USE_MATH_DEFINES before math.h
vector::const_iterator it = lines.begin();
while (it != lines.end()){
float rho = (*it)[0];
float theta = (*it)[1];
if (theta < M_PI_4||theta>3*M_PI_4)
{
Point pt1(rho / cos(theta), 0);
Point pt2(rho - bound.rows*sin(theta) / cos(theta), bound.rows);
line(boundless,pt1, pt2, Scalar(255), 1);
}
else
{
Point pt1(0, rho / sin(theta));
Point pt2(bound.cols, rho - bound.cols*cos(theta) / sin(theta));
line(boundless, pt1, pt2, Scalar(255), 1);
}
++it;
}
imshow(“new”, boundless);

Probabilistic Hough transform
class findline
{
public:
findline() :deltarho(1), deltatheta(M_PI / 180), minvote(10), minlength(0.), maxGap(0.){}
// ~findline();
void setAccResolution(double rho=1, double theta=M_PI/180){
deltatheta = theta;
deltarho = rho;
}// smaller deltatheta and deltarho means more time and more precise
void setMinVote(int minv){
minvote = minv;
}// more votes, less lines
void setLineLengthAndGap(double length, double gap){
minlength = length;
maxGap = gap;
}// gap between lines and length of lines
vector findLines(Mat& binary){
lines.clear();
HoughLinesP(binary, lines, deltarho, deltatheta, minvote, minlength, maxGap);
return lines;
} //use probabilistic hough transform
void drawDetectedLines(Mat &image, Scalar color=Scalar(255,255,255)){
vector::const_iterator it2 = lines.begin();
while(it2 != lines.end()){
Point pt1((*it2)[0], (*it2)[1]);
Point pt2((*it2)[2], (*it2)[3]);
line(image, pt1, pt2, color);
++it2;
}
}// use const_iterator to draw lines

private:
Mat image;
vector lines;
double deltarho, deltatheta;
int minvote;
double minlength;
double maxGap;
};

void m(){
findline finder;
finder.setMinVote(80);
finder.setLineLengthAndGap(100, 20);
Mat image = imread(“ground.jpg”);
Mat coutours = Mat(cvLoadImage(“ground.jpg”, 0));
Canny(coutours, coutours, 125, 350);
// imshow(“hen”, coutours);
vector lines = finder.findLines(coutours);
finder.drawDetectedLines(image,Scalar(0,0,0));
imshow(“Hough”, image);
imwrite(“ok.jpg”, image);
}

vector

posted on 2015-02-05 20:59 rex686568 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Pomodori/p/4316620.html

你可能感兴趣的:(opencv入门笔记之三 简单图像识别,识别线,点,圆,轮廓)