所以我对C ++和OpenCV很陌生,我遇到了一个问题,我一遍又一遍地重复使用同一段代码。所以我想把它变成一个函数,我可以输入我的点,它会按照我输入的参数运行这个代码
所以我的问题是:我如何将下面的代码作为一个函数,其中一个函数可以用我自己的参数替换“myPoint”。所以我不需要多次复制粘贴代码
Vec3b Rightcolor = matImg.at<Vec3b>(myPoint); //defines the color at current positions
while(myPoint.x != cols){
myPoint.x = myPoint.x +1; //extend the arrow
int blue = Rightcolor .val[0];
int green = Rightcolor .val[1];
int red = Rightcolor .val[2];
Rightcolor = matImg.at<cv::Vec3b>(myPoint);
if(blue == 255 && green == 255 && red == 255){
break; //color is white at current position
}
}
//end of function
if (m_debug) { //this draws the line in the main file
line(matImg, bottomPoint,myPoint,cvScalar(0,0,102), 1, 8);
}
使用我的函数进行更新,但该函数不起作用,因为它没有画线,而我的代码下面还有其他代码:
Point increase(Mat img ,Point myPoint , int cols)
{
Vec3b rightLaneColor = img.at<Vec3b>(myPoint);
while(myPoint.x != cols){
myPoint.x = myPoint.x +1; //extend the arrow
int blue = rightLaneColor.val[0];
int green = rightLaneColor.val[1];
int red = rightLaneColor.val[2];
rightLaneColor = img.at<cv::Vec3b>(myPoint); //checks for color at current position
if(blue == 255 && green == 255 && red == 255){
break; //color is white at current position
}
}
return myPoint;
}
void LaneDetector::processImage() {
Mat mat_img(m_image); //IPL Is outdated
Mat gray;
Mat canny; //Canny for detecting edges
cvtColor(mat_img, grayImg, CV_BGR2GRAY);
Canny( grayImg, cannyImg, 50, 170, 3);
cvtColor(canny, mat_img, CV_GRAY2BGR);
cannyImg.release();
int rows = mat_img.rows;
int cols = mat_img.cols;
Point bottomPoint; /*Rightbottom Arrow Start*/
Point rightPoint; /*Right Arrow End*/
bottomPoint.x = cols/2;
bottomPoint.y = 350;
rightPoint.x = bottomPoint.x;
rightPoint.y = bottomPoint.y;
/*
Vec3b rightLaneColor = mat_img.at<Vec3b>(rightPoint); //defines the color at current positions
while(rightPoint.x != cols){
rightPoint.x = rightPoint.x +1; //extend the arrow
int blue = rightLaneColor.val[0];
int green = rightLaneColor.val[1];
int red = rightLaneColor.val[2];
rightLaneColor = mat_img.at<cv::Vec3b>(rightPoint); //checks for color at current position
if(blue == 255 && green == 255 && red == 255){
break; //color is white at current position
}
}
*/
increase(mat_img,rightPoint,cols);
if (m_debug) {
line(mat_img, bottomPoint,rightPoint,cvScalar(80,255,12), 1, 8);
}
imshow("Lanedetection", mat_img);
cvWaitKey(10);
//Steering instructions from here
SteeringData sd;
// Create container for finally sending the data.
Container c(Container::USER_DATA_1, sd);
// Send container.
getConference().send(c);
}