#pragma region
#include <iostream>
#include <opencv4/opencv2/core/core.hpp>
#include <opencv4/opencv2/highgui.hpp>
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/imgproc/types_c.h>
#include "pigpio.h"
#include <thread>
#include <cmath>
#include <chrono>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <termios.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
using namespace std;
using namespace cv;
#pragma endregion
#define BAUD 9600//串口波特率
#pragma region
int MAX_YU = 140;
int MIN_YU = 60;
// PID 各个数值
float kr = 0.00000001, kl = -0.0000001, lr = 0, ll = 0, br, bl;
bool flag_avoid = false; // 躲避锥桶标志位
bool flag_zhang = false; // 未知
bool flag_voidcount = false; // 未知
bool flag_ren = false; // 人行道判断标志位
bool flag_yellow = false;
double limit_voidtime = 0.17;
bool flag_start; // 判断蓝色挡板标志位
bool flag_count; // 未知
double time_void = 100; // 未知
double angle; // 计算得到的角度
double angle_bi; // 避障角度
double angle_cover; //??????????
double speed_avoid = 10050; //???????
// double speed_delay = 10200;//?????????????????
double heigth;
double kuan;
double low = 0, high = 0;
Mat kernel_3 = Mat::ones(cv::Size(3, 3), CV_8U);
Mat kernel = getStructuringElement(MORPH_RECT, Size(1, 1));
int shu = 0;
int sum_avoid = 0;
double width, x_r, x_l; //,heigth;
Mat frame, ca;
static int ret;
static int GPS;
char r_buf[1024];
FILE* fp;
double angle_x = 0; //???????y??
float a[3], w[3], Angle[3], h[3];
int flag_picture, duo_flag;
// Rect list;cd ~
clock_t start_, end_, mid_time;
static void on(int, void*);
//double picture();
void GetROI(Mat src, Mat& ROI);
double PID(double error1);
vector<Point2f> get_lines_fangcheng(vector<Vec4i> lines);
void Set_gpio();
Rect Obstacles(Mat img);
Rect blue(Mat img); // 识别蓝色挡板
Rect yellow(Mat img); // 识别黄色挡板
string str = "sudo cp /home/pi/.Xauthority /root/";
int flag = system(str.c_str());
#pragma endregion
//5G
Mat img, img_HSV, img_HSV_mask, img_combined, img_per, img_clone, warped;
int start_flag = 1, find_blue_card_flag = 0, cross_flag = 0, music_flag = 0, avoid_flag = 0, car_blake_flag = 0;
int LAB_Bmin = 190, LAB_Bmax = 255, HSL_Lmin = 157, HSL_Lmax = 255;
int TIMECOUNT = 0; //定时器延时
int line_error = 0, line_error1 = 0, line_last_error = 0, line_last_error1 = 0;
float center_x = 0, left_x = 0, right_x = 0;
int line_y=190;
int rail_width_pix=56;//跑道一半宽度
Point mousePos(0, 0);
double kp = 0.28; // 0.155;//0.11?????;//0.16;//0.2;//0.3;//0.1???? 0.3????
double kd = 0.04; // 0.1;//0 ?????
double kp1 = 0.25,kd1=0.02;
double min_angle = 30; // 80 90 - 10
double max_angle = 30; // 100
int Angle_Z;
void cd(char* path)
{
chdir(path);
}
bool crossing(Mat image)
{
Mat roi, labels, stats, centroids, hui;
GetROI(image, roi);
//imshow("roi", roi);
cvtColor(roi, hui, COLOR_BGR2GRAY);
Mat binaryImage;
threshold(hui, binaryImage, 180, 255, cv::THRESH_BINARY);
imshow("binaryImage", binaryImage);
int numComponents = cv::connectedComponentsWithStats(binaryImage, labels, stats, centroids);
int crossingnum = 0;
for (int i = 1; i < numComponents; i++)
{
int area = stats.at<int>(i, cv::CC_STAT_AREA);
if (area > 200)
{
crossingnum++;
}
// cout<<area<<endl;
}
cout << "CrossingNum:" << crossingnum << endl;
if (crossingnum >= 4)
{
cout << "find crossing" << endl;
return true;
}
return false;
}
void gpio_init(void)
{
if (gpioInitialise() < 0)
exit(1);
gpioSetMode(13, PI_OUTPUT);
gpioSetPWMrange(13, 40000);
gpioSetPWMfrequency(13, 200);
gpioPWM(13,12000);
gpioDelay(1000000);
gpioSetMode(22, PI_OUTPUT);
gpioSetPWMfrequency(22, 50);
gpioSetPWMrange(22, 1000);
gpioPWM(22, 75); //
gpioSetMode(23, PI_OUTPUT);
gpioSetPWMfrequency(23, 50);
gpioSetPWMrange(23, 1000);
gpioPWM(23, 70); //大上下小
gpioSetMode(12, PI_OUTPUT); // 设置GPIO12为PWM输出
gpioSetPWMfrequency(12, 50); // 50Hz标准频率
gpioSetPWMrange(12, 30000); // 设置PWM范围
printf("GPIO initial successful");
}
void steering(int angle)
{
double value = (0.5 + (2.0 / 180.0) * angle) / 20 * 30000; // 100;//20000
gpioPWM(12, value);
}
void Start_motor(void)
{
cout << "1" << endl;
gpioPWM(13, 10400);
//gpioDelay(1000000);
/*for (int x = 12000; x >= 9000; x -= 300)
{
cout << x << endl;
gpioPWM(13, x);
gpioDelay(500000);
}*/
// 第二次启动时,可以注释掉 下面部分
//cout << "2" << endl;
//gpioPWM(13, 12000);
//gpioDelay(1000000);
// 到此为止
//cout << "3" << endl;
//gpioPWM(13, 11000);
//gpioDelay(1000000);
//cout << "4" << endl;
//gpioPWM(13, 10300);
//double value = speed_init; // ???动慢???
//gpioPWM(13, value);
}
void Control_stop(void)
{
gpioPWM(13, 12600); // 刹车
gpioDelay(50000);
gpioPWM(13, 12200); // 保护
//gpioDelay(3000000);
//gpioPWM(13, speed_mid); // 冲刺
}
int uart_open(int fd, const char* pathname)
{
fd = open(pathname, O_RDWR | O_NOCTTY);
if (-1 == fd)
{
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open %s success!\n", pathname);
if (isatty(STDIN_FILENO) == 0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
return fd;
}
int uart_set(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio, oldtio;
if (tcgetattr(fd, &oldtio) != 0) {
perror("SetupSerial 1");
printf("tcgetattr( fd,&oldtio) -> %d\n", tcgetattr(fd, &oldtio));
return -1;
}
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch (nBits)
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch (nEvent)
{
case 'o':
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'e':
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'n':
case 'N':
newtio.c_cflag &= ~PARENB;
break;
default:
break;
}
/*设置波特率*/
switch (nSpeed)
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if (nStop == 1)
newtio.c_cflag &= ~CSTOPB;
else if (nStop == 2)
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
if ((tcsetattr(fd, TCSANOW, &newtio)) != 0)
{
perror("com set error");
return -1;
}
printf("set done!\n");
return 0;
}
int uart_close(int fd)
{
assert(fd);
close(fd);
return 0;
}
int send_data(int fd, char* send_buffer, int length)
{
length = write(fd, send_buffer, length * sizeof(unsigned char));
return length;
}
int recv_data(int fd, char* recv_buffer, int length)
{
length = read(fd, recv_buffer, length);
return length;
}
void Control_Xun(void)
{
int angle = kp * line_error + kd * (line_error - line_last_error);
line_last_error = line_error;
angle = 90 - angle;
if (angle > 90 + max_angle) // 130
angle = 90 + max_angle;
if (angle < 90 - min_angle)
angle = 90 - min_angle;
//cout <<"line_error"<<line_error<<"angle:" << angle << endl;
//cout << "循迹处理完毕" << endl;
steering(angle);
}
void Control_avoid(void)
{
int angle = kp1 * line_error1 + kd1 * (line_error1 - line_last_error1); // PID 公式
line_last_error1 = line_error1;
angle = 90 - angle;
if (angle > 90 + max_angle) // 130
angle = 90 + max_angle;
if (angle < 90 - min_angle)
angle = 90 - min_angle;
cout << "avoid_angle:" << angle << endl;
steering(angle);
}
void GetROI(Mat src, Mat& ROI)
{
// 取得源图像的长和宽
int width = src.cols;
int height = src.rows;
Rect rect(Point(0, (height / 20) * 10), Point(width, (height / 20) * 17)); // 按比例截取
ROI = src(rect); // 赋值ROI
}
vector<Point2f> get_lines_fangcheng(vector<Vec4i> lines)
{
// 遍历概率霍夫变换检测到的直线
float k = 0; // 斜率
float b = 0; // 截距
vector<Point2f> lines_fangcheng;
for (unsigned int i = 0; i < lines.size(); i++)
{
k = (double)(lines[i][3] - lines[i][1]) / (double)(lines[i][2] - lines[i][0]);
b = (double)lines[i][1] - k * (double)lines[i][0];
lines_fangcheng.push_back(Point2f(k, b));
}
return lines_fangcheng;
}
Rect blue(Mat img)
{
Mat HSV, roi;
GetROI(img, roi);
cvtColor(roi, HSV, COLOR_BGR2HSV);
Scalar Lower(90, 50, 50);
Scalar Upper(130, 255, 255);
Mat mask;
inRange(HSV, Lower, Upper, mask);
Mat erosion_dilation;
erode(mask, erosion_dilation, kernel_3, Point(-1, -1), 1, BORDER_CONSTANT, morphologyDefaultBorderValue());
dilate(erosion_dilation, erosion_dilation, kernel_3, Point(-1, -1), 1, BORDER_CONSTANT, morphologyDefaultBorderValue());
Mat target;
bitwise_and(roi, roi, target, erosion_dilation);
Mat binary;
threshold(erosion_dilation, binary, 127, 255, THRESH_BINARY);
imshow("blue", binary);
vector<vector<Point>> contours;
findContours(binary, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
int maxContourIndex = -1;
double maxContourArea = 0.0;
for (int i = 0; i < contours.size(); i++)
{
double area = cv::contourArea(contours[i]);
if (area > maxContourArea)
{
maxContourArea = area;
maxContourIndex = i;
}
}
if (maxContourIndex != -1)
{
return boundingRect(contours[maxContourIndex]);
}
else
{
return Rect();
}
}
Rect Obstacles(Mat img)//识别到锥桶
{
Rect obstacle_object = blue(img);//识别到的蓝色物体
if (obstacle_object.height > 5 && obstacle_object.height < 100)//对识别到的蓝色物体限幅,
{
rectangle(img, Point(obstacle_object.x, obstacle_object.y + frame.rows / 2), Point(obstacle_object.x + obstacle_object.width, obstacle_object.y + obstacle_object.height + frame.rows / 2), Scalar(0, 255, 0), 3);
return obstacle_object;
}
return Rect();
}
int car_start(Mat img)
{
Rect start_flag = blue(img);
// cout << start_flag.width << " " << start_flag.height << endl;
if (start_flag.width > 50 && start_flag.height > 50)
{
return 0; // ??????????
}
return 1; // ????????I????
}
void camera(int angl_x, int angl_y)
{
gpioPWM(22, angl_x);//大左小右
gpioPWM(23, angl_y); //大上下小
}
void GPS_init(void)
{
char r_buf[1024];
bzero(r_buf, 1024);
GPS = uart_open(GPS, "/dev/ttyUSB0");/*串口号/dev/ttySn,USB口号/dev/ttyUSBn */
if (GPS == -1)
{
fprintf(stderr, "GPS error\n");
exit(EXIT_FAILURE);
}
if (uart_set(GPS, BAUD, 8, 'N', 1) == -1)
{
fprintf(stderr, "GPS set failed!\n");
exit(EXIT_FAILURE);
}
}
void ParseData(char chr)
{
static char chrBuf[100];
static unsigned char chrCnt = 0;
signed short sData[4];
unsigned char i;
time_t now;
chrBuf[chrCnt++] = chr;
if (chrCnt < 11) return;
if ((chrBuf[0] != 0x55) || ((chrBuf[1] & 0x50) != 0x50)) { printf("Error:%x %x\r\n", chrBuf[0], chrBuf[1]); memcpy(&chrBuf[0], &chrBuf[1], 10); chrCnt--; return; }
memcpy(&sData[0], &chrBuf[2], 8);
switch (chrBuf[1])
{
case 0x51:
for (i = 0; i < 3; i++) a[i] = (float)sData[i] / 32768.0 * 16.0;
time(&now);
//printf("\r\nT:%s a:%6.3f %6.3f %6.3f ", asctime(localtime(&now)), a[0], a[1], a[2]);
break;
case 0x52:
for (i = 0; i < 3; i++) w[i] = (float)sData[i] / 32768.0 * 2000.0;
//printf("w:%7.3f %7.3f %7.3f ", w[0], w[1], w[2]);
break;
case 0x53:
for (i = 0; i < 3; i++) Angle[i] = (float)sData[i] / 32768.0 * 180.0;
//printf("A:%7.3f %7.3f %7.3f ", Angle[0], Angle[1], Angle[2]);
Angle_Z = Angle[2] - 167;
printf("Z:%d \r\n", Angle_Z);
break;
case 0x54:
for (i = 0; i < 3; i++) h[i] = (float)sData[i];
//printf("h:%4.0f %4.0f %4.0f ", h[0], h[1], h[2]);
break;
}
chrCnt = 0;
}
void Get_GPS_data(void)
{
while(1)
{
ret = recv_data(GPS, r_buf, 44);
if (ret == -1)
{
fprintf(stderr, "uart read failed!\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < ret; i++) ParseData(r_buf[i]);
usleep(1000);
}
//usleep(1000);
}
void audio(void)
{
system("omxplayer /home/pi/car435/car.wav");
}
Mat HLS_Lthresh(const cv::Mat& img, int min_thresh = 220, int max_thresh = 255)
{
Mat hls;
cvtColor(img, hls, COLOR_BGR2HLS);
vector<Mat> channels;
split(hls, channels);
Mat l_channel = channels[1];
double minVal, maxVal;
minMaxLoc(l_channel, &minVal, &maxVal);
l_channel.convertTo(l_channel, CV_8UC1, 255.0 / maxVal);
Mat binary_output = Mat::zeros(l_channel.size(), CV_8UC1);
inRange(l_channel, min_thresh, max_thresh, binary_output);
return binary_output;
}
Mat LAB_BThreshold(const Mat& img, int min_thresh = 190, int max_thresh = 255)
{
Mat lab;
cvtColor(img, lab, COLOR_BGR2Lab);
vector<Mat> lab_channels;
split(lab, lab_channels);
Mat b_channel = lab_channels[2];
//cout<<b_channel<<endl;
double maxVal;
minMaxLoc(b_channel, nullptr, &maxVal);
//cout<<maxVal<<endl;
if (maxVal > 175) {
b_channel.convertTo(b_channel, CV_8UC1, 255.0 / maxVal);
}
Mat binary_output = Mat::zeros(b_channel.size(), CV_8UC1);
inRange(b_channel, min_thresh, max_thresh, binary_output);
return binary_output;
}
Mat combineThresholds(const Mat& img_LThresh, const Mat& img_BThresh)
{
Mat combined = Mat::zeros(img_BThresh.size(), CV_8UC1);
bitwise_or(img_LThresh, img_BThresh, combined);
return combined;
}
struct LaneData
{
vector<Point> left_points;
vector<Point> right_points;
vector<float> left_fit;
vector<float> right_fit;
vector<float> mid_fit;
vector<Rect> left_windows;
vector<Rect> right_windows;
};
Mat getHistogram(const Mat& binary_img){
Mat hist;
reduce(binary_img.rowRange(binary_img.rows / 2, binary_img.rows),
hist, 0, REDUCE_SUM, CV_32S);
return hist;
}
pair<int, int> findLaneBases(const Mat& hist) {
int midpoint = hist.cols / 2;
int quarter = midpoint / 2;
Point left_base, right_base;
minMaxLoc(hist.colRange(quarter, midpoint), 0, 0, 0, &left_base);
minMaxLoc(hist.colRange(midpoint, midpoint + quarter), 0, 0, 0, &right_base);
return { left_base.x + quarter, right_base.x + midpoint };
}
LaneData slidingWindow(const Mat& binary_img, int nwindows = 9, int margin = 20, int minpix = 10)
{
LaneData result;
auto [leftx, rightx] = findLaneBases(getHistogram(binary_img));
//cout<<"leftx:"<<leftx<<"rightx:"<<rightx<<endl;
int window_height = binary_img.rows / nwindows;
vector<Point> nonzero;
findNonZero(binary_img, nonzero);
for (int i = 0; i < nwindows; ++i) {
int win_y_low = binary_img.rows - (i + 1) * window_height;
int win_y_high = binary_img.rows - i * window_height;
Rect left_win(leftx - margin, win_y_low, 2 * margin, window_height);
Rect right_win(rightx - margin, win_y_low, 2 * margin, window_height);
result.left_windows.push_back(left_win);
result.right_windows.push_back(right_win);
for (const auto& pt : nonzero) {
if (left_win.contains(pt)) result.left_points.push_back(pt);
if (right_win.contains(pt)) result.right_points.push_back(pt);
}
if (result.left_points.size() > minpix)
leftx = mean(result.left_points)[0];
if (result.right_points.size() > minpix)
rightx = mean(result.right_points)[0];
}
return result;
}
vector<float> polyFit(const vector<Point>& points, int order = 2)
{
if (points.size() < order + 1)
{
cout << "data no enough for" << order << "profit" << endl; return {};
}
Mat A(points.size(), order + 1, CV_32F);
Mat B(points.size(), 1, CV_32F);
for (size_t i = 0; i < points.size(); ++i) {
float y = points[i].y;
A.at<float>(i, 0) = y * y;
A.at<float>(i, 1) = y;
A.at<float>(i, 2) = 1.0f;
B.at<float>(i, 0) = points[i].x;
}
Mat coeff;
solve(A, B, coeff, DECOMP_SVD);
//solve(A, B, coeff, DECOMP_QR);
return { coeff.at<float>(0), coeff.at<float>(1), coeff.at<float>(2) };
}
vector<float> calculateCenterLaneCoefficient(const vector<float>& left_fit, const vector<float>& right_fit)
{
if (left_fit.size() != 3 || right_fit.size() != 3) {
cout << "Invalid polynomial coefficients" << endl;
return {};
}
vector<float> center_fit(3);
center_fit[0] = (left_fit[0] + right_fit[0]) / 2.0f; // A 系数
center_fit[1] = (left_fit[1] + right_fit[1]) / 2.0f; // B 系数
center_fit[2] = (left_fit[2] + right_fit[2]) / 2.0f; // C 系数
//cout<<center_fit[0]<<center_fit[1]<<center_fit[2]<<endl;
return center_fit;
}
float calculateXFromPolynomial(const vector<float>& fit, float y)
{
if (fit.size() != 3) return 0.0f;
return fit[0] * y * y + fit[1] * y + fit[2];
}
void visualize(Mat& img, const LaneData& data) {
// 绘制滑动窗口
// for (const auto& win : data.left_windows)
// rectangle(img, win, Scalar(0,255,0), 2);
// for (const auto& win : data.right_windows)
// rectangle(img, win, Scalar(0,255,0), 2);
// 绘制车道线点
for (const auto& pt : data.left_points)
circle(img, pt, 3, Scalar(255, 0, 0), -1);
for (const auto& pt : data.right_points)
circle(img, pt, 3, Scalar(0, 0, 255), -1);
// 绘制拟合曲线
if (!data.left_fit.empty())
{
for (int y = 0; y < img.rows; ++y)
{
int x = data.left_fit[0] * y * y + data.left_fit[1] * y + data.left_fit[2];
if (x >= 0 && x < img.cols)
circle(img, Point(x, y), 2, Scalar(0, 0, 255), -1);
}
}
if (!data.right_fit.empty())
{
for (int y = 0; y < img.rows; ++y)
{
int x = data.right_fit[0] * y * y + data.right_fit[1] * y + data.right_fit[2];
if (x >= 0 && x < img.cols)
circle(img, Point(x, y), 2, Scalar(0, 0, 255), -1);
}
}
circle(img, Point(center_x, line_y), 3, Scalar(0, 255, 0), -1);
}
bool Contour_Area(vector<Point> contour1, vector<Point> contour2)
{
return contourArea(contour1) > contourArea(contour2);
}
Mat customEqualizeHist(const Mat& inputImage, float alpha)
{
Mat enhancedImage;
equalizeHist(inputImage, enhancedImage);
return alpha * enhancedImage + (1 - alpha) * inputImage;
}
void timer(void)//定时器中断
{
TIMECOUNT++;
std::cout << "timer:" << TIMECOUNT << std::endl;
}
void reset_timer(void)//定时器中断
{
TIMECOUNT = 0;
}
//int PID(int error)
//{
// const float Kp = 0.8f;
// int output = static_cast<int>(Kp * error);
//
// output = max(-30, min(30, output));
// return output;
//}
int timer_interrupt(void)//定时器
{
while (true)
{
//timer();
this_thread::sleep_for(chrono::milliseconds(50));
Control_Xun();
}
return 0;
}
void onTrackbar(int, void*)
{
LAB_Bmin=getTrackbarPos("LAB_Bmin", "TrackBars");
LAB_Bmax=getTrackbarPos("LAB_Bmax", "TrackBars");
HSL_Lmin=getTrackbarPos("HSL_Lmin", "TrackBars");
HSL_Lmax=getTrackbarPos("HSL_Lmax", "TrackBars");
}
void UI_init(void)
{
namedWindow("TrackBars",WINDOW_NORMAL);
resizeWindow("TrackBars", 320, 240);
moveWindow("TrackBars", 0, 50); // 设置窗口位置
createTrackbar("LAB_Bmin", "TrackBars", &LAB_Bmin, 255, onTrackbar);
setTrackbarPos("LAB_Bmin", "TrackBars", LAB_Bmin);
createTrackbar("LAB_Bmax", "TrackBars", &LAB_Bmax, 255, onTrackbar);
setTrackbarPos("LAB_Bmax", "TrackBars", LAB_Bmax);
createTrackbar("HSL_Lmin", "TrackBars", &HSL_Lmin, 255, onTrackbar);
setTrackbarPos("HSL_Lmin", "TrackBars", HSL_Lmin);
createTrackbar("HSL_Lmax", "TrackBars", &HSL_Lmax, 255, onTrackbar);
setTrackbarPos("HSL_Lmax", "TrackBars", HSL_Lmax);
}
void onMouse(int event, int x, int y, int flags, void* userdata)
{
mousePos = Point(x, y);
}
void test(void)
{
while(1)
{
line_error=-100;
this_thread::sleep_for(chrono::milliseconds(500));
line_error=100;
this_thread::sleep_for(chrono::milliseconds(500));
}
}
void find_blue_card(void)//检测到蓝色挡板
{
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(img_HSV_mask, contours, hierarcy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
if (contours.size() > 0)
{
sort(contours.begin(), contours.end(), Contour_Area);
//cout<<cStart_motorontours.size()<<endl;
vector<vector<Point>> newContours;
for (const vector<Point>& contour : contours)
{
Point2f center;
float radius;
minEnclosingCircle(contour, center, radius);
if (center.y > 90 && center.y < 160)
{
newContours.push_back(contour);
}
}
contours = newContours;
if (contours.size() > 0)
{
if (contourArea(contours[0]) > 500)
{
cout << "find blue card" << endl;
Point2f center;
float radius;
minEnclosingCircle(contours[0], center, radius);
circle(img, center, static_cast<int>(radius), Scalar(0, 255, 0), 2);
find_blue_card_flag = 1;
}
else
{
cout << "not blue card" << endl;
}
}
}
else
{
cout << "not blue card" << endl;
}
}
void find_blue_card_remove(void)//蓝色挡板移开
{
cout << "detecting blue card remove" << endl;
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(img_HSV_mask, contours, hierarcy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
if (contours.size() > 0)
{
sort(contours.begin(), contours.end(), Contour_Area);
vector<vector<Point>> newContours;
for (const vector<Point>& contour : contours)
{
Point2f center;
float radius;
minEnclosingCircle(contour, center, radius);
if (center.y > 90 && center.y < 160)
{
newContours.push_back(contour);
}
}
contours = newContours;
if (contours.size() == 0)
{
start_flag = 0;
cout << "blue card remove" << endl;
//Start_motor();
//sleep(2);
}
}
else
{
start_flag = 0;
cout << "blue card remove" << endl;
//Start_motor();
//sleep(2);
}
}
int crossroad(void)//斑马线识别
{
VideoCapture capture;
capture.open(2);
if (!capture.isOpened())
{
cout << "Can not open camera" << endl;
return -1;
}
else
cout << "camera open successful" << endl;
capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
capture.set(cv::CAP_PROP_FPS, 30);
capture.set(cv::CAP_PROP_FRAME_WIDTH, 320);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 240);
this_thread::sleep_for(chrono::milliseconds(2000)); //让图像稳定
cout << "Program started" << endl;
Mat img_cross;
Mat blur;
while (1)
{
capture >> img_cross;
GaussianBlur(img_cross, blur, Size(5, 5), 0);
Mat hsv;
cvtColor(blur, hsv, COLOR_BGR2HSV);
Scalar lower_white = Scalar(0, 0, 200);
Scalar upper_white = Scalar(180, 40, 255);
Mat cross_mask;
inRange(hsv, lower_white, upper_white, cross_mask);
// Mat edges;
// Canny(blur, edges, 50, 150);
// bitwise_or(cross_mask, edges, cross_mask);
Mat kernel = getStructuringElement(MORPH_RECT, Size(7, 7));
//dilate(mask1, mask1, kernel);
//erode(mask1, mask1, kernel);
morphologyEx(cross_mask, cross_mask, MORPH_CLOSE, kernel);
//Rect roi(0,120,320,120);//shang
Rect roi(0,260,640,220);//xia
Mat region = cross_mask(roi);
//imshow("combined_image", region);
//imshow("Full Mask", cross_mask); // 完整图像
//imshow("ROI Region", region); // 仅用于调试检测区
//imshow("combined_image", mask1_roi);
int stripe_count = 0, transitions = 0;
for (int i = 0; i < region.rows; i++)
{
transitions = 0;
for (int j = 5; j < region.cols - 5; j++) {
if (region.at<uchar>(i, j) != region.at<uchar>(i, j - 1))
transitions++;
}
//cout<<"transitions:"<<transitions<<endl;
if (transitions >= 6) stripe_count++;
}
cross_flag = (stripe_count >= 16) ? 1 : 0;
cout << "stripe_count"<<stripe_count<<"cross_flag:" << cross_flag << endl;
if (cv::waitKey(1) == 27)break;
}
gpioTerminate();
capture.release(); // 释放摄像头
destroyAllWindows(); // 关闭所有窗口
//uart_close(GPS);
return 0;
}
Mat per_wt(Mat& frame)
{
int img_h, img_w;
img_clone = frame.clone();
img_h = img_clone.rows; // 图像高度
img_w = img_clone.cols;
vector<cv::Point2f> src = {
Point2f(125, 198),
Point2f(213, 198),
Point2f(295, 237),
Point2f(55, 237)
};
vector<cv::Point2f> dst = {
Point2f(100,0),
Point2f(img_w - 100, 0),
Point2f(img_w - 100, img_h),
Point2f(100, img_h)
};
// 定义目标坐标(变换后的四个点)
vector<cv::Point> polygon;
for (const auto& pt : src)
{
polygon.push_back(Point(static_cast<int>(pt.x), static_cast<int>(pt.y)));
}
polylines(img_clone, polygon, 1, Scalar(0, 0, 255), 3, cv::LINE_AA);
// 计算透视变换矩阵
Mat M = getPerspectiveTransform(src, dst);
Mat Minv = getPerspectiveTransform(dst, src);
// 应用透视
warpPerspective(frame, warped, M, cv::Size(img_w, img_h), cv::INTER_LINEAR);
waitKey(5);
return warped;
}
int find_line(void) //循迹线程
{
// 初始化程序
//UI_init();
VideoCapture capture;
capture.open(0);
if (!capture.isOpened())
{
cout << "Can not open camera" << endl;
return -1;
}
else
cout << "camera open successful" << endl;
capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
capture.set(cv::CAP_PROP_FPS, 30);
capture.set(cv::CAP_PROP_FRAME_WIDTH, 320);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 240);
this_thread::sleep_for(chrono::milliseconds(1000)); //让图像稳定
cout << "Program started" << endl;
while (1)
{
capture >> img;
if (start_flag == 1)
{
cvtColor(img, img_HSV, COLOR_BGR2HSV);
Scalar HSV_L = Scalar(100, 43, 46);
Scalar HSV_H = Scalar(124, 255, 255);
inRange(img_HSV, HSV_L, HSV_H, img_HSV_mask);
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(img_HSV_mask,img_HSV_mask, MORPH_OPEN, kernel);
morphologyEx(img_HSV_mask, img_HSV_mask, MORPH_CLOSE, kernel);
if (find_blue_card_flag == 0)find_blue_card();
else find_blue_card_remove();
}
else
{
img_per = per_wt(img);
Mat HLS_Lresult = HLS_Lthresh(img_per, HSL_Lmin, HSL_Lmax);
Mat LAB_Bresult = LAB_BThreshold(img_per, LAB_Bmin, LAB_Bmax);
img_combined = combineThresholds(HLS_Lresult, LAB_Bresult);
Mat white_image(img.size(), img.type(), Scalar::all(255));
LaneData lanes = slidingWindow(img_combined);
lanes.left_fit = polyFit(lanes.left_points);
lanes.right_fit = polyFit(lanes.right_points);
if (lanes.left_fit.size() != 3 || lanes.right_fit.size() != 3)
{
if(lanes.left_fit.size() != 3)center_x=lanes.right_fit[0] * line_y * line_y + lanes.right_fit[1]* line_y + lanes.right_fit[2]-rail_width_pix;
if(lanes.right_fit.size()!= 3)center_x=lanes.left_fit[0] * line_y * line_y + lanes.left_fit[1]* line_y + lanes.left_fit[2]+rail_width_pix;
cout<<center_x<<endl;
}
if (lanes.left_fit.size() == 3 && lanes.right_fit.size() == 3)
{
center_x=(lanes.right_fit[0] * line_y * line_y + lanes.right_fit[1]* line_y + lanes.right_fit[2]+lanes.left_fit[0] * line_y * line_y + lanes.left_fit[1]* line_y + lanes.left_fit[2])/2;
cout<<center_x<<endl;
}
line_error = center_x - 160;
//cout << left_x << "," << right_x << "," << center_x << "," << line_error << endl;
visualize(white_image, lanes);
//namedWindow("img", cv::WINDOW_NORMAL); // 创建可调整大小的窗口
//resizeWindow("img", 320, 240); // 设置窗口尺寸
//moveWindow("img", 320, 50); // 设置窗口位置
//imshow("img", img_clone);
//namedWindow("combined_image", cv::WINDOW_NORMAL); // 创建可调整大小的窗口
//resizeWindow("combined_image", 320, 240); // 设置窗口尺寸
//moveWindow("combined_image", 640, 50); // 设置窗口位置
//imshow("combined_image", img_combined);
//namedWindow("white_image", cv::WINDOW_NORMAL); // 创建可调整大小的窗口
//resizeWindow("white_image", 320, 240); // 设置窗口尺寸
//moveWindow("white_image", 960, 50); // 设置窗口位置
//imshow("white_image", white_image);
if(cross_flag==1){Control_stop();audio();break;}
}
if (cv::waitKey(1) == 27)break;
//if(car_blake_flag == 1) ;//break;//结束代码
}
gpioTerminate();
capture.release(); // 释放摄像头
destroyAllWindows(); // 关闭所有窗口
//uart_close(GPS);
return 0;
}
//—————————————————————————
int Blue_cone(void)
{
VideoCapture capture;
capture.open(2);
if (!capture.isOpened()) {
cout << "Error: Camera not available" << endl;
return -1;
}
else
cout << "camera open successful" << endl;
capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
capture.set(cv::CAP_PROP_FPS, 30);
capture.set(cv::CAP_PROP_FRAME_WIDTH, 640);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
this_thread::sleep_for(chrono::milliseconds(1000)); //让图像稳定
cout << "Program started" << endl;
Mat frame;
while (1)
{
capture >> frame;
if (!capture.read(frame)) { // 更健壮的帧读取检查
cout << "Frame read error!" << endl;
break;
}
Rect cone = Obstacles(frame);
if (cone.width > 0 && cone.height > 0) {
// 计算障碍物中心与画面中心的水平偏移
int obstacle_center_x = cone.x + cone.width / 2;
int frame_center_x = frame.cols / 2;
int line_error1= obstacle_center_x - frame_center_x; // 水平偏移量
Control_avoid();
imshow("blue_cone", frame);
}
if (waitKey(1) == 27) break; // ESC退出
}
gpioTerminate();
capture.release(); // 释放摄像头
destroyAllWindows(); // 关闭所有窗口
return 0;
}
//————————————————————————--
int main()
{
gpioTerminate();
gpio_init();
GPS_init();
camera(72,86);
steering(90);
//thread thread1(find_line);//循迹线程
thread thread2(Get_GPS_data);//test
thread thread3(timer_interrupt);//定时器功能
//thread thread4(crossroad);
thread thread5(Blue_cone);
//thread1.join();
thread2.join();
thread3.join();
//thread4.join();
thread5.join();
return 0;//GPS_init();
}
//取数据调试的main函数
/*int main(void)
{
UI_init();
gpioTerminate();
gpio_init();
camera(72,86);
steering(90);
// Mat img = imread("C:/Users/wtcat/CLionProjects/untitled7/test_images/225.jpg",IMREAD_COLOR_BGR);
// if(img.empty())
// {
// std::cerr << "Error: Could not load the image" << std::endl;
// return -1;
// }
VideoCapture capture;
capture.open(2);
if (!capture.isOpened())
{
cout << "Can not open camera!" << std::endl;
return -1;
}
capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
capture.set(cv::CAP_PROP_FPS, 30);
capture.set(cv::CAP_PROP_FRAME_WIDTH, 320);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 240);
this_thread::sleep_for(chrono::milliseconds(1000)); //让图像稳定
Mat img_per,img_combined;
string windowName = "XY";
namedWindow(windowName, WINDOW_AUTOSIZE);
setMouseCallback(windowName, onMouse, NULL);
while (1)
{
capture>>img;
img_per=per_wt(img);
string coordText = "X=" + to_string(mousePos.x) + " Y=" + to_string(mousePos.y);
putText(img_clone, coordText, Point(15, 35),FONT_HERSHEY_SIMPLEX, 0.7, Scalar(0, 255, 100), 2);
moveWindow(windowName, 320, 50);
imshow(windowName, img_clone);
Mat HLS_Lresult = HLS_Lthresh(img_per,HSL_Lmin,HSL_Lmax);
Mat LAB_Bresult = LAB_BThreshold(img_per,LAB_Bmin,LAB_Bmax);
img_combined=combineThresholds(HLS_Lresult, LAB_Bresult);
//img_combined=HLS_Lresult;
Mat white_image(img.size(), img.type(), Scalar::all(255));
LaneData lanes = slidingWindow(img_combined);
lanes.left_fit = polyFit(lanes.left_points);
lanes.right_fit = polyFit(lanes.right_points);
visualize(white_image, lanes);
namedWindow("combined_image", cv::WINDOW_NORMAL); // 创建可调整大小的窗口
resizeWindow("combined_image", 320, 240); // 设置窗口尺寸
moveWindow("combined_image", 640, 50); // 设置窗口位置
imshow("combined_image", img_combined);
namedWindow("white_image", cv::WINDOW_NORMAL); // 创建可调整大小的窗口
resizeWindow("white_image", 320, 240); // 设置窗口尺寸
moveWindow("white_image", 960,50); // 设置窗口位置
imshow("white_image", white_image);
if (cv::waitKey(1) == 27)
{
break;
}
//if(car_blake_flag == 1) ;//break;//结束代码
}
capture.release(); // 释放摄像头
destroyAllWindows(); // 关闭所有窗口
return 0;
}*/
为什么该程序图传中绿色线框没有框住蓝色锥桶
最新发布