Ask Your Question

azerty's profile - activity

2015-06-12 08:41:50 -0600 asked a question How to find the mass center of a line in a video capture

Hiiii,

We are student in a French School, and we need your help to find the center of a white line (Our project is a car which have to follow a lane). We have a problem with our opencv code and precisely to find the mass center of the line and follow it; bellow our code:

int main() { CvCapture*capture;

Mat camera;
Mat greyImg;
Mat roiImg;
char key;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector <Point>  approx;

//ouverture flux video
capture=cvCreateCameraCapture(CV_CAP_ANY); 

//vérification de l'ouverture du flux
if (!capture){
    cout << "Erreur dans ouverture du fichier source" << endl;
    return 0;
}

// definition des fenêtres
namedWindow("Original", WINDOW_AUTOSIZE);
namedWindow("centre", WINDOW_AUTOSIZE); 

//on redefinit la partie de la fenetre que l'on souhaite traiter
cv::Rect myROI(15,290,280,100);


//analyse du flux:
while(key!='q' &&key!='Q'){

    //on recupère le flux courrent
    camera = cvQueryFrame(capture);

    //on met la video en nuance de gris
    cvtColor(camera,greyImg,CV_RGB2GRAY);

    greyImg(myROI).copyTo(roiImg);

    //binarisation de l'image
    threshold(roiImg, roiImg,175,255,THRESH_BINARY_INV);

    Mat erodeElmt = getStructuringElement(MORPH_RECT, Size(3,3));
    Mat dilateElmt = getStructuringElement(MORPH_RECT, Size(5,5));
    erode(roiImg, roiImg, erodeElmt);
    dilate(roiImg,roiImg, dilateElmt);



    //renforce les contours des objets
    Canny(roiImg,roiImg,50,200); 

    //trouve les points sur les contours
    findContours(roiImg, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0));

    vector<Moments> mu(contours.size() );
    for(int i = 0; i<contours.size();i++)
    {   
        mu[i] =moments (contours[i], false );
    }

    vector<Point2f> mc(contours.size() );
    for(int i = 0; i<contours.size();i++)
    {
        mc[i] = Point2f ( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 );
        cout << "Le centre a pour coordonne x:#" <<  << "et y : #"<<  << endl;
        circle(camera, mc[i], 5, Scalar(0,255,0),-1,8,0);

        }



    imshow("Originalrecadre",camera);

    imshow("centre",roiImg);
    key=cvWaitKey(10);

}
cvDestroyAllWindows();
cvReleaseCapture(&capture);

return 0;

}

Thank you for your Help !!