Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

error in unknown size x,y

Hi guys,

I have an error unknown size x,y, any suggestion?

#include <stdio.h> #include <iostream>#include <vector> #include <cmath> #include<opencv2/opencv.hpp> #include <opencv2/nonfree/features2d.hpp> #include<opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include "linefinder.h" #include "edgedetector.h"using namespace cv; int main(int argc, char** argv){
//-- CV Capture object for camera
CvCapture* capture;
//-- Frame Captured from capture object
Mat frame;
Mat lastFrame;
//-- Start capture from default camera
capture = cvCaptureFromFile(".\video1.avi");

//-- If capture was successful
if (capture)
{
    cv::namedWindow("Detected Lanes");
    cvSetWindowProperty("Detected Lanes", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
    int linesDetected = 20;
    //-- While this program is running
    //futline is used to store the slope of past lines in order to compare them between each other
    float futline[linesDetected];
    //the x coordinates of stored lines 
    float x[2 * (linesDetected)];
    //the y coordinates of stored lines
    float y[2 * (linesDetected)];
    std::vector<cv::Vec4i> lastLines;
    int b = 0;
    int frameCount = 0;

    LineFinder ld;
    ld.setLineLengthAndGap(40, 5);
    ld.setMinVote(10);
    while (true)
    {
        frame = cvQueryFrame(capture);
        if (!frame.empty()){

            cv::Mat image;
            cv::resize(frame, image, Size(), 0.5, 0.5, INTER_NEAREST);
            cv::Mat currentFrame;
            cv::resize(frame, currentFrame, Size(), 0.5, 0.5, INTER_NEAREST);
            int height = image.size().height;
            int width = image.size().width;
            cv::Rect myROI(width*0.167, height / 2, width*.833, height / 2);
            image = image(myROI);
            if (!image.data)
                return 0;
            EdgeDetector ed;
            ed.computeSobel(image);
            cv::Mat contours;
            cv::Canny(image, contours, 50, 200);
            cv::Mat contoursInv;
            cv::threshold(contours, contoursInv, 128, 255, cv::THRESH_BINARY_INV);
            cv::imshow("Edges", contours);
            std::vector<cv::Vec2f> lines;
            cv::HoughLines(contours, lines, 1, PI / 180, 10);
            cv::Mat result(contours.rows, contours.cols, CV_8U, cv::Scalar(255));
            image.copyTo(result);
            float slope[lines.size()];
            int f = 0;
            int e = 0;
            int col1 = 0;
            int col2 = 0;

            std::vector<cv::Vec2f>::const_iterator it = lines.begin();

            while (it != lines.end()) {

                float rho = (*it)[0]; // first element is distance rho
                float theta = (*it)[1]; // second element is angle theta

                if (theta < PI / 4. || theta > 3.*PI / 4.) { 

                    cv::Point pt1(rho / cos(theta), 0);

                    cv::Point pt2((rho - result.rows*sin(theta)) / cos(theta), result.rows);
                    cv::line(result, pt1, pt2, cv::Scalar(200, 0, 0), 10);

                }
                else {
                    cv::Point pt1(0, rho / sin(theta));
                    cv::Point pt2(result.cols, (rho - result.cols*cos(theta)) / sin(theta));
                    cv::line(result, pt1, pt2, cv::Scalar(200, 0, 0), 10);
                }


                ++it;
            }

            std::vector<cv::Vec4i> li = ld.findLines(contours);

            li = ld.removeLinesOfInconsistentOrientations(ed.getOrientation(), 0.4, 0.15, image);

            std::vector<cv::Vec4i>::const_iterator it2 = li.begin();

            int li_size = 0;

            while (it2 != li.end()) {

                if ((*it2)[0] > 30 && (*it2)[1] > 30 && (*it2)[2] >30 && (*it2)[3] > 30){

                    std::cout << "0 : " << (*it2)[0] << "\n";
                    std::cout << "1 : " << (*it2)[1] << "\n";
                    std::cout << "2 : " << (*it2)[2] << "\n";
                    std::cout << "3 : " << (*it2)[3] << "\n";
                    li_size++;

                }

                ++it2;

            }


            it2 = li.begin();


            b = 0;
            while (it2 != li.end()) {

                float x1 = (float)(*it2)[0];
                float x2 = (float)(*it2)[2];
                float y1 = (float)(*it2)[1];
                float y2 = (float)(*it2)[3];

                float abdist = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));


                float dx = (x2 - x1) / abdist;
                float dy = (y2 - y1) / abdist;

                slope[f] = dx / dy;


                float t1 = dx*(230 - x1) + dy*(444 - y1);
                float t2 = dx*(380 - x1) + dy*(444 - y1);
                float ex1 = t1*dx + x1;
                float ey1 = t1*dy + y1;
                float ex2 = t2*dx + x1;
                float ey2 = t2*dy + y1;

                float ecdist1 = sqrt(pow((ex1 - 230), 2) + pow((ey1 - 444), 2));
                float ecdist2 = sqrt(pow((ex2 - 380), 2) + pow((ey2 - 444), 2));

                if (ecdist1 < 20){
                    col1 = 1;
                }
                else if (ecdist1 == 20){
                    col1 = 1;
                }

                // test if the line intersects the circle 2
                if (ecdist2 < 20){
                    col2 = 1;
                }
                else if (ecdist2 == 20){
                    col2 = 1;
                }

                futline[b] = slope[f];
                x[e] = x1;
                x[e + 1] = x2;
                y[e] = y1;
                y[e + 1] = y2;
                e += 2;
                b++;
                //increment all the counters!
                ++f;
                ++it2;
            }

            ld.drawDetectedLines(currentFrame);
            std::cout << "Lines: (" << li_size << ")\n";
            if (li_size != 0){
                lastLines = li;
            }

            if (col1 == 1){
                cv::circle(image, cv::Point(230, 444), 20, cv::Scalar(0, 0, 255, 255), -1, 8, 0);
            }
            else{
                cv::circle(image, cv::Point(230, 444), 20, cv::Scalar(255, 0, 0, 255), -1, 8, 0);
            }


            if (col2 == 1){
                cv::circle(image, cv::Point(380, 444), 20, cv::Scalar(0, 0, 255, 255), -1, 8, 0);
            }
            else{
                cv::circle(image, cv::Point(380, 444), 20, cv::Scalar(255, 0, 0, 255), -1, 8, 0);
            }
            cv::imshow("Detected Lanes", currentFrame);

            lastFrame = currentFrame;

        }
        else{
            printf(" --(!) No captured frame -- Break!"); break;
        }
        int c = waitKey(10);
        if ((char)c == 'c') { break; };

    }
}
return 0; }