任务描述:识别方向(19任务三加强版)
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat srcImage,dstImage1,dstImage2,dstImage3,dstImage4;
srcImage=imread(/home/ma/下载/pictures/1.jpg);
Mat zero= Mat::zeros(srcImage.size(),srcImage.type());
cvtColor(srcImage,dstImage1,COLOR_BGR2GRAY);
threshold(dstImage1,dstImage1,80,255,THRESH_BINARY);
Canny(dstImage1,dstImage2,3,9,3);
namedWindow(src,0);
imshow(src,srcImage);
vector<Vec4i>lines;
HoughLinesP(dstImage2,lines,1,CV_PI/180,80,50,10);
namedWindow(dst4,0);
imshow(dst4,dstImage2);
for(uint i=0;i<lines.size();i++)
{
Vec4i vec1=lines[i];
Point pt1=Point (vec1[0],vec1[1]);
Point pt2=Point (vec1[2],vec1[3]);
if(lines[i][0]<srcImage.cols*0.75&&lines[i][0]>srcImage.cols*0.2&&lines[i][1]<srcImage.rows*0.85&&lines[i][1]>srcImage.rows*0.25){
line(zero,pt1,pt2,Scalar(0,0,255),3);
double y=(double)lines[i][1]-lines[i][3];
double x=(double)lines[i+1][0]-lines[i+1][2];
double k1=(double)(lines[i][1]-lines[i][3])/(double)(lines[i][0]-lines[i][2]);
double k2=(double)(lines[i+1][1]-lines[i+1][3])/(double)(lines[i+1][0]-lines[i+1][2]);
if(0<y&&y<1436){
if(-1785<x&&x<-2){
if(-60<k1&&k1<12){
if(-60<k2&&k2<12){
if(lines.size()>24&&lines.size()<42){
cout<<cross<<endl;
}
}
}
}
}
if(-1150<y&&y<1664){
if(-3676<x&&x<0){
if(-64<k1&&k1<61){
if(-56.1<k2&&k2<56.8){
if(lines.size()>40&&lines.size()<50){
cout<<straight<<endl;
}
}
}
}
}
if(370<y&&y<1750){
if(-3830<x&&x<-140){
if(-8.5<k1&&k1<-2.5){
if(-8.5<k2&&k2<0){
if(lines.size()>10&&lines.size()<18){
cout<<straight<<endl;
}
}
}
}
}
if(-1590<y&&y<370){
if(-2470<x&&x<-50){
if(7<k1&&k1<7.2){
if(-0.33<k2&&k2<8.2){
if(lines.size()>20&&lines.size()<34){
cout<<left<<endl;
}
}
}
}
}
if(-472<y&&y<-281){
if(-1990<x&&x<-1.1){
if(0.28<k1&&k1<0.32){
if(0.31<k2&&k2<0.57){
if(lines.size()>19&&lines.size()<25){
cout<<left<<endl;
}
}
}
}
}
if(-2128<y&&y<0){
if(-2111<x&&x<509){
if(0.23<k1&&k1<0.36){
if(0.22<k2&&k2<0.32){
if(lines.size()>19&&lines.size()<25){
cout<<left<<endl;
}
}
}
}
}
if(-1360<y&&y<124){
if(-2360<x&&x<-6){
if(4<k1&&k1<4.8){
if(4<k2&&k2<4.8){
if(lines.size()>15&&lines.size()<34){
cout<<right<<endl;
}
}
}
}
}
// cout<<x=<<x<<endl;
// cout<<y=<<y<<endl;
// cout<<k1=<<k1<<endl;
// cout<<k2=<<k2<<endl;
// cout<<lines.size()<<endl;
}
}
namedWindow(dst1,0);
imshow(dst1,zero);
waitKey(0);
return 0;
}