opencv入门笔记之三 简单图像识别,识别线,点,圆,轮廓
2015-02-05 20:59
393 查看
Edge detection
int main(){
IplImage* girl = cvLoadImage(“road.jpg”, 0);
// convert color to grag
Mat boy;
//[b]*************start******************************[/b]///
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
//[b]*****************end**************************[/b]///
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
int main(){
IplImage* girl = cvLoadImage(“road.jpg”, 0);
// convert color to grag
Mat boy;
//[b]*************start******************************[/b]///
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
//[b]*****************end**************************[/b]///
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
相关文章推荐
- opencv学习笔记 在输入图像上寻找并绘制轮廓
- OpenCV 2 学习笔记(3): 一个简单的opencv程序:在Console上显示图像
- OpenCV入门:简单图像处理实例——平滑滤波
- 数字图像处理编成入门笔记——第7章 边沿检测与提取,轮廓跟踪
- 【OpenCV自学笔记】DYA3 测试旧版本OpenCV函数的使用和图像简单缩放
- OpenCV笔记8:一个简单的变换:图像的平滑
- 图像识别 python+opencv的简单人脸识别
- opencv-ios开发笔记5 利用二值图像简单提取轮廓
- Python下opencv使用笔记(一)(图像简单读取、显示与储存)
- 数字图像处理编成入门笔记——第10章 图象处理编程工具及简单的多媒体编程
- opencv学习笔记-入门(7)单通道的图像数据访问
- opencv入门笔记之二 操作图像像素点
- OpenCV学习笔记(8)VS2008 MFC下使用OpenCV2.0进行简单图像处理
- opencv-ios开发笔记6 利用canny简单提取轮廓
- OpenCV学习笔记(8)VS2008 MFC下使用OpenCV2.0进行简单图像处理
- opencv学习笔记-入门(7)单通道的图像数据访问
- OpenCV入门_学习笔记_读取、显示、处理图像
- python-opencv笔记 图像的读取和简单几何图形绘制
- Python OpenCV学习笔记之:图像轮廓处理
- Opencv图像识别从零到精通(23)----轮廓