#include <stdio.h>
#include <iostream>
#include <cv.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <highgui.h>
#include <unistd.h>
#include <raspicam/raspicam_cv.h>
#include <sys/time.h>
using namespace std;
using namespace cv;
class ColorDetector
{
private:
int minDist;
cv::Vec3b target;
cv::Mat result;
bool getDistance(cv::Vec3b color)
{
return abs(color[0] - target[0])<minDist && abs(color[1] - target[1])< minDist && abs(color[2] - target[2])<minDist;
}
public:
ColorDetector() :minDist(45)
{
target[0] = target[1] = target[2] = 0;
}
void setColorDistanceThreshold(int distance);
int getColorDistanceThreshold() const;
void setTargetColor(unsigned char red, unsigned char green, unsigned char blue);
void setTargetColor(cv::Vec3b color);
cv::Vec3b getTargetColor() const;
cv::Mat process(const cv::Mat &image);
};
void ColorDetector::setColorDistanceThreshold(int distance)
{
if (distance < 0)
distance = 0;
minDist = distance;
}
int ColorDetector::getColorDistanceThreshold() const
{
return minDist;
}
void ColorDetector::setTargetColor(unsigned char red, unsigned char green, unsigned char blue)
{
target[2] = red;
target[1] = green;
target[0] = blue;
}
void ColorDetector::setTargetColor(cv::Vec3b color)
{
target = color;
}
cv::Vec3b ColorDetector::getTargetColor() const
{
return target;
}
cv::Mat ColorDetector::process(const cv::Mat &image)
{
result.create(image.rows, image.cols, CV_8U);
cv::Mat_<cv::Vec3b>::const_iterator it = image.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::const_iterator itend = image.end<cv::Vec3b>();
cv::Mat_<uchar>::iterator itout = result.begin<uchar>();
for (; it != itend; ++it, ++itout)
{
if (getDistance(*it))
{
*itout = 0;
}
else
{
*itout = 255;
}
}
return result;
}
int main(int argc,char** argv)
{
double scale = 0.3;
int ave=0, sum=0,count=0,sum1=0,sum2=0;
ColorDetector cdetect;
cv::Mat image;
struct timeval tv1;
struct timeval tv2;
long start,stop;
float dis;
CvScalar s;
raspicam::RaspiCam_Cv Camera;
if(!Camera.open()){cout<<"Error opening"<<endl;return -1;}
for(int i =0;i<50;i++)
{
Camera.grab();
}
Camera.retrieve(image);
gettimeofday(&tv1,NULL);
Size dsize = Size(image.cols*scale,image.rows*scale);
Mat imagesuo = Mat(dsize,CV_32S);
resize(image, imagesuo,dsize);
cdetect.setTargetColor(18, 58, 114);
imagesuo = cdetect.process(imagesuo);
int* v = new int[image.rows];
memset(v,0,image.rows*4);
// cout<<"imagesuo.cols = "<<imagesuo.cols<<endl;
// cout<<"imagesuo.rows = "<<imagesuo.rows<<endl;
for(int x=0;x<(imagesuo.cols);x++){
for(int y =0;y<(imagesuo.rows);y++){
int value = imagesuo.at<uchar>(y,x);
if(value == 0)
{
v[x]++;
}
}
}
/* for(int x=0;x<imagesuo.cols;x++){
cout<<"x"<<x<<" v[x]"<<v[x]<<endl;
}*/
for(int x=0;x<imagesuo.cols;x++){
if(v[x]>imagesuo.rows/3)
{
if(count<12){
count++;
sum+=x;
}
else
break;
}
}
ave=sum/count;
if(v[ave-40]>10&&v[ave+40]<10)
cout<<"left turn"<<endl;
else if(v[ave+40]>10&&v[ave-40]<10)
cout<<"right turn"<<endl;
else
cout<<"go go go"<<endl;
/* cout<<"imagesuo.cols = "<< imagesuo.cols<<endl;
cout<<"imagesuo.rows = "<< imagesuo.rows<<endl;
cout<<"ave = "<<ave<<endl;
*/
if(ave < ((imagesuo.cols)/2-15))
cout<<"letf"<<endl;
else if(ave > (imagesuo.cols/2+15))
cout<<"right"<<endl;
else
cout<<"just ok"<<endl;
gettimeofday(&tv2,NULL);
start = tv1.tv_sec * 1000000 + tv1.tv_usec;
stop = tv2.tv_sec * 1000000 + tv2.tv_usec;
dis = (float)(stop - start) / 1000000 ;
cout<<dis<<endl;
Camera.release();
imwrite("/home/pi/camera/image.jpg",imagesuo);
cout<<"success"<<endl;
return 0;
}